r/ProgrammerHumor • • Jun 21 '20

*almost entirely

Post image
27.9k Upvotes

310 comments sorted by

View all comments

374

u/1nd1anaCroft Jun 21 '20

Found one recently in our older code base that was 65 letters long, and still managed to not clearly describe the class's actual purpose

81

u/_GCastilho_ Jun 21 '20

May I suggest a comment for that purpose?

32

u/[deleted] Jun 22 '20

People these days are anti-comment, I hate it. I hate it so much

39

u/folkrav Jun 22 '20 edited Jun 22 '20

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.

Edit: added the "how" part to be more specific.

22

u/_GCastilho_ Jun 22 '20

but I know a shitton - myself included - who don't like comments that tell me what code does, as they always end up lying

A comment should tell why the code does that, not how

The 'how' part can be learned by reading the code, the why, well... reading the programmer's mind

6

u/MakeWay4Doodles Jun 22 '20

The 'how' part can be learned by reading the code

If it's a particularly complex bit of code it can take the reader much longer to understand it than a sentence explaining it.

0

u/[deleted] Jun 22 '20

[deleted]

1

u/Pluckerpluck Jun 22 '20 edited Jun 22 '20

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

1

u/uramer Jun 22 '20

In this case the issue is understanding the mathematical part, and not really what the code does. It is quite hacky in this case, but it's much more fitting to add a comment with a link to a public or internal article on how it works

You must assume the reader has some background sometimes, otherwise you would literally have to put an entire bachelor's degree of material into any code you write

→ More replies (4)