Code should mostly be self-documenting in terms of the "how", and coding style, idioms, and guidelines should be enforced to ensure that the code actually achieves this goal. Importantly, a lot of implicit intent by the programmer can be made explicit through coding style alone. I prefer this method when possible because inferring programmer intent is one of the single biggest headaches for legacy code, and when intent is made explicit by the code itself you don't have to worry about the documentation diverging from the code nearly as much. I say "mostly self-documenting" because there are occasional cases where the "how" is not obvious even if the code is e.g. a specific result is guaranteed as a side effect of non-local state.
The "why", on the other hand needs significant documentation to exist inline with the code, and you typically need two types of "why"s. First, for some things you need to explain why a specific implementation was chosen versus other possible implementations. Sometimes an implementation is arbitrary -- and I document this too, so people know it can be swapped out with minimal consequence -- but other times it was chosen to effect specific optimizations based on specific assumptions, both of which need to be documented so that a programmer can determine if the assumptions are still true. The other type of "why" is, of course, why does a specific bit of code need to exist at all? I've seen extremely innocuous code that fixed a subtle bug or edge case that would almost certainly be refactored away by a programmer some day if it wasn't explicitly documented that a tiny bit of logic was doing something very important.
From an "80/20 Rule" perspective, I find that code styles the explicitly capture programmer intent, plus some well-written inline "why" documentation does most of the heavy lifting for code maintenance while minimizing the volume of documentation that has to be written and maintained by the programmers separate from code. There should also be some high-level documentation on design assumptions and scope, but programmers are less involved in that on a day to day basis.
Writing good comments requires a lot of vigilance: not only do you need to know which decisions to explain, but good documentation often entails explaining why you didn’t do something.
“We don’t need a memory barrier here because ____.”
“There is a subtle race between X and Y here, but it’s benign because ____.”
Of course, if your colleagues are not as disciplined as you, even the best comments can fall victim to comment rot over time.
- good commit messages
- references to persistent requirements artifacts (eg Trello cards, Jira tickets, whatever)
YMMV... but really doing these two things makes a huge difference.
So those things are both good also, but I consider them to be a fallback for cases where the code isn't commented like it should be.
Most of the comments I see are useless, outdated, or worse. Code, on the other hand, doesn't lie. Well commented code is nice, but there is no guarantee that comments match the code, it is better if code can be understood without the comments.
This is a common problem in companies with coding rules that require a minimum comment/code ratio. People will write comments for the sake of writing comments. There will probably be also formal specification and design documents, user manuals, etc... That's great on paper, and the dev doing that will probably answer correctly to your interview questions. However, chances are they are all full of lies. Because, doing all that and keeping it up to date is really time consuming, and there are deadlines to meet.
To put things simply, I've never worked on a well documented project. By well documented I mean that the documentation is better than grep. Some big open source project are good but I still sometimes end up with grep. The only part where people can be serious is in public API documentation.
a = 4 //assign value 4 to a
Of course that is exaggeration but usually they restate obvious things and think it is good comment.
Also I don't trust comments, I have to see commit history, and if I see there was piece of code changed where comments are, without stepping through and debugging I don't trust code is doing what comment is saying.
I do not know how other people are writing code, but I have to run code locally, ideally in unit test or click through in the interface with test data. Maybe others just can run code in their heads and account for what is written in comments. But unfortunately I am not human compiler/runtime/processor.
When I see people arguing against comments, I see this strawman again and again and again.
In my multiple decades of programming I have never seen anybody write a blatantly redundant comment like that. It's not "most of the time" that people comment like that, in my experience it's never.
Programmers have a job to do, and in my experience generally do not waste their time using comments to "restate obvious things and think it's a good comment". Rather, they comment when necessary to explain the why, or to provide a concise 2-sentence description that would be too long for a function name but lets you skip reading the next 30 lines of code which would take much longer.
I really wish people would stop using this strawman of "most comments are redundant/obvious anyways, therefore don't comment". I mean, maybe you personally did have the terrible luck to work with a codebase that was actually like that... but I personally have never seen it.
To be clear, I agree with you about how comments should be used.
I have.
That said, I think we are on the same position on the issue. When I've seen such comments, it's when someone that didn't understand the value if comments is responding to a request to ask them.
Thus is not necessarily because they are being deliberately obtuse, but because they never really experienced helpful comments. To me, 90% of the time a good comment wont say what's happening, instead it will say WHY it is happening. If good coding practised makes the code itself clear, good coding practice makes the context the code is written in clear.
But for those new, and/or those too use to a particular codebase or just never exposed to good comments, they wont get it, and when asked to do one (either as a general request or in a code review) the strawman is what you get (usually not as ridiculous as this example, but equally useless).
I see it as a cycle when learn to code: first we find comments useless so we skip them where we can. Then thus becomes normal, so we are used to seeing no or only useless comments. Then one day we look at code that we know we were confused on before but have forgotten why. On the N-th time of this, we leave the one line comment that says the secret. On the N plus one-th time, we read that comment and are enlightened. We then spend years trying to figure out the magic behind a useful and a useless comment, and arguing with those who think only useless comments exist.
Juniors think that they should write comments and then they waste time writing epic descriptions of trivial things. They waste time writing whole story of creating simple piece of code in one commit message. They waste time deliberating if tabs are better than spaces.
This job made me hate inline comments. If I were to ever create a new language, I'd insist in there being no way to write comments in the same line as executable code.
While I share your dislike for this counterargument, it's certainly not never for me. Not even close. In my last job, I was dealing with code that had been in development for 15-20 years. We started a project to clean up the codebase. I would randomly browse people's commits. A fair amount of cleanup involved removing exactly those types of comments.
>Programmers have a job to do, and in my experience generally do not waste their time using comments to "restate obvious things and think it's a good comment".
Try working on a code base developed by engineers who are not trained in programming.
You're simply insanely lucky!
Good code comments are important. Good unit tests are important too. But both are necessary.
That only works if the commit message is informative. A commit message that says "updated tests" is just as informative as a comment that says "assign value to a" (following your code comment example).
Commit messages are as, if not more important, than code comments (given VCS tools like git blame that can associate code changes with a comment explaining what was done and why).
As another commenter pointed out, we use comments for why. Sometimes the commit history can help this, but a well-placed comment can shrink that effort.
I get a feeling we may not pass OP’s litmus test, but I’d be curious if we would. We use the term self-documenting code, but that’s primarily because our tests are part of our code, and those tests are indeed our primary documentation.
It’s also not about whether your code “needs” documentation or comments. The code should be clean and no more complex than required, always, and refactoring is to help do that over time.
That has almost nothing to do with providing comments or documentation for future people who could be inexperienced, unfamiliar with that programming language, unfamiliar with that type of algorithm, coming to the source file many years after the last time it was edited, etc.
The reasons to need documentation are many, and even if the code “is so good it doesn’t need comments” that usually does not matter at all to the person reading the code, and that person still does need comments.
Thinking of comments as an indicator of inferior code is a dangerous code golf mentality that ignores the fact that you write comments _for people_, especially people coming to the code in a way that breaks all the assumptions the code author was thinking when they decided not to write comments.
Any place that takes an attitude of "code should be self-documenting" I just walk away from. That is a type of dysfunction you're not gonna be able to solve. Also places that say, "we're always evaluating new ways to improve documentation practices..." begs further questions to get the specifics out of them.
For most jobs, I know I'm going to have to deal with bad legacy code. But if there is also a culture of not documenting things, arguing against comments or formatted API and function documentation, and it results in documentation spaghetti of Google docs, Markdown files, spreadsheets, emails, confluence pages, etc., then there's no point. I can't help you.