Top 7 programming points for a developer
Writing a program which is understandable by machine is much easier than writing it for human understandability or even if one has to understand the same code after a long time. Because as your system scale it is always good to write the code in a self-explanatory method.
Here are some points which should be helpful in writing a good code-
1) Use short functions
One function should be meant for one logic. Do not write all the code in a single function. Try to break it into smaller chunks and then plug them together. It helps in reusing the smaller chunks of code by different functions and also in modifying them later.
2) Validate your input
Always have validations on input in a function. Do not assume anything, your variable might not be getting a null value now but in future, if someone changes a logic in some other file which might affect the input of your function and you do not have proper validation, it may crash your system.
3) Keep cyclomatic complexity low
Keep you cyclomatic complexity as low as possible. For Example. Instead of writing like this -
if(condition){
// do something
}
you should have logic something like this -
if(!condition){
return;
}
// do something
4) Write test cases
Every code should have test cases and you should try to make the coverage 100% by covering all the scenarios and branches. It helps in keeping the stability of the system as it scales. As there will be changes in the code with time and you don’t want to break the original functionality, so if something breaks the test case will fail and it will prevent it from failing it on production servers.
5) Use a standard code format
Every IDE has its own formatter, use the same formatter throughout your application.
6) Documentation
Add comments to your code is the best documentation. Always add a doc for each function and Object classes. It should contain the logic of your function and what the function will be returning. It helps in understanding the function reading the code.
7) Naming convention
The names of variable and functions should short and explanatory. Short names help in keeping the code clean.