by huggyface · 14y ago · 0 comments
If the code isn't the clearest comment you have, you're doing it wrong.
Comments are a crutch for people who write poor code. Comments have zero authority or guarantee of accuracy, and more often than not have little correlation with the actual code.
Code is canonical. Comments are noise.
Business logic is complicated and rarely defined by a developer but by a product manager. Often you can understand what's being done in the code, but the WHY is necessary to understand why it's there and what it is trying to do. I believe giving a brief synopsis of the business logic in a method comment and, if it's not super straightforward, a brief overview of the steps or algorithm, is incredibly useful.
I guarantee you won't be able to figure out what your program is doing by looking through years old wikis left by product managers no longer at your company.
That the code did X is clear by the code: No amount of words can refute or change that the code does X. The danger is replacing code is always in the behavior of the code, and never simplified descriptions of its actions.
To business logic, as someone who has worked heavily with business code for years (laughable commentary from imbeciles to the contrary), business logic in comments is one of the worst choices a team can make because it is an escape hatch. It negates the need for verbose, traceable code. It negates the need for vastly superior external proof.
Donald Knuth, who gives money to people who spot bugs in his code, even invented literate programming to mix prose explanation and code better. Is he an idiot?
They are the crutch of people who can't read code: "Add more comments because otherwise I can't make sense of what the statements are doing." It is the English speaker demanding that every French passage have an English translation, rather than simply learning French.
They are the crutch of people who can't write code. "My code is a gigantic, illiterate mess, so instead read the comment at the top that has no guarantee of being robust or accurate."
Bringing up mathematicians and Knuth are both irrelevant distractions. Software development in the modern world is a very structure, self-describing affair, or at least it should be. Comments are the short-circuit from having to figure out how to do that.
As has been said, comments explain why you are doing something in a certain way. That why is often related to a business process, several business process, and/or 25 different outside cases.
The code can be amazingly clean and organized, but you comment to indicate why you did something one way and not another.
Sure you are. And that's okay.
I mean, I can write self documenting code without any comments, and it's perfectly understandable.
before_create :auto_increment
def auto_increment
self.count + 1
end
That code fails to tell the programmer why it's in the application logic and not defined in the database schema. What should I do instead, so I can code without crutches? def auto_increment_natural_key
self.count + 1
end
That's not a great deal better, it's still just saying what it does, not why it's there in the first place. def auto_increment_natural_key_because_another_app_relies_on_it
self.count + 1
end
Is that it?I would argue that in this case, it is far better to just understand the code and then fix it appropriately. A comment would leave you second guessing.
def auto_increment
self.count += 1
end
(though, for the purposes of self documenting code, count probably is not a great variable name either)Now we just put the comment in the variable name. The compiler just checks the variable names all match up, but doesn't check whether their names make sense.
Perhaps the example chosen was too much of a toy to yield valuable insight?
Why isn't it a GUID? Why is it 32 bits instead of 64? Why is it signed yet starts at 1? Why isn't it a string? How will the identities be merged?
The notion that answering one single question provides clarity is ridiculous.
Pretty sure that's it.
I'm not trying to be an a$$hole btw. I am not a coding guru, I genuinely want to minimize the need for comments in my code and am willing to learn from examples.
Read almost any non-trivial successful project for good examples. The Linux kernel. Firefox. etc. The frequency and verbosity of comments tends to have a direct correlation with the simplicity of the code (which is the exact opposite of normal expectations).
You seem to have lots of experience with expressing intent in code, and making that intent 'canonical'. How do you make the machine check the accuracy of your code? What language are you using for that?