DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

Comment Rate in Applications: The Higher the Better?

Armel Gouriou user avatar by
Armel Gouriou
·
Sep. 19, 12 · Interview
Like (0)
Save
Tweet
Share
3.88K Views

Join the DZone community and get the full member experience.

Join For Free

In various audit and quality tools you can find the metric “Comment rate”. What does it mean? How is it related to the application’s quality? How does it impact your technical debt? I am going to try to give you insights on this metric.

Definition

For a given application, the comment rate is the percentage of lines that are comments. With N being the number of lines of code and NC the number of lines of comments it is defined as :

Comment rate = NC / (NC + N) * 100

This metric must be considered with care, since a large amount of comments is not the guarantee of good comment quality. However, it gives a hint on the maintainability of the application and there are very few applications that have good reasons to be below 25%. On the opposite an application with a comment rate above 40% probably contains a great number of irrelevant or useless comments. Once again, those are not magic numbers, but they come from several hundreds of performed audits. More important than those numbers, I would like to give you hints on what is and what is not a good comment.

What you should comment

First of all, any public API and all its methods should be commented (getters and setters are the only acceptable exceptions). This is the strict minimum for any application since those comments are meant for the user of your API. Obviously it should not have to rely only on a method’s name to understand its behavior and its parameters. Furthermore most of current IDE are able to automatically generate comments, leaving you only the task to add relevant information.

Another good kind of comment is to explain something which cannot be determined just from the code. For example you may want to declare a synchronized collection in a place of the code where it is needed, but not obvious.

//Synchronized map needed: used by several producer threads latter on
Map library = new HashTable();

This is an example of good comment: the developer motivates an implementation choice or the use of a particular algorithm required by the situation.

 

The pitfalls of comments

A good comment rate is not enough to ensure a good maintainability of your application! I once audited an application with a very good comment rate, something above 50%. All methods where documented, each action was commented, every parameter was described. And all business code was in three classes of 3000 to 7000 lines of code… Thoroughly commenting your code will not make it automatically easy to maintain! Moreover, an overload of comments introduces noise and hides the really useful ones.

Before commenting a line of code there are at least two questions you should ask yourself. Can I express the meaning of this only with code? Check out the following example. In the original version the line is quite long and the author felt the need to comment it. In the refactored version, intermediate operations and adequate naming make the code self-explanatory.


//checks if classification of the referential contains the category of this rule
if(referential.getClassification().getCategories().contains(rule.getInformations().getCategory())

 

Final List<Category> classifCategories = referential.getClassification().getCategories();
Final Category ruleCateg = rule.getInformations().getCategory());
if(classifCategories.contains(ruleCateg))

 

Do I comment it because it is too complicated? If yes, then try to simplify your code! Good commenting is not a valid excuse to write hard to understand, over complicated code. This is well expressed by Kernighan and Plaugher:

Don’t comment bad code—rewrite it.

 

Of course there are many more kinds of correct and bad comments. I will not make an exhaustive list here, which is quite impossible by the way! An important thing to remember about comments is that a good usage of them will improve your code quality. However, irrelevant or abusive usage will increase your technical debt. I hope this article gave you insight on comments in applications and that you will have a thought for it the next time you will be coding.

Now tell me, what is your politic regarding comments? Do you follow specific rules when commenting?

code style application

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 10 Most Popular Frameworks for Building RESTful APIs
  • Data Stream Using Apache Kafka and Camel Application
  • Scaling Your Testing Efforts With Cloud-Based Testing Tools
  • Spring Boot vs Eclipse MicroProfile: Resident Set Size (RSS) and Time to First Request (TFR) Comparative

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: