Practices to Follow while writing Java Code Comments

--

Code comments in java

The primary purpose of comments is to make the code understandable.The Java comments are the statements that are not executed by the compiler and interpreter.

Practices to Follow while writing Java Code Comments

1) Focus on readability of code; assume that you don’t have comments to explain the code. Give your method, variables and class meaningful name.

2) Don’t write what code is doing, this should be left for the code to explain and can be easily done by giving class, variable and method meaningful name. For example:

//calculates square root of given number
//using Newton-Raphson method
public void abc(int a){
r = a / 2;
while ( abs( r - (a/r) ) > t ) {
r = 0.5 * ( r + (a/r) );
}
System.out.println( "r = " + r );
}

Above code is calculating square root using Newton-Raphson method and instead of writing comment you can just rename your method and variable as follows:

public void squareRoot(int num){
root = num/ 2;
while ( abs(root - (num/ root) ) > t ) {
r = 0.5 * (root + (num/ root));
}
System.out.println( " root = " + root );
}

3) Always write why you are writing this piece of code, why you are writing this piece of code because this information is not visible until you write them in comments and this is critical to identify any bug or behavior with changing business environment.

4) If you are writing core libraries which will be used by different project and with different teams. Follow javadoc comment style and document all assumption and precondition for using your API. Joshua Bloch has also mentioned about writing Java-doc comment in his classic Effective Java, which is worth knowing.

5) Include JIRA Number and description on comment, especially if you are modifying an existing piece of code as part of maintenance. This I found extremely useful while comparing different version of code in CVS or SVN. This gives you clear idea why that particular code has been added and whether issue is because of that piece of code or not.

6) Always try to finish your comment in as few words as possible, one liner comment is best until its explaining “Why” part and can’t be replaced by code itself. No body likes or has enough time to read longer comment.

7) Don’t write story in comment as your name, employee id, your department etc because those information can be obtained from CVS commit data in case someone wants to know who has make this change.

8) Always put comment while committing code in source control repository and especially why you are adding this piece of code if possible include JIRA or QC Number so that any one can refer JIRA for complete details.

9) If you want upcoming developer to follow certain standards or inform about certain things then include them in the beginning of your class as comment. E.g. suppose if you are writing serializable class in java then its good to put a serializable alert stating that any new fields addition in this class must implement serializable interface in java or make it transient etc.

10) Last but not the least give your code to fellow developer to understand as part of code review and ask him how much he understands it.

That’s all from me on code commenting, please share the standard, best practices or your experience with writing comments on code. I believe these are the areas which a developer can improve and it’s only possible from learning which each other’s experience.

References:

Happy Coding…

--

--

A Passionate Programmer - A Technology Enthusiast
A Passionate Programmer - A Technology Enthusiast

Written by A Passionate Programmer - A Technology Enthusiast

An Architect practicing Architecture, Design,Coding in Java,JEE,Spring,SpringBoot,Microservices,Apis,Reactive,Oracle,Mongo,GCP,AWS,Kafka,PubSub,DevOps,CI-CD,DSA

No responses yet