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
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

Thoughts on Overusing Code Comments

If you spend time updating comments after you update code, then sure, comments are great. But what is code if not the most detailed specification of your requirements?

Maciej Piekarski user avatar by
Maciej Piekarski
·
Jan. 11, 17 · Opinion
Like (5)
Save
Tweet
Share
3.06K Views

Join the DZone community and get the full member experience.

Join For Free

This article is my response to another DZone author's article, "Comment Your F**king Code."

Sometimes you see an article with the number of f***s so high that it actually makes you give one and spend twenty minutes of your time to respond — especially when you don't agree.

Insurance Company Revisited

Imagine you are looking for insurance to save your family from living on the streets in case you get hit by a truck. You decided on a company and requested an offer. What you received is a thousand-page long document written in legal English mixed with Latin with some comments in plain English added on the margins. To your surprise, on the last page, just below the salesman's signature there are three sentences written in small print:

"The understandable comments added for clarification may or may not be up-to-date with the Terms and Conditions. They also may or may not be highlighting the most crucial parts of the contract. The terms you are agreeing to are stated in legal English; the comments are not a base for claims."

Damn, you think to yourself, I better talk to my lawyer to make sure I know what I'm signing. Looks like the good old times when the contract was understandable to the normal folk are gone. It was so nice to get the separate list of use cases along with it!

Tests

So, let's get back to code. What about it, you say? It's here; all the logic you need is in a bunch of methods, tangled in nested for loops with multiple return statements.

And no tests. You're kidding, right? What am I supposed to do, go through all of it, compile it in my head, and figure out the result? Give me a break and write some tests in the meantime, and name them so that I know what's going on. No, "YEqualsFiveOk" is not ok. And those nulls you want to return...yeah, not that difficult to test that.

Thread safety you say...make your objects immutable and they'll be thread safe.

Names

As expressive as I managed to make them, they won't save the day. Why should they? They just provide you with more information. Yeah, I know, int i is always an iterator in your code, x is always the number of cents, and String n is a name. Hey man! Great job, you're unfireable now!

About those bullet points...

  • "What are the preconditions, especially regarding the arguments I’m passing in?" — They can be nicely described in a test.
  • "What promises are made regarding the return value?" — Tests?

  • "What units are used for arguments and return value?" — Integer bribeInDollars not clear enough? Then you can create a class Dollars and have Dollars bribe.

  • "Under what conditions do exceptions get thrown?" — Tests?

True, when you see an interface called InterfaceA with methods called doA, doZ, and implementations called InterfaceAImpl, DefaultInterfaceA, and SpecialInterfaceAImpl, then making a guess about what your implementations should do might be tricky. So, you wrote a nice documentation and your colleague opened it, printed all 500 pages, and used it to start a fire in a fireplace. Because everyone knows that sh*tstorms under articles like the one I'm referring to taste better when enjoyed with a cup of tea, a cigar, and a crackling fireplace.

After taking a pleasant break, said colleague might go back to his laptop and implement all the methods to return some random stuff, "forget" to update the docs and go home. And good luck figuring out five months later if doZ in SomeImpl was really meant to return rand.

Aging Comments

If you spend your time on updating the comments after you updated the code, then sure, comments are great. But what is code? Isn't it the most detailed specification of your requirements?

I am not saying that there is no place for comments at all. Feel free to comment unusual configuration of the framework you're using — complex algorithms, etc. However, every time you think a comment is necessary, think again and ask a colleague to help you simplify the implementation. It's likely you are overengineering it.

code style

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Develop a Portrait Retouching Function
  • Why Does DevOps Recommend Shift-Left Testing Principles?
  • The Future of Cloud Engineering Evolves
  • Unleashing the Power of JavaScript Modules: A Beginner’s Guide

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: