Beginner programmers often learn quickly that the ability to simply code in a particular language is only half the battle. The ability to write modular, scalable and reliable code is a different story — one that requires knowledge of best practices, attention to detail, and experience with reviewing and editing code to optimize it.
So, here are some of the Tips for Writing Better Code
Respect the pre-existing coding style
You have just joined a software project that runs for a while. The coding conventions used on the project are weird, and you really don’t like them. Perhaps, they force you to put parenthesis on a new line or let you write variable names with the first letter in the capital while you would prefer a small letter. Maintaining consistency across the codebase may seem a time-consuming task. However you should always remember that the product evolves with time and maintaining the code base will become difficult if consistency is not maintained. A few hours spent initially while writing the code will pay for itself in the long run by reducing the time it takes to debug issues and maintain the codebase.
Break your code down into methods
It is a good practice to break down complicated program logic into multiple methods. By doing so, you make it easier for the reader to understand the purpose of your code. Modularity, i.e. breaking a complex code sequence into smaller functions, is one of the important principles of object oriented programming.
A simple guideline can be followed to write modular programs. “If you have to perform the same task at multiple places in the codebase then create a function for performing that task and re-use the function at all places in the codebase’’. Modularity enables you to write flexible code which can be easily modified in the future by editing the function where changes are required. Contrary to this, if you have copy pasted the same code at multiple locations, then in future you will have to edit the code at all the different locations and test for all the functions.
Write easy to read code & use braces & indentation to improve readability
Programming languages defines operator precedence, and it is great that you are aware of it. But please keep in mind that every developer in your organization may not have mastered the operator precedence. Whenever you have to make a choice between writing readable code vs. writing concise code, always choose readability even if it means writing a few extra lines of code.
When you are writing production ready code, keep in mind that there are other people who are going to use it or build on top of it. Better readability of the code is going to help you in the peer review process which is a commonly accepted step before your code is pushed to production.
Learn your programming language idioms
There is no perfect programming language. Idioms get invented to work around the quirks of a specific programming language. Using idioms improves the maintainability of the code. You should learn and use them wherever possible.
Leverage others learning wherever necessary
The best part about programming is that you don’t have to reinvent the wheel. If you are facing some problem, then chances are that someone else would have faced the same problem and figured out a way to solve it or even better shared the code for their solution. Re-use the solutions developed by others so that you can focus on the features that really matter to your customers.