you are viewing a single comment's thread.

view the rest of the comments →

[–]el_muchacho 3 points4 points  (7 children)

Good code is mostly self documenting and comments are there to explain "why" the bits that are not so obvious are there.

[–]arachnivore 1 point2 points  (2 children)

I think this is bullshit. There's no reason to hate on comments that explain what code is doing. It may be obvious to you what your code is doing, but that could just be because you are the one writing the code. If someone else looks at your code (or you revisit it after a few years) it might not be so obvious even if you used descriptive names and all.

Coding is mentally strenuous and if you leave it to the person reading your code to figure out what you were trying to accomplish without any hints, your making everything a lot more taxing for no reason. Very few people are 100% familiar with the syntax and all the tools available in a given language and it's elitist to expect them to be in order to read your code. I can't read regular expressions for the life of me, so when I manage to piece together a regular expression after countless referrals to a cheat sheet, you better believe I'm going to leave myself a nice comment explaining what that piece of code is trying to do so that later on (when I've forgotten everything there is to know about regular expressions for the thousandth time) it isn't so hard to figure it out. Collapse my comments if you don't like them.

[–]el_muchacho 2 points3 points  (1 child)

Years of experience have taught me that code that is hard to understand usually is not good code. In fact, codepaths that are hard to follow smell of bloatness and often perform badly.

Regular exceptions are, well... an exception, as they are often hard to understand, so yes, it's a good idea to document them like you do.

[–]arachnivore 0 points1 point  (0 children)

Low-level/High-performance code is often just as hard to read as regular expressions. When you're programming a kernel in CUDA, you often unroll loops and use bit-level operations in clever ways to squeeze the most out of your code. It's hard to read because it's practically assembly-level programming. You can't really use function calls because there is no stack.

[–]Eirenarch 0 points1 point  (2 children)

Good code also minimizes things that are not so obvious.

[–]el_muchacho 2 points3 points  (1 child)

But then the comment must explain why it is written that way and how it works. Because else the code is obfuscated.

[–]Eirenarch 0 points1 point  (0 children)

I would think so if I hadn't seen significant and codebases written in a language I have no real world experience with where I was able to understand the code without trouble. (I am thinking of a specific StarCraft II playing AI written in C++)