• (cs)

    At least he didn't comment every line.   I don't waste near as much time on that comment as I would on the following that appearently many stupid teachers require.  (Smart teachers will kill you for commenting every line)

    <font>// if hasSiblings, actionType == cont; else actionType == end.</font>
    <font>if</font> ($hasSiblings)       // check if there hasSiblings is set
    { // start block for if hasSiblings is set
    $actionType = <font>"cont"</font>; // set actionType to the string "cont"
    } // end block for case where hasSibings is Set
    <font>else</font> // there is an else case
    { // start block for case where hasSiblines is not set
    $actionType = <font>"end"</font>; // set actionType to the string "end"
    } // end else case for when hasSiblings is not set
    The stupid teacher would of course deduct points for mispelling hasSiblings in one of those comments.

  • trojan_mind (unregistered)

    I don't think this is that funny, really.  You can criticize code for using more lines that what it could but that is being extremely geeky and arrogant.  As for the function’s name… well its kind of long right; too long probably.   But if you find that to be really humorous and reply sarcastically you really need to find some next hobbies or ways of investing your time.

  • (cs) in reply to trojan_mind

    We don't actually know the function's name - so it's not that that is being made fun of.

    What you seem to have failed to notice is that the sole comment is utterly pointless, since all it does is to restate the code itself. It's this pointlessness that should be, and is being, mocked.

    The imagined function name was Alex taking the self-documenting code idea and guessing what this programmer's response would have been. Since for this programmer commenting means replaying the code, surely the function name should do too.

  • ZeoS (unregistered) in reply to UncleMidriff
    UncleMidriff:
    dpm:
    ZeoS:
    dpm:
    ZeoS:
      that code will be readable for any programmer...

    I don't want my code to be maintainable by "any" programmer.  {...}

    so... easy to read means dumb to you? 


    You said "readable by any programmer".  Now you say "easy to read".
    Try staying in place when you're making an argument, eh?

    ok
    dpm



    I would think that if X is "easy to read," and since that phrase doesn't specify or exclude any particular group for whom X is easy to read, then X would be "easy to read" by "any programmer."  In other words, if X is "easy to read," that includes X being "readable by any programmer."  "Easy to read" seems to be more general than "readable by any programmer" to me.  Therefore, even if we can't say that ZeoS has stayed in one place while making his argument, all we can say is that he's moved to a more extreme version of his previous argument.  He hasn't moved in closer to your position in an attempt to make his original argument seem more reasonable, which is what, it appears to me, you're accusing him of doing.

    Of course, it's still early yet and I'm not fully awake, so I very well may be totally incorrect.

    That´s true, but in fact I think it was a problem with my english, I was talking about programmers & programming languajes and I used "easy to read" as a "synonym" of "readable".

    Replase "easy to read" with "easy to read by any programmer" or "readable by any programmer".

  • JBange (unregistered) in reply to Ytram
    Really? I hadn't ever heard that the ternary operator is bad practice. I wonder what the justification for that blanket statement is? Anyone know?

    I know it was confusing the first time I saw one, but once I figured it out I was like "Neato!"

    I think it's considered bad practice because so many code grinders don't understand it, which means they can't hire a local high school student part time to maintain your code after they lay you off. I've never been asked not to use it, but I have had a coworker ask me "WTF is that thing?"

  • ZeoS (unregistered) in reply to ZeoS
    ZeoS:

    Replase "easy to read" with "easy to read by any programmer" or "readable by any programmer".



    "replase".replace("s","c"); :D
  • (cs) in reply to Satanicpuppy
    Satanicpuppy:

    I usually try and stay away from it because people who come along later always make the same grunting puzzled sound when they see it "Uhhaarooo?" and I have to explain it's just another way of writing a conditional, but the little crease between their eyebrows never goes away, and I know as soon as I'm not looking, that line will be replaced with if-then-else.

    So I just save a step, and leave it out.



    Why not just hand them a copy of "C for Dummies"? (Or whatever C-like language you are using.)

  • (cs) in reply to Alexis de Torquemada

    Alexis de Torquemada:
    Why not just hand them a copy of "C for Dummies"? (Or whatever C-like language you are using.)

    <FONT face="Courier New" size=2>why don't you punch them in the face and make them look pretty, tough guy?</FONT>

  • (cs) in reply to kipthegreat
    kipthegreat:
    Alex Papadimoulis:

    In yesterday's post (Doing What You Say, Saying What You Do), we all learned how important it is to be explicit when naming functions; preferably, one should encapsulate the entire function's logic into that function's name. Although I didn't cover commenting in yesterday's post, the same rule should apply: be as explicit as possible. The ideal comments are free of any non-programming languages (English, Dutch, etc) and should be as verbatim to the code as possible. Nick F demonstrates this perfectly with code from (what I would imagine is) the set_actionType_to_cont_or_end_depending_on_whether_hasSiblings_is_true_or_false() function ...

    <font color="#006600">// if hasSiblings, actionType == cont; else actionType == end.</font>
    <font color="#000099">if</font> ($hasSiblings)
    {
    $actionType = <font color="#990000">"cont"</font>;
    }
    <font color="#000099">else</font>
    {
    $actionType = <font color="#990000">"end"</font>;
    }


    Not really related to the WTF at all, but something I've been wondering:  Am I the only one who likes the ternary ?: operator?  It is usually grouped with global variables and goto statements as "bad no matter what."

    <font size="1">$actionType = $hasSiblings ? "cont" : "end";</font>

    That is much easier and doesn't waste so much space, and I don't see how it would be hard to understand...

    I use it as well, and here goes the board screwing up HTML again ...

  • (cs) in reply to llxx
    llxx:

    (cnt==1)? printf("There is 1 instance of foo\n")

            : printf("There are %d instances of foo\n",cnt);

     


    And instead of

    if (newCapacity > oldCapacity) {
        buffer = realloc(buffer, newCapacity);
        if (buffer == NULL)
        {
           fprintf(stderr, "Out of memory.\n");
           exit(1);
        }
    }

    write:

    newCapacity > oldCapacity && ((buffer = realloc(buffer, newCapacity)) || (fprintf(stderr, "Out of memory.\n"), exit(1), 0));

    Isn't that so much more readable?

  • (cs) in reply to Martin Vilcans
    Anonymous:

    Have fun localizing that code!


    Code isn't localized, it's internationalized.
    </supersmartassmode>
  • (cs) in reply to D
    Anonymous:
    switch (condition) { case first: (first value); case second: (second value); ...}


    If you switch on boolean values, then you've been reading TheDailyWTF for way too long.

  • (cs) in reply to masklinn
    masklinn:

    This furiously looks like Lisp code with random crap thrown in.



    How do you distinguish the Lisp part from the random crap?

  • (cs) in reply to emptyset
    emptyset:

    Alexis de Torquemada:
    Why not just hand them a copy of "C for Dummies"? (Or whatever C-like language you are using.)

    <font face="Courier New" size="2">why don't you punch them in the face and make them look pretty, tough guy?</font>



    Why don't you go and stick your head into a cow, as you promised, farmer boy?

  • (cs) in reply to Alexis de Torquemada
    Alexis de Torquemada:
    Anonymous:
    switch (condition) { case first: (first value); case second: (second value); ...}


    If you switch on boolean values, then you've been reading TheDailyWTF for way too long.


    In COBOL,
    <font style="font-family: courier new;" size="2">     evaluate true</font>
    will allow you to follow with cases on arbitrary expressions.  See <http://www.fluffycat.com/cobol/cobol-eval.html> for an example.

    Sincerely,

    Gene Wirchenko

  • (cs) in reply to Alexis de Torquemada

    Alexis de Torquemada:
    Why don't you go and stick your head into a cow, as you promised, farmer boy?

    <FONT face="Courier New" size=2>listen up punk there's something to ponder / you talk smack but you're just a flounder / working tex-mex pecs on your bowflex? / baby please sit down and get back to your everquest</FONT>

    <FONT face="Courier New" size=2>bustin' out chops like walker, texas ranger / ooh, look at you, you're mr. kung foo danger / like han solo look-alike at last year's dragon*con / plastic guns and a flash drive with lolikon</FONT>

    <FONT face="Courier New" size=2>punch 'em in the face if they don't close their brace / crack 'em in the skull if they don't check for null / that's why the boys come running for some love / just another big bear looking for a little cub</FONT>

  • Paul O (unregistered) in reply to Alexis de Torquemada
    Alexis de Torquemada:
    Satanicpuppy:

    I usually try and stay away from it because people who come along later always make the same grunting puzzled sound when they see it "Uhhaarooo?" and I have to explain it's just another way of writing a conditional, but the little crease between their eyebrows never goes away, and I know as soon as I'm not looking, that line will be replaced with if-then-else.

    So I just save a step, and leave it out.



    Why not just hand them a copy of "C for Dummies"? (Or whatever C-like language you are using.)



    It's this sort of thinking that led people to believe the Obfuscated C Contest produced shining examples of good code.  People who don't understand maintainability.  People who arrogantly think they're better than someone else because they can write obfuscated code.  If you use shorthand operators because you can't type the full form, then you should learn how to type.

    The only reason to use the ternary is if you need to focus on the (conditional) assignment, and not on the conditional (possibly including an assignment operation).  If you're using it to save space, that's bad practise.
  • (cs) in reply to Paul O
    Anonymous:
    Alexis de Torquemada:

    Why not just hand them a copy of "C for Dummies"? (Or whatever C-like language you are using.)



    It's this sort of thinking that led people to believe the Obfuscated C Contest produced shining examples of good code.  People who don't understand maintainability.  People who arrogantly think they're better than someone else because they can write obfuscated code.  If you use shorthand operators because you can't type the full form, then you should learn how to type.

    The only reason to use the ternary is if you need to focus on the (conditional) assignment, and not on the conditional (possibly including an assignment operation).  If you're using it to save space, that's bad practise.


    "My brother is a carpenter.  He always uses simple slotted screws because someday someone might want to unscrew it and they won't know about fancy Philips-head screws."

    Anyone who is stumped by     x = (foo > bar) ? this : that;     has no business maintaining C code.
    If they're too stupid to figure it out from context alone, how the hell are they going to understand something complex like a pointer to a function?

    Where do you draw the line about how dumbed-down you force your code to be?

    ok
    dpm
  • (cs) in reply to Paul O
    Anonymous:

    The only reason to use the ternary is if you need to focus on the (conditional) assignment, and not on the conditional (possibly including an assignment operation).  If you're using it to save space, that's bad practise.


    Your point of view is exceedingly small.  I use the conditional operator frequently.  One reason is to avoid repetition of the LHS,
    not only to save space but to save the person looking at the code from having to compare two variable names to make sure that the same target is specified both times.

    Here is one piece of actual code.   You tell me which is easier to read:

        if (bExtended)
        {
            pvScsiInfo->sWork.sPassthru.get_extended  = 1;
            pvScsiInfo->sWork.sPassthru.code_page_num = nWhichPage;
        }
        else
        {
            pvScsiInfo->sWork.sPassthru.get_extended  = 0;

            pvScsiInfo->sWork.sPassthru.code_page_num = 0;

        }

    or

        pvScsiInfo->sWork.sPassthru.get_extended  = (bExtended) ?          1 : 0;

        pvScsiInfo->sWork.sPassthru.code_page_num = (bExtended) ? nWhichPage : 0;


    ok
    dpm

  • (cs) in reply to dpm
    dpm:
    Anonymous:

    The only reason to use the ternary is if you need to focus on the (conditional) assignment, and not on the conditional (possibly including an assignment operation).  If you're using it to save space, that's bad practise.


    Your point of view is exceedingly small. ....


    And in both of your rebuttal postings, you used a conditional assignment to prove (???) that "the only reason to use the ternary is if you need to focus on the (conditional) assignment" is too narrow in focus. Huh?
  • Z (unregistered) in reply to Alexis de Torquemada
    Alexis de Torquemada:
    Anonymous:
    switch (condition) { case first: (first value); case second: (second value); ...}


    If you switch on boolean values, then you've been reading TheDailyWTF for way too long.



    If you think that a condition is a (two-valued) boolean, than you haven't been reading TheDailyWTF enough. :)
  • (cs) in reply to dpm
    dpm:
    Anonymous:

    The only reason to use the ternary is if you need to focus on the (conditional) assignment, and not on the conditional (possibly including an assignment operation).  If you're using it to save space, that's bad practise.


    Your point of view is exceedingly small.  I use the conditional operator frequently.  One reason is to avoid repetition of the LHS,
    not only to save space but to save the person looking at the code from having to compare two variable names to make sure that the same target is specified both times.

    Here is one piece of actual code.   You tell me which is easier to read:


    Instead of rebutting, you accidentally agreed, by posting two valid uses of the ternary.
  • Paul O (unregistered) in reply to dhromed
    dhromed:

    Instead of rebutting, you accidentally agreed, by posting two valid uses of the ternary.


    I concur.

    But a lot of people really hate being told that they're agreeing with something.  Especially if their point was to be disagreeable.

    And still people (apparently) think that all of the code linked from the following page demonstrates "C" code that any programmer worth a dime should be able to maintain:
    http://remus.rutgers.edu/~rhoads/Obfuscated_C/obfuscate.html
    (Hope I typed that all correctly.)  After all, if "I" can read the ternary operator without blinking, then it must be great code!

    I laugh.  I cry.  It becomes a part of me.
  • (cs) in reply to IceFreak2000
    IceFreak2000:
    No, you're not the only one - the ternary operator is very neat syntactic sugar. The only thing neater is C# 2.0's new null coalescing operator

    string test = null;
    System.Console.WriteLine("test = {0}", test ?? "{null value}");


    Which when does practically the same thing as GCC's ternary operator always did when the second operand was omitted, but surely soon everyone will think Microsoft invented this.

  • (cs) in reply to Alexis de Torquemada
    Alexis de Torquemada:
    Which when does practically the same thing as GCC's ternary operator always did when the second operand was omitted, but surely soon everyone will think Microsoft invented this.



    Remove the first "when" and recompile.

  • (cs) in reply to Paul O
    Anonymous:

    It's this sort of thinking that led people to believe the Obfuscated C Contest produced shining examples of good code.  People who don't understand maintainability.  People who arrogantly think they're better than someone else because they can write obfuscated code.  If you use shorthand operators because you can't type the full form, then you should learn how to type.

    The only reason to use the ternary is if you need to focus on the (conditional) assignment, and not on the conditional (possibly including an assignment operation).  If you're using it to save space, that's bad practise.


    1. No one thinks obfuscated C produces shining examples of good code. You are fighting straw men.
    2. If you cannot easily read horribly nested expressions, whether they involve ternary operators or not, you are excused.
    3. If, on the other hand, you don't know what the ternary operator does at all, you are a dummy, and should either learn about it or switch programming languages. Or your profession.

    Is that so hard to understand - even to a dummy?

  • (cs) in reply to Alexis de Torquemada

    I don't mind the ternary operator that much. OTOH, it's misused a lot and can make a simple assignment look a bit more complicated, if used incorrectly, so I can understand why some people discourage it.

  • Steve Bennett (unregistered) in reply to dpm
    dpm:

    pvScsiInfo->sWork.sPassthru.get_extended  = (bExtended) ?          1 : 0;

      pvScsiInfo->sWork.sPassthru.code_page_num = (bExtended) ? nWhichPage : 0;


    For the first line how about:
    pvScsiInfo->sWork.sPassthru.get_extended  = !!(bExtended);

    Depends if this is part of working set of C idioms though.  Some people would be horrified.  !! seems very natural to me though as a boolean cast.

    Steve
  • dasmb (unregistered) in reply to Anonymous

    I think this is typical of VB coders.  They must teach them to write TODO comments first over at New Horizons, then never told them it was okay to take them out when you've done it.

    I once worked with a guy famous for 20 line design-by-contract style header comments for one line private methods. 

    Coincidentally, when he did really wonky things (like iterate over massive arraylists looking for a specific string rather than use a goddamn hashmap in the first place or loading the same record three or four times in a row), he didn't document them all.

    But the best of all is when he'd copy & paste 3/4 of an object's code, without changing a LETTER of his "documentation."  Now, this is something I was guilty of too (we did a lot of attribute-based O/R mappings and it was just easier to C&P then remember the somewhat esoteric syntax), but I at least tried to encapsulate the similarities. 

  • (cs) in reply to Steve Bennett
    Anonymous:

    For the first line how about:
    pvScsiInfo->sWork.sPassthru.get_extended  = !!(bExtended);

    Depends if this is part of working set of C idioms though.  Some people would be horrified.  !! seems very natural to me though as a boolean cast.

    Steve


    You could define a macro:

    #define BOOLIFY(x) (!!(x))

    It's an open question whether that would help some people, though. :)
  • Ed (unregistered) in reply to kipthegreat

    I  Funny you should mention the ternary operator.  I posted an entire blog entry about it not too long ago:  http://emooers.blogspot.com/2005/10/c-ternary-operator.html

    Some people abuse and misuse it, but I've found a couple of very appropriate applications for it.

    • Ed

  • (cs) in reply to Ed

    Anonymous:
    I posted an entire blog entry about it not too long ago

    <FONT face="Courier New" size=2>hold on a minute.  you just said you posted to your blog about the ternary operator.  </FONT>

    <FONT face="Courier New" size=2>i just want you to know that you just won the international mccorkle award that i just made up.  it's awarded to those that demonstrated a superior ability to overcome the outer thresholds of geekdom and rise to take your place as not only a total loser, but a total loser of the world.</FONT>

  • Porn Star (unregistered) in reply to rbrendler

    The greatest thing here is that the behavior in this condition varies with compiler!  Using GCC you get a different result than MS's VC6 compiler.

    Very nice.

Leave a comment on “Self-Documenting Comments”

Log In or post as a guest

Replying to comment #:

« Return to Article