Thoughts on Overusing Code Comments
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?
Join the DZone community and get the full member experience.Join For Free
Discover how you can take agile development to the next level with low-code.
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!
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.
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.
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.
Opinions expressed by DZone contributors are their own.