Live data from Hacker News

Don't waste your time commenting source code

riyadsthoughts.blogspot.com

11–20 of 25 posts

Re: Don't waste your time commenting source code

#11
post #3

This argument is old and tired. Programming languages are rarely sufficiently expressive as to document the nuances, guarantees, and expectations of a particular block of code in the code itself. Type systems can help here, but they alone are not sufficient. An often stated rebuttal to the above is that the developer can comment the "complicated bits", and save time by skipping it the rest of the time. This is a flaw…

I'm not sure I agree with all of that, especially the second paragraph. In my experience, if your comments are English translations of code, you could probably be writing the code more clearly. Exceptions are when you're using a particularly hairy API, writing in Perl, building complex regular expressions, or need to document side effects that are not otherwise clear.

Additionally, commenting every bit of the code, especially as you go along, leaves one prone to forgetting the most important reason to comment: explaining the rationale for what you're doing. Explaining the design decisions in comments (and especially why you didn't do the alternative) is invaluable, and much more accessible to a maintainer (such as oneself a few months later) than an external design document.

Re: Don't waste your time commenting source code

#12
post #2

Comment the why, not the what.

Exactly. There isn't a programming language in the world, and never will be, that can explain the WHY of the code being written. I write tons of comments explaining WHY I'm doing things so that 1) I can understand what I did when I come back to the code and 2) Other people can understand what I was thinking and how the code fits into the greater whole. I HATE code with no comments. And tests are not sufficient docume…

I've worked with a lot of legacy code over the years. I don't usually care whether code has comments or not. Comments are a very poor way of expressing semantics in code.

Usually, I'm looking for general readability and understandability. I'd much rather see short, well-named classes and methods, well-named variables and good unit tests before I see a single comment.

In the absence of clean, readable code, comments are probably a reasonable but nevertheless inferior resort. I also agree that, sometimes, they 'why' of code is often best expressed as comments.

Re: Don't waste your time commenting source code

#13
post #8

I find that when you are developing an open source application, its rude to not comment your code. Its the least you can do to help any other developers willing to work on your project. I feel like its more inviting as well.

I'd suggest that you help your fellow developers more by writing code that needs fewer comments to make it understandable.

However, I agree that what you say is certainly true of APIs.

Re: Don't waste your time commenting source code

#14
Code should be beautiful. It should be expressive. It should be concise and speak for itself. It should be both clear and obvious on the first glance. A variable should be called what it is, not x or y. Once you've written a piece of code, re-write it. Again and again, until it doesn't need comments and speaks for itself. Once you've re-factored your code enough times and you feel it can't get any better, then you can comment your code.

Many times developers write shitty code, put a stamp of "it works" on it and then comment it, as if comments make it OK. Bullshit. Perfect code doesn't need comments. We can't achieve perfection, but we can try.

Re: Don't waste your time commenting source code

#15
I wish I could get to this point where I work, but his article presupposes a couple things:

* You have tests.

* People write code to be readable.

Where I work, neither is the case. I try to write tests[1] for my stuff, and I try to write clearly. But I have to comment what I'm doing since the libraries and APIs I'm calling are so baroque and almost deliberately obfuscated, that I have to comment what's going on if for no other reason than self defense.

[1] As a point of reference, in my current project we have something like 6 - 8 developers. Of the unit tests that exist, I've written 85% of them, and I'm embarrassed at how few I've written.

Re: Don't waste your time commenting source code

#16
Every time this subject comes up, a pack of vociferous compulsory commenters come out and start shouting about how desperately unprofessional it is not to write comments.

I imagine, also, that a fair collection of idiots also start using it as an excuse not to bother commenting their illegible code, but they seem to keep quiet about it.

The author points out that "Sometimes you just need to leave a note", but that doesn't stop people coming out and declaring it "the most retarded article ever". These are people who can't tell the difference between "Never write comments" and "Write clean code to avoid the need for comments".

Even as I write this, I imagine some of them bursting a blood vessel because "You can't always avoid comments".

I agree, you can't. If (for example) you are authoring some workaround for a counterintuitive 3rd party API, then yes, good commentary is important.

However, if you write short, well-named, DRY, SRP-obeying units, with well named parameters and variables, and well-named, clear tests, then many of the comments that you find in poorly written code simply aren't needed any more, because they are there in the code itself.

Re: Don't waste your time commenting source code

#17
post #9

"A well-designed program uses classes and design patterns" Ha ha ... also don't waste your time asking me for a job.

The only zealot as bad as the patterns zealot is the anti-(patterns zealot). Which is of course, different from the (anti-patterns) zealot.

Re: Don't waste your time commenting source code

#19
I wouldn't work with a programmer who believed so strongly, against, commenting source code. In the long run, it probably saves more time than it "wastes". If someone develops proper habits writing sufficient comments, then writing them becomes less of a hassle, and more of a better business practice.

Let's not forget that writing comments and notes is not unique to computer programming. Most professions, law, medicine, must create notes, and comments to understand the work at hand. Those who don't, or refuse are looked at as lazy and unprofessional.

I don't want notes for every single line of code, but for sections of complicated code comments are a must for ease debugging, or future modifications.

Post reply on HN