Comments Are Code (2018)
responsibleautomation.wordpress.com
responsibleautomation.wordpress.com
And I’ll push back against absolutist arguments about never writing about the “what” too.
So much moaning and whining about the possibility that the comment might get out of sync with the code. Oooh so scary.
Lazy reviewers are the real problem there.
At this point I’m almost a specialist in doing deep dives and deciphering what spaghetti code is actually doing and why. And I make damn sure to write comments to help out the next poor soul.
This may just be me but does anyone else actually enjoy documenting their code?
There's some excitment in identifying those, and that's where I enjoy documenting the intention of my code.
this code is done in this way because EU regulation #1374 regarding Maritime tracing.
this code seems inefficient and silly but is that way to get around a specific rendering issue in Opera Mini which needs to be supported because our project needs to support basically everything because it is a required governmental service.
This code is dealing with an issue in Safari that is supposed to be fixed by June 2024 release - please check if it is still an issue - test by doing the following...
Tldr: No, you can not always make your code self-explanatory.
I don't always read comments. But when I do, it's after reading the code.
The checklist includes:
1 are you using comments to manage obfuscated code? I.e. the comment isn't the problem it's the spaghetti code it makes the person feel entitled to write after
2 can the code just be a function with a clear name
3. Is the comment a non comment ?
<some others I forget the book is a great read>
That said. Plenty of times you need a good comment.
No more wishy-washy than the concept of a comment explaining code.
Problem solved, apparently.
During code review, someone asked, why are you doing X on line 17?
Typically, you are explaining statements with a comment. This is code which becomes self-documenting once you turn it into a method with a good name. Every tricky bit can now be documented via JavaDocs as well (and other language equivalents).
This also tends to avoid having 200+ line methods as well :D
But commit history gets messy quick...I have food for thought.
Leave the history alone for us bug spelunkers
Before, for the last couple decades of my career as a programmer, I thought that it was important to have well commented code. I don't believe that anymore. I'm fine with no comments in the code.
If you would have told me that a few years ago, I would have told you you were crazy. But after trying it, it's fine. It works. At their best, comments in code are wasted effort. That's my opinion now after 4 years of working on a code base with zero comments.
Surely the "why" for anything non-obvious should be written down just as much as the code shows the "what"?
Talking about comments as "wasted effort" sounds like you view them just as something added at the end to please a manager, rather a tool to help you write correct code in the first place.
No, I've just worked for a long time where comments were required, and there was an effort in code reviews to make sure the comments were detailed and correct, and I've worked in a zero comment code base, and I don't see any difference. They just don't add value in my limited experience with one code base without comments for the last four years and with several code bases with comments for the 20 years before that.
It was kind of surprising to me, but that's how it turned out.
To be fair, the commits are associated with a JIRA ticket, and I can go that to figure out the "why". But I do that less than I would have expected also.
> Yes, it’s generally better for code to be self-documenting. In lieu of being dumb for dumb’s sake, however, perhaps add some code comments to provide context for others…or perhaps your future self.
When writing comments, dont say what it does, say why its done that way. If the why doesnt need explination then it doesnt need comments.
Programming is theory building. Comments should only help you understand the theory not the source code. The source code is meant to help you know what the computer will do without interpretation. If you cant read the source code and know what it will do then you NEED to rewrite it. Otherwise just wtite it in assembly it will at least be faster that way.
Also dont include things that are learnable outside the context of your code. Dont explain what a state machine is in your comments. If you must, link to wikipedia or something.
But yes, generally the text you write in a comment isn't contemplated by the compiler.
Well, after a few decades writing software, I've come to the conclusion that yes, yes it is. But just because it can be hard to write a good comment it doesn't mean you need to agonise over it.
Here are a few "simple" examples:
// Once we receive the cancellation ack, we should automatically send the updated flow
if (releasedFlow.Status == ReleasedFlowStatus.IndicativeCancelled)
{
await _mediator.Send(new SendFlowCommand ...
It should be pretty clear from the context that that's what is happening and it doesn't explain why. This is because it would take multiple paragraphs to explain. Could I just post a link to the documentation about this business rule? Yes, but the location of the said documentation changes so often it can render the comment useless. Could I change it to say something like "Please refer to the documentation"? Sure, but then why don't I put that comment behind every piece of business logic? No, the simple purpose of this comment is just that - a comment on what the business rule was at the time of writing, especially with respect to the other possibilities. You can spend ages overthinking this but there's no point. Just read, understand and move on.What about this:
if (foo)
{
DoFoo();
}
else if (bar)
{
DoBar();
}
else
{
// Intentionally empty
}
Dead code with a comment that explains nothing. Is this code better with or without the dead code and comment? I'd argue that the code is better with it. In fact, this is a well-known technique: https://en.wikipedia.org/wiki/Intentionally_blank_pageYes, you can argue that the coder should explain why it's blank. But this depends on the context, and who the intended audience is. Again, no need to overthink it. Read it, modify it if you really think it's necessary (keeping in mind that the alternative is often no comment or empty else statement), and move on with your life.
It's unpopular because it is astonishingly wrong. Customer: "It does not work". Me: "The comments are correct, the code is not, let's call it a 50/50".
> code comments are critical to code readability and maintainability
Fine, but we are on another level: once the code is right, maintainability becomes important. As long as the code is wrong, fix it and just pray that your code survives long enough for another soul to get a look at it!
I’ve learned to unwind the typical spaghetti that passes for code, these days, but every time I do, it reinforces my own posture of leaving a legacy.
Here’s my take on code documentation: https://littlegreenviper.com/miscellany/leaving-a-legacy/
> But wait! The comments will get out of sync with the code! Well, they shouldn’t. When we perform our code reviews, we need to review the comments as well as the code. Comments should be correct, understandable, and valuable; we need to keep these points in mind during our reviews.
I'm not against such "why" comments, but if I really want to make sure such assumptions about code are preserved I strongly prefer unit tests. They don't get out of date accidentally.
> code comments are critical to code readability and maintainability
Fine, but we are on another level: once the code is right, maintainability becomes important. As long as the code is wrong, fix it and just pray that your code survives long enough for another soul to get a look at it!
https://hexdocs.pm/elixir/1.16.0/docs-tests-and-with.html#do...
A simple example is `Foo * foo`
That could be an input pointer, out parameter, array or it might be allowed to be null.
In zig we can express each one distinctly and all combinations.
The "why" vs "what" advice is the universal advice everyone has been giving for the last two decades.
```
# 1.1 CSV reading
# read CSV onto a pandas dataframe
df = pd.read_csv("somefile.CSV")
```
I really wish I was joking. . . .