This is a beginner-friendly guide on how to improve your coding style when programming. Writing code becomes very different when you do it with a team, your tech lead might have valid opinions to why you should write your code a certain way, your company might adopt a coding standard in which you should adhere to. All of this is so the codebase is readable and easy to understand.
Trust me I just went through my first professional code review and it was thorough.
Now that we have touched on the what, when and why we can dive into 6 ways you can start improving before you start writing code professionally.
1. Use Linters and Code Formatters
These are static code analysis tools that are used to flag programming errors, bugs, or style errors etc. I find that with a lot of programming languages, when written professionally you come across opinions on how the code needs to be formatted or styled for readability, and sometimes it becomes difficult to make these standards a habit or remember all the formatting rules.
2. Comment your code
Comment your code (that was repeated for emphasis), avoid writing obvious comments, documentation should be relevant and informative. Commenting is a form of documentation, where comments are vital for me is when I am writing a function/method or constructing a class. It's always useful to indicate, that is if already not obvious
What the programming logic is doing and why
3. Be Consistent with your naming style
I remember I was once corrected for having inconsistencies with my naming style. This is another element that enhances code readability.
4. Limit the length of your lines
Coding Line limits are suggested to limit horizontal scrolling which makes reading code a little easier. However, some may argue its unnecessary since our screens have become wider and can accommodate enough characters which doesn't impede readability.
And opinions are always welcome, but according to famous published Coding Standards the line limit should be between 80-125 characters (HINT - You can configure prettier to follow these guidelines).
Personally, my line lengths are not long, I try to break lines into their own if I notice its becoming too long. But I do not have a hard coded number for how long my lines should be, however I am conscious that it doesn't become too long.
5. Indicate expected types of arguments and return values
6. Follow Accepted Coding Styles/Standards
The coding styles recommendations listed from 1-5 were derived from my experience writing code professionally and what was suggested to me, while some of them I have read about and implemented. In general, if you are writing code for yourself by yourself, these might not matter too much. However, it's good practice for when you start writing code professionally.
When that happens, the coding styles adopted might be different from team to team, or company to company. So in that case your go-to standard is what is agreed in your team and organization. Hopefully they align with what others are doing, but generally the rule of thumb is these standards are encouraged to standardize a level of code quality we can all agree to. And these standards vary.
What do you think? What other coding styles do you think is good practice? I would like to hear from you. Leave your thoughts below.