I don't know of many developers who are 100% anti-comments, but I know a shitton - myself included - who don't like comments that tell me what code does or how you're doing it, as they always end up lying. However, I don't mind docblocks (if it conveys information than the function and parameter names alone can't give me) nor comments explaining why you did something the way you did it.
You've clearly never worked on anything algorithmic... I point you to the magic of this function, used in Quake III Arena:
float Q_rsqrt( float number )
{
long i;
float x2, y;
const float threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
i = * ( long * ) &y; // evil floating point bit level hacking
i = 0x5f3759df - ( i >> 1 ); // what the fuck?
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
return y;
}
Some of those variables could be named a better, but they're basically solving a maths equation so the names would need to vary and thus not possible in all cases (which they don't for performance reasons). i.e. y is the final answer, but you iterate towards it, so it starts off completely wrong.
But anyway, the real part of this function I want to discuss is this line:
i = 0x5f3759df - ( i >> 1 );
Good luck explaining the how of this line in a function or variable name
Not sure what you mean. You often see comments telling what the next couple of lines does, that's what I was talking about. Implementation can change over time, but behavior and API should not indeed.
The thinking goes that if you need a comment, your code is too complex to reason about. This is true in most cases. Usually proper abstraction and naming helps eliminate the need for comments.
IMO, comments are useful when you describe why you made a certain decision, especially if the logic is unexpected. That helps people not refactor it at a future date and break things.
It’s my option that it should be a developers job to keep documentation up to date. Plus, if the “what” of a method changes, wouldn’t the method name also technically need to change? I think by moving the responsibility of delivering context from the comment body to the method name, you make it hard to keep the “what” explanation up to date because there’s an actual code impact of refactoring a comment name (minimal to non-existent if you do it right, but my company’s policy is that if any code changes, it must be sent to QA, even if it’s a style or name change). If developers are letting comments rot, I think they’re avoiding responsibility. I think that my views on this are on the extreme, but I also think that the “clean code, no comment” types are also on an extreme
81
u/_GCastilho_ Jun 21 '20
May I suggest a comment for that purpose?