by tines · 2y ago · 0 comments
> Well written ruby code can often read like natural language. Features like predicate methods even give up punctuation. [...] This is often why ruby programmers don’t like comments. In most cases, the language makes comments unnecessary.
churns my stomach. Good comments are rarely about saying what the code does, they're about explaining _why_ the code does what it does. Regardless of how comprehensible ruby code is, it doesn't change the need for comments. If I had a nickel for every time I gave a commit a -1 review because it had too few comments or the wrong kind of comments or lacked a "why" commit message, I'd be a rich man.
"Why" comments need to go onto code that make some kind of arbitrary decision. Or code which is integrated with processes that are happening elsewhere and has to behave in a way that complements those behaviors. Otherwise the reader would have to read that code together with all those other pieces (or specifications) in order to understand it. Code that is written in a certain way (particularly, an unusual way) for some reason that is not obvious deserves a "why" comment. E.g. "due to a compiler/library bug [give specific version info], we cannot just do this: ...".
Code that doesn't make arbitrary decisions or integrate with things elsewhere, or make unusual coding decision for reasons, often doesn't require "why" comments.
The author of the article hastened to add:
> And when you do need [comments], it’s often when you’re doing a very specific or obscure thing that requires context to understand. It’s clear from the code why you need a comment in that case.
It's clear from the code why you need a comment, because it's a "why" comment.
Commit messages are such a good place to explain code and I rarely need the same explanation in a comment. I worry though about the fragility of the connection between (a) the source code and (b) the important commits that explain why the code is there.
Perhaps there are some very good UIs out there already that show something a little more intelligent than than the most recent commit from blame?
Another idea might be to attach blame-like functionality to the syntax tree so that the explanatory messaging can be tracked at the function level, rather than as lines of source code. Can others here can point me in the right direction for either a more tangible description of this feature, or an existing research?
Why does this function exist? Why was it implemented this way and not that way? Sometimes these are really important bits of info. Here's where this approach breaks down. Here's a sharp edge that we hope to remove someday. This part here incurs some technical debt but this is why we did it.
In order to really maintain a codebase (meaning: safely fix bugs and add new features), you need to have an understanding that at least approaches that of the person who wrote the code originally, and good comments (e.g. level of intent comments) can help you do that. This is true even for your own code that you're returning to after some amount of time.
Comments are not checked by compilers, linters, code formatters, unit tests, integration tests, or any other automated tools for correctness, consistency, and good formatting. They rot quickly.
For example, if code is operating on a list, and then it sorts that list and passes it along to some other code, I can see perfectly that the list will be sorted. But that doesn't tell me _why_ it needed to be sorted in the first place. Often that is not knowable except by reviewing things outside the scope of the code in question, or outside any code at all. A comment is essential in that context.
In a different language you might deal with more boilerplate (getting some data access object for feature status, passing the db connection, extracting the subscription id, etc), or less clear naming schemes (importing namespaces, aliasing them for convenience, your subscription might come from some one letter temp variable), or something else. At this point the “why” comment might be not that different from the actual ruby code.
Note that we only see one line of ruby code; the parent method name, or the if body would have more context.
Then again, ruby and rails make it especially easy; perhaps the amount of conventions helps. Other things, like the ‘?’, clean syntax and focus on structure (OO), help too.
This only makes sense if we're in the school of thought that writes
i = i + 1 // increment i by one
The more charitable interpretation is that they're pointing out the developer ergonomics of things like predicate methods remove the need for comments that might otherwise be reasonable to insert. Whether or not that is true is subject to reasonable debate, but it's definitely a more nuanced opinion than "Ruby's predicate idiom means I don't have to document my obvious code."
If I had to guess, the anti-comment attitude comes from consultants who re-implement the same CRUD webapp on Rails over and over. Sure - no surprises, no comments, fine.
Hence, lots of Ruby code is a sequence of trivial declarative blocks that look like natural language.
In these parts of the code, comments are redundant. I think that is the OP's point. Other parts definitely need them.
I say this as someone who wrote Ruby for 6 years and loves the language for its other features (e.g. everything is an object, functional programming, DSL-capabilities)
Consider the overhead of a comment - first you need to read the comment because they are always above the code so that's what your brain will do. This will prejudice your reading of the code.
Then you need to read the code while trying to block the comment prejudice out of your mind.
Then you need to sort of mentally diff the comment and the code, and if the comment is wrong (spoiler...) decide if you are going to delete the comment (the best choice, unless you get a code reviewer who likes comments) or fix the comment (the worst choice, a waste of time, but maybe the only way to get it through code review).
Now consider code without a comment: you just read the code.
Sometimes you just need comments.
Is mathematic better when it reads like English? I personally don't think so. Yeah it's a bunch of symbols and conventions to learn but it ends up being more precise.
I dont have anything to back it up :) I just think that natural language is almost by design messy and imprecise so I'm not really looking for that in my code.
What's even better than comments, is a compiler that actually guarantees you're gonna be given something of the type you expect.
I would even go a step further : I prefer a cryptic code with good comment explaining what the code does and why it does what it's trying to do vs a very clear code with comment on How the code works.