Did you know these coding guidelines???
Code readability is an important aspect of programming and it is one of the first things we learn as programmers.
Following are few coding guidelines every programmer should keep in mind when writing good code:
- Commenting and documentation
While coding we should follow certain standards for commenting the code and adhere to those. If we stick to a particular method of commenting the code, it helps in maintaining good knowledge of the overall working. If proper comments are placed, then, any other developer working on the same piece of code will find it much easier to understand your code.
- Consistent indentation
There is no “best” style for indentation that everyone should follow. What we can conclude is, the best style is a consistent style. If you are part of a team or if you are contributing code to a certain project, you should follow the existing style that is being used in that project rather than using your own.
- Avoid obvious comments
Commenting the code is a very good practice; however, it can be overdone or become redundant. When the text is obvious it is not required to be repeated within the comments. So, instead of repeating the same comment we can just combine it in a single line instead.
- Code grouping
Certain tasks require few lines of code. It is always a good idea to keep these tasks within separate blocks of code, with some spaces between them. Adding a comment at the beginning of each block of code also emphasizes the visual separation.
- Consistent naming scheme
Consistent naming scheme should be followed while coding. The variable names that will be used should have some relevance to the code that you are writing. This helps us in understanding the code better. Also, some language platforms tend to use a certain naming scheme. For instance, in Java, most codes use camelCase names, while in PHP, the majority use underscores. This should also be taken care of while coding.
- DRY Principle
DRY stands for Don’t Repeat Yourself.The DRY principle should be maintained in every code. The same piece of code should not be repeated over and over again. Instead of each one writing the piece of code again and again, the code that is common to all can be kept in a separate file so that, everyone can make use of the same code and code redundancy can be avoided. Headers and footers are usually best candidates for this. It’s not a good idea to keep copy pasting these headers and footers into every page. Instead maintain separate header and footer file and then call them in each code file.
- Avoid Deep Nesting
Many levels of nesting can make the code harder to read and follow. For the sake of readability, it is necessary to make changes in the code to reduce the level of nesting.
- Code Refactoring
Refactoring means making changes to the code without changing any of its functionality. You can think of it like a “clean up” for improving readability and quality. It doesn’t include bug fixes or any new functionality. You might refactor code that you have written the day before, while it’s still fresh in your mind, so that it is more readable and reusable when you may potentially look at it two months from now.
Have I missed out any important ones? If so, you can share more guidelines from your list of guidelines.
– Prachi Sinai Kavlekar