Slashdot Mirror


Comments are More Important than Code

CowboyRobot writes "I was going through some code from 2002, frustrated at the lack of comments, cursing the moron who put this spaghetti together, only to realize later that I was the moron who had written it. An essay titled Comments Are More Important Than Code goes through the arguments that seem obvious only in hindsight - that 'self-documenting' code is good but not enough, that we should be able to write code based on good documentation, not the other way around, and that the thing that separates human-written code from computer-generated code is that our stuff is readable to future programmers. But I go through this argument with my colleagues, who say that using short, descriptive variable names 'should' be enough as long as the code is well-organized. Who's right?"

42 of 1,021 comments (clear)

  1. Even more annoying... by Anonymous Coward · · Score: 5, Funny

    I used to grade student's code as a TA at my university, and I'll tell you what is more annoying than NO comments, this:

    printf("Encrypt message..."); /* print "Encrypt message..." to the console */

    and then followed by about 150 lines of uncommented spaghetti code

    1. Re:Even more annoying... by blueadept1 · · Score: 1, Funny

      I'm really sorry dude. I thought you didn't know what print was :/

      Just flaunting my leet ski11z. Keep it uber.

    2. Re:Even more annoying... by FuzzyBad-Mofo · · Score: 2, Funny

      And a metamod system to deal with the inevitable poor moderation. Hmm, Slashcode as a source control system? Scary thought..

    3. Re:Even more annoying... by s100w · · Score: 2, Funny
      Some of your graduates most have taken jobs at my company.
      /**
      * Gets the name
      * @returns String name
      * @author RG
      * @throws Exception
      * @param boolean
      */
      public String getName(boolean b) [
      if (b) {
      return magicalSideEffectGetter();
      } else {
      return name;
      }
      // Changed by RG 10/18/1999
      // Re bug fix #1044
      // return name;
      }
      Of course, the old code, revision date, and author is in CVS. And the bug is in the bug database, but ... more comments are better, right? In the real world, this method would be about a page long.
    4. Re:Even more annoying... by AndroSyn · · Score: 4, Funny

      1994 is calling and it wants its Sparc IPX back.

    5. Re:Even more annoying... by coaxial · · Score: 5, Funny

      I came across that with a grad student at my univ. I told him that I couldn't read his code because it wasn't indented. He said, "I was going to back and do that later." WTF? I thought that was going to be the Quote of The Day, but it turned out he wasn't done yet. I started indenting every line as I walked through a function. Seeing this, he inquired, "How are you moving those lines over?" My answer? "Tab."

      Shocking.

    6. Re:Even more annoying... by Gilk180 · · Score: 5, Funny

      An emacs post replying to an Eclipse post and you choose to point out how bloated emacs is?

    7. Re:Even more annoying... by el-spectre · · Score: 5, Funny

      As a freshman I had a professor who kept giving me shit about not having enough comments, so my next program looked something like this: ...
      a=5; //assign integer variable 'a' a value of 5
      b=2; //assign integer variable 'b' a value of 2
      print(a+b); //print to the console the sum of integer variable 'a' and integer variable 'b' ...

      and so on, for about 200 lines. Worthless commenting? Sure, and childish too... but it was amusing and I never got docked for insufficient commenting again :)

      --
      "Faith: Belief without evidence in what is told by one who speaks without knowledge, of things without parallel." - A.B.
    8. Re:Even more annoying... by aixou · · Score: 4, Funny

      Heh.. his roommates probably think he's having some marathon maturbation session when he's in his one of code-indenting sessions (presumably with the space bar?)

    9. Re:Even more annoying... by Anonymous Coward · · Score: 1, Funny

      Reminds me of an old roommate. After he finished typing a paper in Word, he'd go back and put a pair of carriage returns at the end of every line in order to double space the paper.

      Was kinda funny though when he had to revise the paper later on, having to undo all that work then redo it after making the edits...

    10. Re:Even more annoying... by Ratbert42 · · Score: 3, Funny

      I just ripped this comment out of our code today: // close the file
      closeFile();

    11. Re:Even more annoying... by geminidomino · · Score: 5, Funny

      So tell us, since you have an inside view:

      When is Longhorn actually coming out?

    12. Re:Even more annoying... by zonix · · Score: 3, Funny

      What should be done is: Take this 1000 line programme. Add on 5 lines. Add on 20 lines. Add on 100 lines.

      Better yet - remove 5 lines. remove 20 lines. remove half the lines ... and it better still work!

      Pfft! Just remove the comments! ;-)

      z
      --
      What would an EWOULDBLOCK block, if an EWOULDBLOCK could block would? -- me
    13. Re:Even more annoying... by aneeshm · · Score: 1, Funny

      At my school , we have the same problem . But because we had not reached a point where comments were useful , we were asked to comment trivial programs .

      I got sick of it , so I commented every cout , cin , and variable and function declaration , but none of the working code . I didn't get marked down , and the teacher hasn't bothered me about comments since .

    14. Re:Even more annoying... by ashultz · · Score: 2, Funny

      I can't tell if this is a real comment or a joke.

      gg=G to indent C code? The only way to learn a command like that is to have it whispered to you in the alley behind the Bang and Bracket pub at midnight by a Romanian Documentation Smuggler.

  2. Use long variable names by Joey7F · · Score: 2, Funny

    Just go ahead and use the long names, that alone will reduce documentation/commenting

    --Joey

    1. Re:Use long variable names by agm · · Score: 2, Funny

      I skip it and use "l" instead. Variable names that start with k are reserved solely for developing KDE apps.

  3. Comments more important? by Rollie+Hawk · · Score: 5, Funny

    Has he read the ones here?

    --
    Before any liberals are tempted to mod up one of my comments, a word of warning: I'm actually making fun of you.
  4. huh, just like /. by eobanb · · Score: 4, Funny

    ...where comments are more important than articles.

    --

    Take off every sig. For great justice.

  5. Re:Gotta document that code... by eobanb · · Score: 2, Funny

    Judging from your indenting, I'm sure glad I don't have to read YOUR code.

    --

    Take off every sig. For great justice.

  6. Nah by Anonymous Coward · · Score: 5, Funny

    Variable names should be proportional to the size of their scope within the code.

    1. Re:Nah by Fjornir · · Score: 2, Funny

      So the length of a variable name for a global var should approach infinity?

      --
      I want a new world. I think this one is broken.
    2. Re:Nah by mazarin5 · · Score: 2, Funny
      int main()
      {
      unsigned char ucGlobalVariableNamesShouldBeReallyLongEspeciallyI fTheyAreImportantAndYouNeedToKnowExactlyWhatTheyDo AndYouDontReallyFeelLikeUsingProperCommentsAndJust RetypingAReallyLongNameLikeThisIsSoMuchMoreConveni entAndGodForbidYouShouldMakeASpellingErrorLetMeTel lYouHowFrustratingThatCanBeAnywaysImRamblingBackTo ThePointWhichIsThatSuperLongButDescriptiveVariable Names = false;
      return 0;
      }
      --
      Fnord.
    3. Re:Nah by slashdot.org · · Score: 2, Funny

      That was modded (+1, Funny), but I think it was a serious comment. It wasn't a bad rule of thumb, either.

      Heh, it would have been funny if he'd said "Variable names should be inversely proportional to the size of their scope within the code."

    4. Re:Nah by slashdot.org · · Score: 2, Funny

      yeah. Nothing can cause quite the same thrill as declaring a global "int i".

  7. Uhh... by PrimeWaveZ · · Score: 5, Funny

    UsngShrtCmtsIsOftNotEnghAsOneMyNdToReWrtShtInTheFt r.

  8. Indeed by screwballicus · · Score: 5, Funny
    I was recently reading through some code I wrote ages ago and hadn't looked at in years, and wondering...dear god, what's this confused mess trying to do?

    Imagine my relief when I came upon a helpful comment:

    What the hell was I thinking when I wrote this?

    All it took was one comment to put my mind to rest: no, it's not just me being stupid in the present. This code seemed this terrible back then, too.

    Comments save the day once again.
  9. If I may... by ecko3437 · · Score: 1, Funny

    Why do you think they call it 'code'?

    --
    -Eric Smith
  10. Re:Gotta document that code... by Anonymous Coward · · Score: 1, Funny

    I made the code up on the spot, dumbshit, and didn't even finish it. I can't believe you stopped jerking off for the five minutes it must have taken to write that.

    Posting AC to stay at your level.

  11. Re:Gotta document that code... by BasilBrush · · Score: 2, Funny

    Unless you use python. People using a different indenting style to the original programmer will learn the hard way that that's not going to work.

  12. semicolons in column 80 by morcheeba · · Score: 4, Funny

    I had a co-worker who needed a little help with his program. It was C code, but all the semicolons were in column 80, arranged in a nice column. Yikes. He casually said "what, you've never seen that?". Turns out that part of his problem was that he was using float to represent a byte position in an extremely large file. He knew enough that UINT32 wouldn't do it, but ...

    1. Re:semicolons in column 80 by SComps · · Score: 2, Funny

      I popped my coding cherry on RPG II. My momma was right, I later regretted not waiting.

  13. Re:Gotta document that code... by spammacus · · Score: 5, Funny

    All generalisms are wrong.

    Isn't that a paradox? :)

  14. Re:What do you guys think of the following code? by natrius · · Score: 5, Funny

    Please don't post my code on Slashdot.

    Come see me in my office.

    - Your Supervisor

  15. Great topic for a funny (but long) story by RetiredMidn · · Score: 2, Funny
    Back in college in the 70's, I worked as a "consultant" in the computer center, assisting users with whatever questions they might have. A freshman taking the entry-level programming course, which used FORTRAN on punched cards(!), came to ask why his program wouldn't compile.

    I opened his listing (printed on non-recycled 11x14 paper), and was surprised to find the listing double-spaced. I tried to figure out how he did it (assuming it was an obscure parameter on the job control card), and eventually realized that he had inserted a blank comment between every line of code (ignore and forgive the periods; I had trouble indenting):

    C
    . INT I
    C
    . REAL X
    C
    . I = 0
    C
    . X = 0.0

    Understand that this meant that, while at the keypunch, he took the time to type a single "C", then feed a new card (taking a couple of seconds), before entering another line of code. (Not to mention the waste of expensive card stock.)

    When I asked why he took the time to generate all those blank cards, he answered, sincerely, (wait for it...):

    "The professor said the program would be easier to debug if we had a lot of comments."

  16. Re: On code maintenance and commenting by Anonymous Coward · · Score: 1, Funny

    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

    John F. Woods

  17. Re:Quality not quantity by professorfalcon · · Score: 2, Funny

    variables that do something relatively mindless I name 'Gilligan'

    Do particulary sexy variables get named Ginger or Mary Ann?

  18. Re:Top Ten Code Comment Do's List by bytesmythe · · Score: 4, Funny
    1. Comment each function
    - Function name
    - what it does
    - parameters
    parameter name - what is is for and any restrictions on it (i.e., must not be null)
    - return value (all possible return values)

    /*
    * Function: p-inc
    * Purpose: adds one to a positive integer
    * Input: x, the integer to be incremented.
    * Possible return values: 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480, 481, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558, 559, 560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571, 572, 573, 574, 575, 576, 577, 578, 579, 580, 581, 582, 583, 584, 585, 586, 587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 597, 598, 599, 600, 601, 602, 603, 604, 605, 606, 607, 608, 609, 610, 611, 612, 613, 614, 615, 616, 617, 618, 619, 620, 621, 622, 623, 624, 625, 626, 627, 628, 629, 630, 631, 632, 633, 634, 635, 636, 637, 638, 639, 640, 641, 642, 643, 644, 645, 646, 647, 648, 649, 650, 651, 652, 653, 654, 655, 656, 657, 658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 670, 671, 672, 673, 674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687, 688, 689, 690, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700, 701, 702, 703, 704, 705, 706, 707, 708, 709, 710, 711, 712, 713, 714, 715, 716, 717, 718, 719, 720, 721, 722, 723, 724, 725, 726, 727, 728, 729, 730, 731, 732, 733, 734, 735, 736, 737, 738, 739, 740, 741, 742, 743, 744, 745, 746, 747, 748, 749, 750, 751, 752,

    --
    bytesmythe
    Hypocrisy is the resin that holds the plywood of society together.
    -- Scott Meyer
  19. Depends on the comments... by The_Incubator · · Score: 2, Funny
    I recently finished a product that my company inherited from another developer that was going out of business. In the midst of some of the worst code I've ever seen were comments like this:

    //here again we continue with the hack we don't understand

    No joke, that was in production code from developer that I'm sure a lot of you would recognize. There was also something in there to the effect of:

    //we continue again with the +1 magic

    But I didn't find that one, I just heard stories.

    Nick

  20. here's some Pretty Evil Shit - Re:Even more annoyi by Fubari · · Score: 5, Funny
    Here is an example of some Pretty Evil Shit:
    I want to laugh when I read it.
    Some of it is funny.
    Some of it is just scary.
    The human mind can be a freakishly messed up thing.
    http://mindprod.com/unmaindocumentation.html
    It is part of a larger essay about writting crappy code.
    Anybody that even comes close to software development
    should check it out.

    --- begin excerpts ---
    Avoid Documenting the "Obvious" : : If, for example, you were
    writing an airline reservation system, make sure there are at
    least 25 places in the code that need to be modified if you were
    to add another airline. Never document where they are. People who
    come after you have no business modifying your code without
    thoroughly understanding every line of it.

    Units of Measure : : Never document the units of measure of any
    variable, input, output or parameter. e.g. feet, metres, cartons.
    This is not so important in bean counting, but it is very important
    in engineering work.

    As a corollary, never document the units of measure of any conversion
    constants, or how the values were derived.

    It is mild cheating, but very effective, to salt the code with some
    incorrect units of measure in the comments.

    If you are feeling particularly malicious, make up your own unit of
    measure; name it after yourself or some obscure person and never
    define it. If somebody challenges you, tell them you did so that
    you could use integer rather than floating point arithmetic.

    On the Proper Use of Design Documents : : When implementing a very
    complicated algorithm, use the classic software engineering principles
    of doing a sound design before beginning coding. Write an extremely
    detailed design document that describes each step in a very complicated
    algorithm. The more detailed this document is, the better.

    In fact, the design doc should break the algorithm down into a hierarchy
    of structured steps, described in a hierarchy of auto-numbered individual
    paragraphs in the document. Use headings at least 5 deep. Make sure that
    when you are done, you have broken the structure down so completely that
    there are over 500 such auto-numbered paragraphs.

    For example, one paragraph might be: (this is a real example)
    1.2.4.6.3.13 - Display all impacts for activity where selected
    mitigations can apply (short pseudocode omitted).
    then... (and this is the kicker) when you write the code, for each of these paragraphs
    you write a corresponding global function named:
    Act1_2_4_6_3_13()
    Do not document these functions. After all, that's what the
    design document is for!

    Since the design doc is auto-numbered, it will be extremely difficult
    to keep it up to date with changes in the code (because the function
    names, of course, are static, not auto-numbered.) This isn't a problem
    for you because you will not try to keep the document up to date.
    In fact, do everything you can to destroy all traces of the document.

    Those who come after you should only be able to find one or two
    contradictory, early drafts of the design document hidden on some
    dusty shelving in the back room near the dead 286 computers.
    --- end excerpts ---
  21. Re:Top Ten Code Comment Do's List by Wavicle · · Score: 5, Funny
    * Oh man... I'm so tired of typing these numbers!

    You should have written a function to generate the list of numbers for you:

    /*
    * Function: num_list
    * Purpose: Returns a list of positive integers greater than one
    * Input: x, the largest positive integer in the list.
    * Possible return values: "2", "2, 3", "2, 3, 4", "2, 3, 4, 5", "2, 3, 4, 5, 6", "2, 3, 4, 5, 7", "2, 3, 4, 5, 6, 7, 8", "2, 3, 4, 5, 6, 7, 8, 9", "2, 3, 4, 5, 6, 7, 8, 9, 10", "2, 3, 4, 5, 6, 7, 8, 9, 10, 11", "2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12", "2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14", "2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15", "2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16", "2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17", "2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18", "2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19", "2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20",

    * Hmmm, I think I need a new function to generate the comment for this function...
    */
    --
    Education is a better safeguard of liberty than a standing army.
    Edward Everett (1794 - 1865)
  22. Re:There are 10 types of programmers by Anonymous Coward · · Score: 1, Funny

    Those who understand binary, and those who dont.

    Sorry! :(