Stories
Slash Boxes
Comments

News for nerds, stuff that matters

Comments are More Important than Code

Posted by timothy on Tue Apr 26, 2005 09:11 PM
from the if-you-disagree-provide-comments dept.
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?"
This discussion has been archived. No new comments can be posted.
Comments are More Important than Code | Log In/Create an Account | Top | 1021 comments (Spill at 50!) | Index Only | Search Discussion
Display Options Threshold:
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
(1) | 2 | 3
  • Even more annoying... (Score:5, Funny)

    by Anonymous Coward on Tuesday April 26 2005, @09:12PM (#12354434)
    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
    • Re:Even more annoying... by eobanb (Score:3) Tuesday April 26 2005, @09:19PM
      • Re:Even more annoying... by FuzzyBad-Mofo (Score:2) Tuesday April 26 2005, @09:30PM
        • Top Ten Code Comment Do's List (Score:5, Insightful)

          by Anonymous Coward on Tuesday April 26 2005, @10:44PM (#12355097)
          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)

          2. Add comments to each file you modify so that over time, the file becomes better documented

          3. Add ASSERT() like comments and ASSERT() or equivalent to your code

          4. Use dividing comments like a line of dashes to seperate blocks of code

          5. Put in a '?' comment for code that you do not understand (good for function headers)

          6. Avoid stupid naming schemes for your local variables since that makes it harder to comment

          7. Review your code for both logic and comment completeness after you code it before committing it to version control

          8. Tag your bug fixes, code enhancements with a comment followed by a dash, date, and your initials. This is essential for large projects or for anything you will be working on for more than 6 months.

          9. Format your comments so that multiple line comments line up on the left hand side (increases readability)

          10. Don't count on java doc or equivalent as being good enough code documentation.

          [ Parent ]
          • by bytesmythe (58644) <bytesmythe AT gmail DOT com> on Tuesday April 26 2005, @11:35PM (#12355511)
            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,

            [ Parent ]
          • Re:Top Ten Code Comment Do's List by Coryoth (Score:2) Wednesday April 27 2005, @01:17AM
          • Re:Top Ten Code Comment Do's List (Score:5, Interesting)

            by dooglio (313304) on Wednesday April 27 2005, @01:27AM (#12356119)
            (http://www.dooglio.net/)
            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)

            ****

            I really hate having parameters documented in a comment block above the function declaration, because it seldom gets updated when the function signature itself is changed.

            I worked with a guy who used to format his functions like this:

            void my_func( // My function does foo
            int arg1, // arg1 documentation
            char * arg2, // arg2 documentation
            ...
            )
            {
            // Function guts go here
            }

            The nice thing about this method, although the "look" of a function is broken up, is that when you add or change parameters, the programmer is a lot more likely to change the documentation as well.

            When you're under the gun to get the code out, the less the developer has to do to change the docs, the more likely it is they will be changed.
            [ Parent ]
          • Re:Top Ten Code Comment Do's List (Score:5, Informative)

            by HomeworkJunkie (877015) on Wednesday April 27 2005, @01:48AM (#12356249)
            We comment all functions and classes using the Doxygen format. This means that the comments can be used to generate HTML, PDF or man pages for the functions and classes. It will also generate call trees if required. Very useful.
            [ Parent ]
          • But even more important... by lyk (Score:1) Wednesday April 27 2005, @02:08AM
          • Re:Top Ten Code Comment Do's List by zsombor (Score:2) Wednesday April 27 2005, @03:08AM
          • Re:Top Ten Code Comment Do's List by FireFury03 (Score:1) Wednesday April 27 2005, @05:02AM
          • Re:Top Ten Code Comment Do's List by famebait (Score:2) Wednesday April 27 2005, @05:05AM
          • Re:Top Ten Code Comment Do's List by bcd (Score:1) Wednesday April 27 2005, @06:18AM
          • Not so fast! by Anonymous Brave Guy (Score:2) Wednesday April 27 2005, @06:57AM
            • Re:Not so fast! by mwood (Score:2) Wednesday April 27 2005, @09:35AM
            • The three items you seem to object to all have fairly legitimate uses in some computing contexts.

              1. Comment each function
              - Function name


              Why do you need to repeat the name of the function? It's right there in the code.

              Yes, and in some contexts that is precisely the problem. Depending on the language and editor(s) you are using, embedding the function names in comments can be very useful.

              For example, if I use an "FC" command in a Unisys 2200 mainframe editor (say ED or UEDIT) to show all lines starting with "C" (comment lines) in a Fortran program, the results will be confusing if the function names themselves are not specified in a comment line. All I will see is comments with no context.

              By adding the subroutine or function names in the related comments themselves, that missing context is restored.

              5. Put in a '?' comment for code that you do not understand (good for function headers)

              Code with personal annotations should never be released.

              While I agree with this part,

              If you don't understand it, you shouldn't be working with it; go find someone who does understand it instead, or stop and work it out.

              this part of your comment seems limited to "perfect world" scenarios only.

              I've spent most of my career working on mainframe code that was either originally written by people at other organizations or companies, or that was written so long ago that the original designers and coders were either retired or deceased.

              When making a first pass at understanding such code, I've found it to be quite useful to leave various comments in the code so I have a good idea about my level of understanding the next time I have to come into that area of code.

              I've also found such comments useful when left in the code by others, since it reflects their level of understanding as well. Some of the stuff I'm looking at now was originally coded in the early 80's and rewritten by my predecessor, and the comments he's placed in the code have proven to be invaluable to me, even though some of them were speculative in nature. Something like "What does this code do?" in his comments often alerts me to a tricky part in the logic that I'll want to pay special attention to.

              FWIW, question marks and other comments/speculations are commonly used when working on someone else's previously-written code, at least in my experience, especially when that code is only intended for use in-house (the source is not released to customers).

              8. Tag your bug fixes, code enhancements with a comment followed by a dash, date, and your initials. This is essential for large projects or for anything you will be working on for more than 6 months.

              Once again, nothing with personal annotations should ever be released into source control. Not only should I not need to know who wrote or changed a particular snippet of code to fix a bug, I shouldn't even be able to tell by looking at the code.

              When working on code that was written in-house, and which is intended to stay in-house, I *want* to know who made a certain change and why it was done. That type of notation can save me (or another support programmer) a lot of time.

              While those types of details might be handled by the source control system(s) that you work with, keep in mind that some of us work on platforms where analogs to the UNIX-like source control systems many people are used to simply do not exist (and often such a thing isn't really needed).

              When I did Unisys A-series mainframe contract work at a small company in southern Minnesota, they used the unparsed end of each COBOL line as a comment area, and one always placed their initials and a date there so people knew who did which changes and when those changes were created. The editor they used (CANDE) even had a special setting to automagically place a change mark on the end of each line that was modified.

              A si
              [ Parent ]
            • Re:Not so fast! by AuMatar (Score:2) Wednesday April 27 2005, @12:46PM
          • Re:Top Ten Code Comment Do's List by Ripplet (Score:2) Wednesday April 27 2005, @07:35AM
          • Re:Top Ten Code Comment Do's List by Espen Skoglund (Score:2) Wednesday April 27 2005, @08:04AM
          • Re:Top Ten Code Comment Do's List by Skapare (Score:1) Wednesday April 27 2005, @10:39AM
        • 1 reply beneath your current threshold.
    • Re:Even more annoying... (Score:5, Insightful)

      by Klar (522420) <curchin@@@gmail...com> on Tuesday April 26 2005, @09:22PM (#12354521)
      (http://www.curchin.org/ | Last Journal: Monday January 17 2005, @10:02PM)
      hah I also grade as a university TA, and let me tell you what is more annoying than that... people who don't know how to indent their code, and want you to help debug it! Ahhhhh!!!!!
      [ Parent ]
    • Re:Even more annoying... by blueadept1 (Score:1) Tuesday April 26 2005, @09:27PM
    • Re:Even more annoying... by s100w (Score:2) Tuesday April 26 2005, @09:34PM
    • Re:Even more annoying... (Score:5, Insightful)

      by Anonymous Coward on Tuesday April 26 2005, @09:46PM (#12354709)

      You know whose fault that is? The morons that mark you down if you don't comment trivial programs like Hello World.

      I actually ended up doing three introductory programming courses at college and university (overlap in curricula), and I've been marked down for not including this type of comment in all three courses.

      printf("Hello, world!\n"); /* send "Hello, world!" to the console and move to the next line. */

      The problem is that they try and teach you to write comments prematurely. When all most people on the course can write are programs like Hello World, they really can't see the use of comments and there are no good examples to give. The use of comments should come after things like functions etc IMHO, so that students can actually see how comments can be useful.

      [ Parent ]
      • Re:Even more annoying... by Brandybuck (Score:1) Tuesday April 26 2005, @10:40PM
      • Ive been in enough classes this year (well below my level.. dammit, I need the "letters") that Ive come to the conclusion that introductory programming courses are taught all wrong.

        Write 5 lines. Write 20 lines. Write 100 lines. Of useless and pointless code.

        What should be done is: Take this 1000 line programme. Add on 5 lines. Add on 20 lines. Add on 100 lines. Beacuse that would require being able to read code, too. Being able to understand what is already there is frequently more then half the battle. Saying "What the fuck does this mean?" a few dozen times is the only way to get to write comments. Being a sysadmin, rather then a full time programmer, this took me litteraly years to learn. And it was usualy "What the fuck were you doing here, brain? Dammit, one of these days were going to have to start commenting our code."

        About 6 months ago, I read through a project request on one of these ebay-for-coders sites. Language optional; 1 comment per 5 LOC, but if using Perl, 1 comment per 2 LOC.. With requrements like that you are only asking to get BS comments like "Print X to the console".
        [ Parent ]
      • Re:Even more annoying... (Score:5, Insightful)

        by JohnsonWax (195390) on Tuesday April 26 2005, @11:07PM (#12355314)
        The problem is that they try and teach you to write comments prematurely.

        No, the problem is that they try and teach you to write code prematurely.

        Document your application, requirements, constraints, and system interactions (what the engineer does). Then write the code (what the coder does).

        What you will quickly learn is that it's better to be the engineer than the coder. What you'll realize is that the engineer is the client-side guy that figures out how to solve the challenge presented and the coder is the guy who can live in Manipal.

        The computer scientist is another guy altogether, that sets the boundaries within which the engineer must work and provides many of the tools.

        Somehow CS education has gotten horribly derailed, and asks students to combine the equivalents of electromagnetic theory, power system design, and basic home wiring in one curriculum. No wonder enrollments are plummeting - nobody knows what a CS major is or should do.
        [ Parent ]
      • x86 Assembly can be worse by grahamsz (Score:2) Wednesday April 27 2005, @12:36AM
        • Actually by Moraelin (Score:2) Wednesday April 27 2005, @03:20AM
      • by Fubari (196373) on Wednesday April 27 2005, @01:01AM (#12356023)
        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 [mindprod.com]
        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 ---
        [ Parent ]
      • Re:Even more annoying... by aneeshm (Score:1) Wednesday April 27 2005, @03:19AM
        • 1 reply beneath your current threshold.
      • Re:Even more annoying... by John Courtland (Score:2) Wednesday April 27 2005, @09:02AM
      • Re:Even more annoying... by Anonymous Coward (Score:1) Wednesday April 27 2005, @12:47AM
      • 1 reply beneath your current threshold.
    • Re:Even more annoying... by ZhuLien (Score:1) Tuesday April 26 2005, @10:03PM
    • Re:Even more annoying... by llamaluvr (Score:3) Tuesday April 26 2005, @10:09PM
    • Re:Even more annoying... (Score:5, Funny)

      by el-spectre (668104) on Tuesday April 26 2005, @10:18PM (#12354924)
      (Last Journal: Tuesday December 30 2003, @07:21PM)
      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 :)
      [ Parent ]
    • Re:Even more annoying... by Bluedove (Score:2) Tuesday April 26 2005, @10:41PM
    • Re:Even more annoying... (Score:4, Funny)

      by Ratbert42 (452340) on Tuesday April 26 2005, @11:03PM (#12355269)
      I just ripped this comment out of our code today: // close the file
      closeFile();
      [ Parent ]
    • Re: On code maintenance and commenting by Anonymous Coward (Score:1) Tuesday April 26 2005, @11:26PM
    • Re:Even more annoying... (Score:5, Interesting)

      by rve (4436) on Wednesday April 27 2005, @12:41AM (#12355943)
      At work, we are not allowed to use comments in the code.

      Allowing comments would "encourage coders to use clever tricks" according to the technical director.

      [ Parent ]
    • Re:Even more annoying... by skasingularity (Score:1) Wednesday April 27 2005, @12:49AM
    • Re:Even more annoying... by Liquid Len (Score:1) Wednesday April 27 2005, @01:04AM
    • Re:Even more annoying... by jmb_no (Score:1) Wednesday April 27 2005, @02:28AM
    • Re:Even more annoying... by Ed Avis (Score:2) Wednesday April 27 2005, @02:28AM
    • Re:Even more annoying... by FireFury03 (Score:2) Wednesday April 27 2005, @04:52AM
    • Well... by ThePyro (Score:2) Wednesday April 27 2005, @07:51AM
    • Re:Even more annoying... by adabyron (Score:1) Wednesday April 27 2005, @08:47AM
    • 3 replies beneath your current threshold.
  • Gotta document that code... (Score:5, Interesting)

    by TripMaster Monkey (862126) * on Tuesday April 26 2005, @09:12PM (#12354435)
    From the Summary:
    ...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?


    You are. No question.

    I have a saying I like to use when people (usually managers) try to persuade me to take the quick/cheap way out:


    "Any time/money you save by cheaping out now, you'll wind up having to pay back twice over on the back end."


    I've yet to see this maxim disproved, and it's just as applicable to coding as it is to anything else. Your colleagues are certainly correct when they state that the code must be well organized, but this simply isn't enough. If you don't put in the necessary time (minimal, really) to properly document your code, you'll wind up spending a lot of time trying to figure out just what you did and why. Also, even if you can remember exactly what your code is all about, the guy that comes after you probably won't...proper documentation is professional courtesy. I suppose they'll learn after they spend a few hours puzzling over a piece of old code (that's how I learned...:P ). Sooner or later, not documenting properly will bite them in the ass.

    • Re:Gotta document that code... (Score:5, Insightful)

      by Fjornir (516960) on Tuesday April 26 2005, @09:14PM (#12354446)
      The canonical form of your maxim is "If you don't have time to do it right the first time, when will you find time to do it again?". The correlary is: "The problem with quick and dirty is that dirty remains long after quick is forgotten."
      [ Parent ]
    • Re:Gotta document that code... by eobanb (Score:3) Tuesday April 26 2005, @09:16PM
    • I agree. Giving your variables descriptive names is a good practice, but it's not nearly enough "commenting". If you have a piece of code that manipulates the values of those descriptive variables with bit masks to spit out a value, how is someone supposed to know what you just did? I think the adage "you don't document your code, but you code your document" is a little extreme, but it gets across the importance of code documentation. I'm working on an open source project of my own right now. I went back to modify a piece of code that I hadn't touched for nearly 4 months, only to make absolutely no sense of what the hell I was doing there. Luckly I commented it extremely well so after reading the comments I was able to get that "Oh yeah..." feeling and make the appropriate modifications without breaking the code. Lesson: commenting code may take you some more time now, but it's going to save you a hell of a lot of time later.

      On that topic, what are some good examples of well-commented code? Rather than just see words, I'd rather see real-life applications of these practices. For starters, here's typically how much I comment my code: Allacrost source code [sourceforge.net] (note: not all files were written by me, by I try to stress heavy commenting on the rest of my team)
      [ Parent ]
      • Hmmm... by ta bu shi da yu (Score:2) Tuesday April 26 2005, @09:51PM
      • Re:Gotta document that code... (Score:5, Insightful)

        by gregmac (629064) on Tuesday April 26 2005, @11:05PM (#12355292)
        (http://groogs.com/)
        Rather than just see words, I'd rather see real-life applications of these practices. For starters, here's typically how much I comment my code:
        tmp = last_update; // tmp = last update time
        last_update = SDL_GetTicks(); // Set the last update time to now
        tmp = last_update - tmp; // tmp = difference between now and last update
        fps_timer += tmp; // Increase our fps millisecond timer
        fps_counter++; // Increase our frame count


        There's also something as far as too much commenting. Don't get me wrong, I can't stand uncommented code. I learnt, like most people, the hard way -- go back to edit something I did 6 months ago, and have no idea what's happening.

        But I don't think this is useful: // tmp = last update time. You've just spelled out in english what tmp = last_update; says in code. Comments don't add anything if you can just read the code and figure it out. If anything, it's a waste of time.

        Here's my version of your same section
        // find the time since our last update
        tmp = last_update;
        last_update = SDL_GetTicks(); // now
        tmp = last_update - tmp;
        // add time and increment counter
        fps_timer += tmp;
        fps_counter++;
        This isn't really the greatest example, but I like to comment sections of code. The details can be read by looking through the code, with "hints" as needed (for example, reminding me that SDL_GetTicks() is the current time).

        Well commented blocks mean you can skim through the code, getting a quick overview of what's happening, without having to read in much detail.
        // find matching entry in hash table
        while (....
        .....
        .....
        }
        You don't have to care about the details of the look, you just know by the time you hit the } you've found what you wanted.

        Don't take this personally or anything. Your code is very readable.. there's just lots of extra comments (granted, I've seen worse) that are really just not worth putting in.

        (also, proper indenting is as, if not more, important.. of course, /. is butchering my indenting.. so please don't harass me about that because i couldn't agree with you more ;) )
        [ Parent ]
        • Re:Gotta document that code... by jbellis (Score:2) Tuesday April 26 2005, @11:39PM
        • Re:Gotta document that code... by Frans Faase (Score:2) Wednesday April 27 2005, @12:07AM
        • Re:Gotta document that code... (Score:4, Insightful)

          by ComputerSlicer23 (516509) on Wednesday April 27 2005, @12:38AM (#12355927)
          The concept I remember most from "Code Complete" is comments should tell you why or what are you doing something, but it shouldn't tell you how you are doing. The how should be obvious from the code.

          Never tell me you are adding shifting the varaible by 4 bits, instead tell me, you are converting from bytes to 16 byte blocks due to a chunks size conversion because that's the size the output device expects blocks to be written in.

          Don't tell me you are swapping variables, tell me why the values should be exchanged.

          Don't tell me you searching for the max value, tell me what the max value is used for when you find it. That sort of concept.

          One easy way to do that, is to look at code and the comments. If the code and the comments could get out of sync because you changed the implementation, the comment is wrong. You documented the how not the what.

          There are cases where the how is important, especially when there are things where the "how" doesn't behave naturally. Oh, this sorts numbers, but it is intentionally sorting the ASCII values of the numbers when represented as a string. That'd be very useful to know. I'd expect a sort to work based on the binary values.

          The other rule I remember from code complete, and from "Writting Solid Code", was the concept of laying out the pseudo code that explained what you wanted to do at a high level. Then filling in between the comments with the implementation. It was a very good way to end up with documented code.

          Kirby

          [ Parent ]
        • Here's what my version would look like:
          //---
          // TODO: gracefully handle wrap-around of SDL_GetTicks()
          // not implemented yet because it only happens every 40 years
          //---
          tmp = last_update;
          last_update = SDL_GetTicks();
          tmp = last_update - tmp;
          fps_millis_timer += tmp;
          fps_counter++;
          My philosophy is that:
          1) production code is not a tutorial of the C(++) language. It's okay to assume the reader knows their stuff. A bad example:
          tmp = last_update; // tmp = last update time
          2) production code is not documentation on the APIs that it uses. APIs have their own documentation, the reader either is familiar with it, or can find documentation. A bad example:
          last_update = SDL_GetTicks(); // now
          If the API is only used in one file, then you could point at the documentation right where the header is included. If it's used on a project level, then it should be pointed to at the project documentation.
          3) comments are just as much about why you didn't do something a certain way as it is about why you did do something a certain way.
          4) the obvious; name your variables correctly. Instead of:
          fps_timer += tmp; // Increase our fps millisecond timer
          use
          fps_millis_timer += tmp;
          5) if you are implementing using a spec, somewhere in the top of the file describe the revision of the spec. Additional comments can describe the section of the spec that they relate to.
          For example:
          // Init GDT, see section 6.3.1
          virtual_machine.gdt.base = base;
          virtual_machine.gdt.limit = limit;
          6) NEVER put actual values in comments. For example:
          com1.baudrate = 9600; //set baudrate to 9600
          The code already shows the actual value, and all too often the comment doesn't get updated.
          Sometimes it's okay to list the possible values:
          out_p16_d8( base + CSR, 0x66 ); //0x61 = 1x, 0x62 = 2x, 0x66 = 4x, other val illegal
          but I only prefer that when this is the _only_ place where it is used. Otherwise you'd use enums and/or refer to the documentation section/page
          7) write top level documentation to explain what the software is actually supposed to do. It's amazing how often this is missing. Write one bloody page that describes to a programmer that doesn't know ANYTHING about the software what the hell it's supposed to do, and how it's accomplishing this. It also helps tremendously if every file has a little description in the header. And I'm not talking about what license the code is distributed under.
          [ Parent ]
        • Re:Gotta document that code... by ArcadeNut (Score:2) Wednesday April 27 2005, @02:00AM
          • 1 reply beneath your current threshold.
        • Re:Gotta document that code... by conradp (Score:2) Wednesday April 27 2005, @03:57AM
        • Re:Gotta document that code... by ajs (Score:3) Wednesday April 27 2005, @07:59AM
        • Re:Gotta document that code... by dcam (Score:2) Wednesday April 27 2005, @09:32AM
        • And now without comments: by Chemisor (Score:2) Wednesday April 27 2005, @10:18AM
      • Re:Gotta document that code... by Abcd1234 (Score:2) Tuesday April 26 2005, @11:06PM
      • Re:Gotta document that code... by crazyphilman (Score:3) Wednesday April 27 2005, @12:46AM
      • Re:Gotta document that code... by PDAllen (Score:1) Wednesday April 27 2005, @04:26AM
      • Re:Gotta document that code... by Trinition (Score:2) Wednesday April 27 2005, @05:22AM
        • 1 reply beneath your current threshold.
      • Re:Gotta document that code... by jdowland (Score:1) Wednesday April 27 2005, @06:00AM
      • tmp is a curse word by DennisInDallas (Score:1) Wednesday April 27 2005, @12:57PM
    • Re:Gotta document that code... by Xyrus (Score:3) Tuesday April 26 2005, @09:35PM
    • Re:Gotta document that code... (Score:5, Insightful)

      by snorklewacker (836663) on Tuesday April 26 2005, @09:36PM (#12354641)
      You are. No question.

      Hello. I question. Look, if you need comments to explain the code flow because your method spans four screens and has six levels of conditionals in two levels of loops, all the comments in the world won't help you.

      Especially if you change the code and now the comments are wrong

      I have had the joy of maintaining lovingly commented code with all these huge blocks at the start showing what args get passed and what happens, and I can't understand a god damned thing about what it's doing, because the code all looks like
      (void *((ebuf->qs) > VRT_FBUF) ? etranf(&q) ...
      All generalisms are wrong.
      [ Parent ]
    • Re:Gotta document that code... (Score:5, Insightful)

      by rkcallaghan (858110) on Tuesday April 26 2005, @09:47PM (#12354721)
      Sooner or later, not documenting properly will bite them in the ass.

      Actually I have found that more often than not, not documenting properly only bites someone else in the ass.

      This is likely the source of the problem, and the least likely to change. I suppose it could be part of the QA process to check for notation on code, but I somehow suspect that with programming jobs on a one way trip to Bangalore that readability it secondary to "works cheap."

      ~Rebecca
      [ Parent ]
    • Re:Gotta document that code... by Stradivarius (Score:3) Tuesday April 26 2005, @09:58PM
    • Re:Gotta document that code... by skraps (Score:3) Tuesday April 26 2005, @10:20PM
      • Re:Gotta document that code... (Score:5, Interesting)

        comments can lie
        I worked with a programmer who disliked comments so much he'd remove them before looking at a function. Ok. So I wrote some code and he came to me and said "why do you have an empty else case?" I was puzzled, then realized that I'd written something like this:
        else
        {
        /* we don't have to do anything in the else case because of x y and z */
        }
        where x y and z would be non-obvious to anyone who wasn't fully immersed in this code. He's run it through a filter that removed all comments. He was a genius programmer - but wrote code that almost no one else could ever maintain. Tons of reliances on edge conditions without comment, reuse of generically-named variables (1 and 2 character names), tricky (but efficient) algorithms. So far as I know, I was the only one there ever to manage to really grok his code, and that required days of immersing myself.

        x = x++; // add one to x
        is obviously not useful.
        // Test FU_E (End bit) after FU_A/FU_B test! If there's a gap, do not consider
        // hitting the End bit a marker to stop - continue until we see another
        // packet/timestamp (in which case we return TRUE), or until we are
        // at the end of the buffer (in which case we return FALSE and keep
        // hoping to assemble it).
        if (((*curr)->GetPayload()[1] & FU_E_BIT) && !gap)
        break; // no error in fragment
        // if there's a gap we still won't return true unless
        // we find a non-fragment packet (or one from another fragment!)
        This is an example of a useful comment - and yes, it has to be maintained if the line of source were to change. I chose that at random; there are better examples - such as explaining what the edge cases are (especially if not handled), and under what circumstances they would become relevant, and how they could be dealt with then.

        Please excuse the incorrent indenting above; "<ecode>" doesn't work exactly like 'pre'

        [ Parent ]
        • Re:Gotta document that code... (Score:5, Insightful)

          by skraps (650379) on Tuesday April 26 2005, @11:50PM (#12355609)
          That comment is a good match for the code it is with. I never write comments like that unless dealing with a 3rd party interface, or something else that is a brick wall I can't refactor across, and there is something very quirky or unusual about it. The 'if' in the last snippet you quoted is very complex, and I would probably try to decompose it into a few functions and/or objects as necessary to make it clear what is happening. For example, you could start by replacing the 'if' with:
          if(message.is_complete())
          break;
          This clearly expresses the intent of the code you quoted (assuming I understood it correctly). Here, I've created a 'message' object to encapsulate some of the logic. The entire loop may be changed to look something like this:
          while(!message.is_complete())
          {
          packet_t packet=connection.get_next_packet();
          message.receive_packet(packet);
          }
          Now, the "is complete" decision making is abstracted to the "message", which seems to me the right place for that responsibility. Another thought is that the [1] should be changed. I'm guessing that you are referring to the second byte of the received packet, and using it as a flags field of some kind. If that is the case, why not go ahead and make a struct that represents the header layout, and reference the flags through a bitfield in the struct? If you do that,
          (*curr)->GetPayload()[1] & FU_E_BIT
          Becomes
          (*curr)->header->flag_end_of_message
          ...which seems clearer to me.

          You could continue along these lines, and get to a point where it would be obvious what the code does. The comment in that snippet also addressed "why", in addition to "how". I would move the "why" explanation into a document that defines the protocol.

          [ Parent ]
        • Re:Gotta document that code... (Score:5, Informative)

          by Jeremi (14640) on Tuesday April 26 2005, @11:51PM (#12355617)
          (http://www.lcscanada.com/jaf)
          x = x++; // add one to x


          is obviously not useful


          Not only is the comment not useful, it's factually incorrect... the behaviour of that line of code is undefined [msdn.com].

          [ Parent ]
        • Re:Gotta document that code... by markandrew (Score:1) Wednesday April 27 2005, @03:38AM
        • Genius programmer by StrawberryFrog (Score:2) Wednesday April 27 2005, @04:55AM
        • Re:Gotta document that code... by Chris_Jefferson (Score:2) Wednesday April 27 2005, @06:21AM
        • Re:Gotta document that code... by Flyboy Connor (Score:2) Wednesday April 27 2005, @08:06AM
        • Re:Gotta document that code... by jesup (Score:2) Wednesday April 27 2005, @12:16PM
        • 3 replies beneath your current threshold.
      • Re:Gotta document that code... by Rasta Prefect (Score:2) Tuesday April 26 2005, @11:56PM
      • Re:Gotta document that code... by Thangodin (Score:2) Tuesday April 26 2005, @11:57PM
      • Depends on the language. by Richard Steiner (Score:2) Wednesday April 27 2005, @10:15AM
    • Re:Gotta document that code... by Meetch (Score:2) Tuesday April 26 2005, @11:04PM
    • Re:Gotta document that code... by alanic (Score:1) Wednesday April 27 2005, @12:11AM
    • Manager's Riposte by 4of12 (Score:2) Wednesday April 27 2005, @07:36AM
    • 4 replies beneath your current threshold.
  • Use long variable names (Score:3, Funny)

    by Joey7F (307495) on Tuesday April 26 2005, @09:13PM (#12354439)
    (http://compustore.com/ | Last Journal: Thursday May 12 2005, @08:06PM)
    Just go ahead and use the long names, that alone will reduce documentation/commenting

    --Joey
  • Comments more important? (Score:5, Funny)

    by Rollie Hawk (831376) on Tuesday April 26 2005, @09:14PM (#12354441)
    (http://searchthefreakingweb.com/)
    Has he read the ones here?
  • huh, just like /. (Score:5, Funny)

    by eobanb (823187) on Tuesday April 26 2005, @09:14PM (#12354442)
    (http://eoban.com/)
    ...where comments are more important than articles.
  • Perl POD Documentation (Score:3, Interesting)

    How about Perl's POD Documentation? I do a lot of hacking of Matt Simerson [simerson.net]'s Mail::Toaster [tnpi.biz] and Nictool [nictool.com] projects, and I find that the Perl POD Documentation system, combined with well-named variables is easy on the eyes, and leads to it being well interpreted by an outsider.
  • Comment. (Score:3, Insightful)

    by stealth.c (724419) on Tuesday April 26 2005, @09:16PM (#12354454)
    Always comment. Always. I'm not a career coder, but I've done enough to know that if it's a project of any noticeable size at all, and you intend to read the code later, take the extra few seconds and COMMENT THE STUPID THING. It makes life a lot easier. The only thing that probably doesn't need commenting is a simple BAT file or a shell script. Comment!
    • Re:Comment. by Fjornir (Score:1) Tuesday April 26 2005, @09:27PM
    • Re:Comment. by tsm_sf (Score:2) Tuesday April 26 2005, @10:02PM
    • Re:Comment. by skraps (Score:2) Tuesday April 26 2005, @10:40PM
      • Re:Comment. by stealth.c (Score:2) Wednesday April 27 2005, @12:49AM
    • Re:Comment. by Brandybuck (Score:2) Tuesday April 26 2005, @11:12PM
    • Re:Comment. by Malc (Score:2) Tuesday April 26 2005, @11:18PM
    • Comments are for sissies by js3 (Score:2) Wednesday April 27 2005, @12:06AM
    • Re:Comment. by marcosdumay (Score:1) Wednesday April 27 2005, @10:07AM
    • Re:Comment. by Boronx (Score:2) Thursday April 28 2005, @02:28AM
  • Opinion... by duhasteifersucht (Score:1) Tuesday April 26 2005, @09:16PM
    • Re:Opinion... by LoztInSpace (Score:1) Tuesday April 26 2005, @09:22PM
      • Re:Opinion... by duhasteifersucht (Score:1) Tuesday April 26 2005, @09:25PM
    • Re: collapse/expand? by SolitaryMan (Score:1) Wednesday April 27 2005, @12:56AM
    • Re:Opinion... by EEBaum (Score:2) Wednesday April 27 2005, @01:28AM
    • Re:Opinion... by GileadGreene (Score:2) Tuesday April 26 2005, @09:51PM
      • Re:Opinion... by BasilBrush (Score:2) Tuesday April 26 2005, @10:35PM
        • Re:Opinion... by GileadGreene (Score:2) Tuesday April 26 2005, @10:44PM
          • Re:Opinion... by BasilBrush (Score:2) Tuesday April 26 2005, @11:48PM
            • Re:Opinion... by GileadGreene (Score:2) Wednesday April 27 2005, @12:51AM
    • 1 reply beneath your current threshold.
  • Switch from C by python_kiss (Score:1) Tuesday April 26 2005, @09:16PM
  • development process by phloydphreak (Score:1) Tuesday April 26 2005, @09:17PM
  • Comments are valuable, but good code essential by Eternally optimistic (Score:1) Tuesday April 26 2005, @09:17PM
  • And this, my friends, is why offshore outsourcing by melted (Score:1) Tuesday April 26 2005, @09:17PM
  • by John Seminal (698722) on Tuesday April 26 2005, @09:18PM (#12354477)
    (Last Journal: Saturday February 21 2004, @08:07PM)
    that using short, descriptive variable names 'should' be enough as long as the code is well-organized.

    This works for code I write that nobody else will ever maintain. Even then I can get tripped up, I'll have to lean back in my chair and try to remember what I was thinking when I wrote the code.

    But if you write code you're getting paid for, or code for an organization, anything but personal stuff, write good comments. Variable names might give a good idea about what data the variable holds, but it does not tell us much about how it is used.

    When I took my first programming class, the most frustrating part was the documentation. I thought it was retarded and stupid and a waste of time. But now I realize it is very important once you write something more significant than "Hello World".

  • Riiiiight... by Etherwalk (Score:1) Tuesday April 26 2005, @09:19PM
  • Not necessarily comments... (Score:4, Interesting)

    by kwoo (641864) <kjwcode AT gmail DOT com> on Tuesday April 26 2005, @09:19PM (#12354488)
    (http://www.kjwcode.com/ | Last Journal: Sunday April 03 2005, @12:28AM)

    In my opinion, comments are useful -- but literate programming is where it's at if you're looking for the best way to document your code.

    Knuth did a lot of work in the area -- if I remember correctly, all of the sources to TeX are written in a style understood by the "web" literate programming tool.

    There was also a good article by one of the Perl folks (Nathan Torkington? M.J. Dominus? Chromatic? I can't remember.) on POD, and how although POD wasn't literate programming, it was still useful. That article was great in that it showed a middle ground that may be more palatable to your non-LP-fanatic programmer.

    That being said, I prefer full-on LP for large projects.

  • Something i notice (Score:5, Insightful)

    by bmajik (96670) <matt@mattevans.org> on Tuesday April 26 2005, @09:19PM (#12354490)
    (http://www.mattevans.org/ | Last Journal: Wednesday April 20 2005, @01:11AM)
    i do a lot of code reviews at work and nothing makes me happier than good comments.

    but just putting a bunch of blocks of comments that are like "get customer", "build record", etc are basically useless. If you use programming by intent then its more or less obvious that the code

    GetCustomerFromDatabase(foo)

    is going to do something with a database and get a customer. a comment telling me that is useless.

    what i like to do is write a few paragraphs of text at the top of a function. it explains my general approach, why im doing certain things the way i am, why im not doing other things, and why the function even exists.

    essentially the comments should be enough that anyone that knew the problem space ought to be able to read them and come up with more or less a similar implementation.

    then, in the body of the method anytime i do something that i feel isn't completely obvious, i put a 1 or 2 liner infront of, i.e. "we have to do this because zergs are sensitive"

    the end result of all of this is that code reviews can see what you were thinking at the time the code was written.. and you're documenting assumptions about the problem, the apis, your understanding of the language, etc, all right in the code. it makes finding errors pretty easy.. someone that can't even read source code can read the comments and get an idea of the correctness of your approach.

  • Doxygen by Umbral Blot (Score:2) Tuesday April 26 2005, @09:20PM
    • Re:Doxygen by p3d0 (Score:2) Tuesday April 26 2005, @09:47PM
    • Re:Doxygen by 2short (Score:2) Tuesday April 26 2005, @10:31PM
  • A rule of thumb... by pdbogen (Score:2) Tuesday April 26 2005, @09:21PM
  • My thoughts.. by LordZardoz (Score:2) Tuesday April 26 2005, @09:22PM
  • dont be lazy by mrterrysilver (Score:1) Tuesday April 26 2005, @09:23PM
  • it depends by joel2600 (Score:1) Tuesday April 26 2005, @09:23PM
    • Re:it depends by swimmar132 (Score:2) Tuesday April 26 2005, @11:12PM
  • Are you writing COBOL? by jackb_guppy (Score:2) Tuesday April 26 2005, @09:23PM
  • Middle ground anyone? by Transcendent (Score:2) Tuesday April 26 2005, @09:24PM
  • Joel on Software (Score:5, Interesting)

    by TrueJim (107565) on Tuesday April 26 2005, @09:24PM (#12354535)
    (http://www.myspace.com/truejim2)
    "There's a subtle reason that programmers always want to throw away the code and start over. The reason is that they think the old code is a mess. And here is the interesting observation: they are probably wrong. The reason that they think the old code is a mess is because of a cardinal, fundamental law of programming:
    It's harder to read code than to write it."

    From Joel on Software
    http://www.joelonsoftware.com/articles/fog00000000 69.html [joelonsoftware.com]

    Always comment.
  • What are programming languages for? by FhnuZoag (Score:2) Tuesday April 26 2005, @09:24PM
  • You're Right by B4D BE4T (Score:1) Tuesday April 26 2005, @09:24PM
  • I don't typically comment code... by i.r.id10t (Score:2) Tuesday April 26 2005, @09:24PM
  • If the comments are clear, a better programmer than you can come along later and say "What the hell was this guy doing?" and then replace your lines of fumbling crap with much cleaner/clearer code.

    It's the difference between being able to see what you were trying to do vs. figuring out what you actually did.

    Call it "Intent Oriented Programming" if you want.

  • It depends on who is reading it by peldrake (Score:1) Tuesday April 26 2005, @09:25PM
  • They are (Score:4, Insightful)

    by MarkusQ (450076) on Tuesday April 26 2005, @09:25PM (#12354549)
    (Last Journal: Friday January 19 2007, @04:54PM)

    Comments are a maintenence nightmare. They get out of sync with the code, and (especially when the code is bad to begin with) people read them instead of the code.

    That means over time the human's understanding of what the program does starts to diverge from the computer's understanding.

    This is not good.

    If something is too hard to understand the way it is written without comments, it should be rewritten. You will save time in the long run, trust me.

    Remember the old adage: Don't get suckered in by the comments--the bug is in the code.

    --MarkusQ

    • Right on! by romango (Score:1) Tuesday April 26 2005, @09:53PM
      • Re:Right on! by MarkusQ (Score:2) Tuesday April 26 2005, @11:12PM
  • Context by spamspamspamspam (Score:1) Tuesday April 26 2005, @09:25PM
  • Uhh... (Score:5, Funny)

    by PrimeWaveZ (513534) on Tuesday April 26 2005, @09:26PM (#12354557)
    (http://www.destination-life.com/)
    UsngShrtCmtsIsOftNotEnghAsOneMyNdToReWrtShtInTheFt r.
    • Re:Uhh... by Soul-Burn666 (Score:2) Wednesday April 27 2005, @06:55AM
  • The problem isn't lack of comments (Score:5, Insightful)

    by jjoyce (4103) on Tuesday April 26 2005, @09:26PM (#12354559)
    The problem is usually functions that are too long and are not orthogonal. Write short, orthogonal functions and you'll see your need for heavy commenting go away along with the need for long variable names.
  • Comment first, code later by Knetzar (Score:2) Tuesday April 26 2005, @09:27PM
  • Quality of comments by SamMichaels (Score:2) Tuesday April 26 2005, @09:27PM
  • Short, descriptive variable names Vs... by iKaz (Score:1) Tuesday April 26 2005, @09:27PM
  • The Trouble with Comments... by Who Man (Score:1) Tuesday April 26 2005, @09:27PM
  • He's right by fallendove (Score:1) Tuesday April 26 2005, @09:27PM
  • Raskin (Score:5, Informative)

    by kebes (861706) on Tuesday April 26 2005, @09:27PM (#12354575)
    (Last Journal: Monday January 08 2007, @02:45PM)
    JEF RASKIN, professor of computer science at the University of Chicago, is best known for his book, The Humane Interface (Addison-Wesley, 2000), and for having created the Macintosh project at Apple. He holds many interface patents, consults for companies around the world, and is often called upon as a speaker at conferences, seminars, and universities. His current project, The Humane Environment (http://humane.sourceforge.net/home/index.html), is attracting interest in both the computer science and business worlds.

    For those who don't know (which apparently includes whoever is in charge of the linked article), Jef Raskin passed away this february. You can view the official press release, [raskincenter.org] or read more about [wikipedia.org] his contributions to computer science. I don't know when the article was written, but it seems it should mention that Raskin has passed away. In any case, his advice about commenting is good, just as his advice on user-interface design has always been lucid and helpful.
    • Re:Raskin by Profane MuthaFucka (Score:1) Tuesday April 26 2005, @10:46PM
    • Re:Raskin by bjb (Score:1) Wednesday April 27 2005, @12:20PM
    • 1 reply beneath your current threshold.
  • Comments as entertainment by syynnapse (Score:1) Tuesday April 26 2005, @09:28PM
  • Jef Raskin by veg_all (Score:2) Tuesday April 26 2005, @09:29PM
  • Indeed (Score:5, Funny)

    by screwballicus (313964) on Tuesday April 26 2005, @09:29PM (#12354591)
    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.
    • Re:Indeed by el-spectre (Score:2) Tuesday April 26 2005, @10:25PM
      • 1 reply beneath your current threshold.
    • Re:Indeed by Soul-Burn666 (Score:2) Wednesday April 27 2005, @07:07AM
    • 1 reply beneath your current threshold.
  • code first, then comment by KalvinB (Score:2) Tuesday April 26 2005, @09:30PM
  • BS by isny (Score:2) Tuesday April 26 2005, @09:31PM
  • There is a middle ground. by Some Random Username (Score:1) Tuesday April 26 2005, @09:33PM
  • Variable Names by slobber (Score:1) Tuesday April 26 2005, @09:35PM
  • You're both wrong. by ph4s3 (Score:1) Tuesday April 26 2005, @09:36PM
  • My opinion by karvind (Score:1) Tuesday April 26 2005, @09:36PM
  • Well, duh by QuantumG (Score:2) Tuesday April 26 2005, @09:37PM
    • Re:Well, duh by shmlco (Score:1) Tuesday April 26 2005, @11:04PM
      • Re:Well, duh by shmlco (Score:1) Tuesday April 26 2005, @11:07PM
  • You're both right. by Randolpho (Score:2) Tuesday April 26 2005, @09:38PM
  • Comments Should Reflect Intent (Score:5, Informative)

    by Bob9113 (14996) on Tuesday April 26 2005, @09:38PM (#12354659)
    (http://www.traxel.com/)
    I found the following in some production code, which quickly and concisely demonstrates why many comments are highly questionable:
    /** Always returns true. */
    public boolean isMagilla() {
    return false;
    }
    That's the core problem with many comments, but it can be avoided. Comments are good when they state the intent or business case for a block of code, acting as a guide to the meaning of a block for subsequent developers. They are bad when they profess to know the actual outcome or implementation of a block; only the code itself can accurately reflect the state of the code.

    The above would be far more useful like this:
    /** Tells whether this instance meets the magilla criteria. */
    public boolean isMagilla() {
    return false; // currently, no MyClass meets the magilla criteria.
    }
    Now the intent of the method is clear, and anyone coming along who wonders why it's hard coded will know under what circumstances they should change it to a formula (namely, if MyClass becomes capable of meeting the Magilla criteria).

    Comments can be good, but they should always be a guidepost to the intent of a block of code, and not attempt to explain how the code achieves that goal.
  • Literate code by GileadGreene (Score:2) Tuesday April 26 2005, @09:39PM
  • Comment on intention not implementation by BbMaj7 (Score:1) Tuesday April 26 2005, @09:39PM
  • Develop realistic habits by Dominic_Mazzoni (Score:2) Tuesday April 26 2005, @09:39PM
  • Code more important than comments. by neurojab (Score:2) Tuesday April 26 2005, @09:41PM
  • *Unit tests* not comments by Anonymous Coward (Score:1) Tuesday April 26 2005, @09:42PM
  • Code != Design Document by saddino (Score:2) Tuesday April 26 2005, @09:42PM
  • by bunratty (545641) on Tuesday April 26 2005, @09:43PM (#12354692)
    My basic rule of thumb about comments is to comment the interface. In C/C++ this would mean writing the comments at the beginning of all function declarations in .h files instead of .c files, and in Java would mean writing Javadoc comments at the beginning of all methods. The idea is that if you know what a function/method does, you should be able to understand how it does it. The inside of functions/methods should have minimal comments, usually just a one-line comment for each major section explaining what that section does. Sometimes how the function/method works needs to be commented, as in:
    void sort(int arr[]); // sort arr, ascending
    ...
    // Uses quicksort
    void sort(int arr[]) { ... }

    Of course, there are always exceptions. When I was writing low-level code that manipulated hardware registers, I wrote a multi-line comment before each line of bit-fiddling code, complete with what the code did and a cross-reference into the hardware manual. Something like:

    // Set the serial port to big-endian mode
    // See SlurpSCC manual page 3-5
    bitset(slurp->serial, bit(13));
  • More comments... please!! by redsilo (Score:1) Tuesday April 26 2005, @09:44PM
  • As invaluable as good directions by squared99 (Score:1) Tuesday April 26 2005, @09:45PM
  • Document at Design Level! by Rew190 (Score:2) Tuesday April 26 2005, @09:45PM
  • Comments Should Come From Good Design by GaryPatterson (Score:2) Tuesday April 26 2005, @09:46PM
    • 1 reply beneath your current threshold.
  • Self-documenting code is harder than commenting by SnprBoB86 (Score:2) Tuesday April 26 2005, @09:46PM
  • Debug code not comments (Score:5, Insightful)

    I find that for simple code that requires little thought, little in the way of comments is required. Sometimes comments just get in the way as the code progresses over time, and the comments don't. It's better to code with long descriptive variable names, and structures then depend on comments.

    For example which would you rather read.

    #define AgentNameLen 41
    #define CustomerNameLen 51

    struct CustomerID
    {
    int CustomerID;
    char AgentName[AgentNameLen];
    char CustomerName[CustomerNameLen];
    };

    or

    struct CoID //Customer ID structure
    {
    int ID; // ID number for the customer
    char AName[41]; // Agent Name
    char CName[51]; // Customer Name
    };

    To me the first is much more clear, and throughout the code will be obvious as to it's use. The second is mildly clear, but will degrade as new things get added to the structure.
    The first also allows things like
    for(int i=0;iAgentNameLen;i++)
    which makes it very clear that you are iterating throught the agent name.

    Algorithm's should be documented, as much as possible, at the top of the function, and any function that takes more then a screen should be looked at to see if it can be broken up into smaller functions.

    (Almost) Always code towards maintainability, never speed. Usually it pays off within the year.

    So to sum up. In order of importance:
    80% code clearly with an eye towards maintainablity
    20% comment

  • If I may... by ecko3437 (Score:1) Tuesday April 26 2005, @09:47PM
  • Depends on who is reading by redelm (Score:2) Tuesday April 26 2005, @09:49PM
  • Comments in English are good. by Penguin Programmer (Score:1) Tuesday April 26 2005, @09:54PM
  • Homegrown by aslsCave (Score:1) Tuesday April 26 2005, @09:56PM
  • The What, the Why, and the How (Score:3, Insightful)

    There are three things the poor maintainance programmer who has to maintain your code needs to know: The What, the Why, and the How.

    The What: What is this code trying to do. This is where your design documents come in, as they tell what the overall goal of the program is.

    The Why: Why are you doing what you are doing? Why are you writing to this hardware register twice? Why do you divide this value by 1800 here? This is where comments are needed - to explain that the hardware sometimes doesn't take the value on the read, or that the nominal deviation of this signal is 1800 Hz.

    The How: How are you getting the work done. This is where well written code with well chosen variable names comes in.
  • Verbosity by man_ls (Score:2) Tuesday April 26 2005, @09:59PM
  • The best comment is no comment by Anonymous Coward (Score:1) Tuesday April 26 2005, @09:59PM
  • Code Comments by onesadcookie (Score:1) Tuesday April 26 2005, @10:00PM
  • The only thing worse is old comments by subStance (Score:2) Tuesday April 26 2005, @10:01PM
  • Quality not quantity (Score:5, Interesting)

    by MagikSlinger (259969) on Tuesday April 26 2005, @10:03PM (#12354828)
    (http://movieotaku.wordpress.com/ | Last Journal: Friday March 30 2007, @12:56AM)

    Having RTFA, I can see what he's trying to get at, but as someone who has (unfortunately) found himself spending most of my 10 year career in programming cleaning up other people's poop. At first I thought it was because I must have done something wrong that I kept ending up being assigned this work, but as I came to realise, it was because I make the code better than I found it and I have a knack for fixing stuff other people give up on. I also had silly managers who assign work to the people least qualified to do it.

    At any rate, some observations:

    1. 20 lines of comments "documenting" your code before you write it (or even after you write it) is far less useful than writing the code COHERENTLY and CORRECTLY in the first place.

    Last month, I had a 1200 (yes 1,200) line method with huge blocks of documentation before big pieces of code. I still can't quite tell you what it thought it was doing. The code was a for loop wrapping around code to handle 3 different and mutually exclusive situations. Instead of identifying which of the 3 situations it was and creating a method for each situation, the person just stuffed it all in with lots of comments documenting everything the article's author said. The code was still unmaintainable.

    2. Comments are useless unless they are kept up to date

    Part of the reason that code was so difficult to figure out was because most of those big verbose documentation comments referred to a completely different implementation. After the programmer had written the first case, she encountered some other bad cases and eventually had to completely change a block of code embedded in this 1200 line for-loop. The code was now correct, but the comments no longer had anything to do with that block of code.

    3. Don't be clever when you can be clear

    I have made a solemn vow to hunt down and hurt anyone who puts "clever" code in my project. I am so sick of trying to figure out what some obfuscated piece of code in C, C++ or even SmallTalk is doing. And find out it was just a "clever" way of doing something pretty straight forward like iterating over a list. There was no speed gain from the clever trick, and the code wasn't even a bottleneck to begin with. *sigh*

    4. If you don't know how to solve the problem, write some experiment code in a separate app to figure it out. Then take the time to do the "right" thing in the production code.

    3 days from final for a video game. The CD streaming library for the Sony Playstation was making this strange "hic-up" sound at rare moments. By this time, the original author of this code has long since gone to another company. So I plunge into the code and found that the original programmer didn't know how to write streaming code so he created this hack of a hack of a hack of a test (ad nauseum). The code was programmed by accident, not design. No amount of comment before coding could help this. If the author had dumped the code, wrote documentation describing everything he learned then wrote the code, things would have been a lot better.

    5. Unrelated to comments, but use variable names that make sense. Don't name them arbitrarily or to amuse yourself!

    That CD sound streamer code I mentioned above used quirky names for variables. Can you tell what "little_ninja" is supposed to be just from the name? When I confronted the coder about this quirk of his (in another library he wrote), he got all huffy and didn't understand why people didn't appreciate his little puzzles or his sense of humor. It galls me he still earns a paycheck in the industry.

  • plausible deniability? by whysanity (Score:2) Tuesday April 26 2005, @10:03PM
  • by francisew (611090) on Tuesday April 26 2005, @10:03PM (#12354838)
    (http://www.esmonde-white.com/)

    I kid you not, this is real code my supervisor writes.

    if(preproct(7) >=thres), for j=size(at,2),at(:,j)=at(:,j)-maa;,for i=1:size(at,1),at(i,j)=(at(i,j))/(stda(i)+.0000000 01);,end,end,end

    Note that this is matlab code, where commas are both an end-of-statement indicator (it's also possible to use just a semicolon), and an array index separator. The nice thing about this code, is that I can at least guess where most of the variable names come from. Oh, and there was *no* line break in the original code. Hooray for the avoidance of those wasteful '/n' characters?!?!

    To answer the original poster: yes, comments are of vital importance.

    Then again, if a program is structured right, things can be organized into sections, each of which is then commented, as opposed to a bunch of seemingly random lines with comments spaced throughout. Sometimes the layout of code, in conjunction with good variable names, is the best possible method of commenting it. The one thing comments are good for is to assure that someone not familiar with the particular language being used, will still understand the purpose of the code.

  • Ideally... by xelph (Score:1) Tuesday April 26 2005, @10:06PM
  • Speak, Memory! (Score:3, Interesting)

    by guet (525509) on Tuesday April 26 2005, @10:07PM (#12354857)
    program.files.each do | class |

    class.methods.sort.each do | method |

    5.times { method.refactor! unless method.elegant? }

    end

    if problem_domain > current_language
    choices << comp.lang.each
    current_language = choices.best
    project.restart
    evangelise(current_language)
    end

    end

    comments << intentions.remove(implimentation_details)
    puts comments

    def refactor!
    method.split! unless method.size < Too_Big
    method.rename! unless method.name.clear?
    end

    def evangelise(lang)
    slashdot.comment.post
    puts "#{lang} is the only real language"
    end

    goto (1.0/0.0) and $beyond

    http://www.rubygarden.org/ruby?MoviesTheRubyWay
  • An argument against comments by CrazyJim1 (Score:2) Tuesday April 26 2005, @10:09PM
  • Literate Programming ... rejected 1500 to 1 by sidles (Score:1) Tuesday April 26 2005, @10:09PM
  • Other ideas for making code easy to read by Error27 (Score:2) Tuesday April 26 2005, @10:14PM
  • You're right, but Raskin is wrong (Score:3, Informative)

    by Univac_1004 (643570) on Tuesday April 26 2005, @10:14PM (#12354905)
    (Last Journal: Thursday March 31 2005, @02:40PM)
    Raskin (who did not write code himself, but was more of a essayist) was a devote of "Literate Programming", first promulgated by Knuth: http://www.literateprogramming.com/knuthweb.pdf/ [literateprogramming.com]

    In "Literate Programming" the comments are all important and the code itself is trivialized. The code, as Jef told me, is like "raisons in the muffin of the comments." There are paragraphs of verbiage which might go on about the history of the project, why certain features were discarded, etc., etc, and might not even explain what the following line or two of code was concerned with.

    It's really very difficult to deal with code that has been written in this style ("literatized?" ) since the actual structure of the program is severly obscured. It serves as an example of how overdoing a good thing is usually a bad thing.

    Jef was a nice guy, and I recommend his book, "The Humane Interface" for its many interesting ideas. His attempt to put them into practice in the Archy project http://rchi.raskincenter.org/aboutrchi/index.php/ [raskincenter.org], was not completed before his death. Even for that, Archy is very close to his vision.

    But since Jef was in many ways an extremist, one can demonstrate in Archy his pushing of his concepts to the limit resulted in the end fully maching his goals. Somewhat like "Literate Programming", in fact.

  • You need both by Bilx777 (Score:2) Tuesday April 26 2005, @10:14PM
  • I'd say both by Tamerlan (Score:1) Tuesday April 26 2005, @10:18PM
  • There will always be losers (Score:5, Interesting)

    by menace3society (768451) on Tuesday April 26 2005, @10:18PM (#12354929)
    There will always be loser "programmers" who write code without comments, or write code without useful comments, or modify usefully-commented code without modifying the comments. Everyone I've seen who's ever put up an example of how code is self-documenting and that comments are just extra text saying the same thing have fallen into the second category. Of course things like "prints a message" or "check to see if i==0" are stupid comments. But that's just a straw man if you want to say that all comments are useless (I dare these people to read uncommented assembly of more than about 40 lines, and tell me the code documents itself).

    The true useful skill lies in reading sloppy and/or wizardly code. Some people think that they have job security if they write impenetrable code, but then they can just be fired and all their code rewritten. If you can read others' "unmaintainable" code, you enable your employer to save money by not having to rewrite everything the guy they just fired wrote. So they'll want to keep you around as they fire/downsize everyone else. I It doesn't really matter what kind of code you write, since you can read whatever. advise everyone to start reading up on the Obfuscated C Contest, and practice figuring out what evertyhing does. Then you can handle any kind of code thrown at you, and the code you actually write becomes of secondary importance.

  • short, descriptive variable names by readin (Score:1) Tuesday April 26 2005, @10:19PM
  • Why I think it's difficult by Paradise Pete (Score:1) Tuesday April 26 2005, @10:19PM
  • Good comments are good (Score:3, Informative)

    by localman (111171) on Tuesday April 26 2005, @10:21PM (#12354947)
    (http://www.sophiafieldphotography.com/)
    using short, descriptive variable names 'should' be enough as long as the code is well-organized.

    Definitely not. No matter how clear your code is, it only gives you an idea of what is going on the small scale. You often need comments to describe your big-picture motivation. But the comments have to be good too.

    Here's a classic bad comment:
    // add one to the total
    ++total;
    That's useless. But a comment can help:
    // the total doesn't count the root node of the tree
    // (since there's no actual entry in the DB for node "0"),
    // so we just add that in here...
    ++total;
    No matter how clear you write the code you're not going to get that much understanding without a comment.

    It's just a simple example off the top of my head, but that's how I use comments. The code clarity is all that's needed to tell what you are doing, but comments can tell you why you are doing it. You need both for truly maintainable code.

    Cheers.
  • Who's right? by KidSock (Score:2) Tuesday April 26 2005, @10:22PM
  • I learned the hard way (Score:5, Interesting)

    by amigabill (146897) on Tuesday April 26 2005, @10:24PM (#12354975)
    My learning experience about commenting code was a difficult one. Like many, while in college I wrote the code and then went back to comment it so the profs were happy.

    Then I did a co-op with an automated storage/retreival systems company in their software department. One of the processes involved in a communications system needed some work. The code was licensed from another company in another country. There was no documentation for this communications system. There was very little commenting in the code. Luckily it wasn't in a foreign language. Unluckily it was wrong, apparently the structure of this program was similar to that of another, which was mostly gutted and rewritten, but a few old-program comments survived to be the ONLY comments in the new program.

    Sure, the sources could be reverse engineered to provide the documentation required. I did it. It took a few weeks.

    After that, I didn't leave comments for last anymore. It's been a good thing. I now work for a semiconductor design company and often write perl scripts or skill-language scripts to automate tedious tasks. I think I'm abou thte only one in the office that comments such scripts in any way. It's nice to read what stuff does when I have to revisit code many months or years later. I hate having to revisit someone else's code because it's nearly guaranteed to be completely barren of anything human-readable.

    Listen up kids! Commenting is GOOD! Your professors aren't just being jerks. Learn the easy way and hopefully save yourself a great deal of trouble with your own code. Other people's code will always suck, but your own shouldn't have to.
  • Your colleagues by Jesus 2.0 (Score:1) Tuesday April 26 2005, @10:27PM
  • how to write unmaintainable code by krunk4ever (Score:2) Tuesday April 26 2005, @10:29PM
  • Xtreme Programming by thisisauniqueid (Score:1) Tuesday April 26 2005, @10:31PM
  • Objective-C by HitByASquirrel (Score:1) Tuesday April 26 2005, @10:37PM
  • Code comments by cparisi (Score:1) Tuesday April 26 2005, @10:38PM
  • I have to wonder... by NoMoreNicksLeft (Score:2) Tuesday April 26 2005, @10:38PM
  • Well, lessee.... by CPNABEND (Score:1) Tuesday April 26 2005, @10:39PM
  • I remember a technique... (Score:3, Interesting)

    by aduzik (705453) on Tuesday April 26 2005, @10:39PM (#12355064)
    (http://nsblog.org/)
    whose name escapes me at the moment. But the idea is that you write comments that are like "psuedo-code" but much more English-like than ordinary psuedo-code. Then you refine the comments down further and further until you replace the lowest-level comments with individual statements, and the higher-level comments stay in as documentation. I remember trying this once and, while overkill for the simple project I was doing, I wrote some damn fine (and readable!) code that way.
  • i know how it feels by eLamer (Score:2) Tuesday April 26 2005, @10:39PM
  • Comments important but not TOO much! by Man in Spandex (Score:2) Tuesday April 26 2005, @10:40PM
  • The design is the code by rakeswell (Score:2) Tuesday April 26 2005, @10:42PM
  • Reviewing for Readability by Bob Munck (Score:1) Tuesday April 26 2005, @10:43PM
  • Best comment ever by Camel Pilot (Score:2) Tuesday April 26 2005, @10:45PM
  • Comments... by RickHunter (Score:2) Tuesday April 26 2005, @10:49PM
  • The acid test for comments and documentation by davidwr (Score:1) Tuesday April 26 2005, @10:50PM
  • Always comment logic by fizban (Score:2) Tuesday April 26 2005, @10:52PM
  • Nobody's 100% right by pg110404 (Score:2) Tuesday April 26 2005, @10:55PM
  • www.lysator.liu.se/c/pikestyle.html by justine_avalanche (Score:1) Tuesday April 26 2005, @10:58PM
  • Not only are you right... by Timbotronic (Score:2) Tuesday April 26 2005, @11:03PM
  • Hacking vs. coding professionally by dgmckay (Score:1) Tuesday April 26 2005, @11:05PM
  • No such thing as... by tius (Score:1) Tuesday April 26 2005, @11:08PM
  • You new fangled whipper snappers (Score:3, Insightful)

    by SpacePunk (17960) on Tuesday April 26 2005, @11:08PM (#12355323)
    (http://slashdot.org/)
    If it was hard to write, it should be hard to read.

  • Funny comments by dgmckay (Score:1) Tuesday April 26 2005, @11:18PM
  • 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."

  • Comments are better! by nrlightfoot (Score:2) Tuesday April 26 2005, @11:21PM
  • Everyone has an opinion by Kwirl (Score:1) Tuesday April 26 2005, @11:22PM
  • Code does shit by gmplague (Score:2) Tuesday April 26 2005, @11:24PM
    • 1 reply beneath your current threshold.
  • Literate Programming (with caution) (Score:4, Interesting)

    by os2fan (254461) on Tuesday April 26 2005, @11:25PM (#12355452)
    (http://www.geocities.com/os2fan2/index.html)

    I have written programs in both raw and literate programming style, i prefer the latter. In fact, i wrote a literate-program pre-processor to write programs, and it made the program easier to write and more bug-free.

    In literate programming, you rely on a pre-processor to make the output production, so you are free to put things together as you want. What this means, is that bracketing code (eg open, close files), can be written in the same block, which are invoked separately.

    The main program then ends up looking like a rough scetch, full of commands to include other bits. With wing comments, it is easy to see what is going on.

    /* A rexx script */
    !inc rexxsets ; standard settings
    !inc cmdopts ; process command line options
    !inc fileopen ; open files
    !inc mainprog; main program
    !inc closefile ; close files
    exit
    !inc subs ; subroutines

    One uses a folding editor to search for strings like "!topic". This will not show you a consolidated index, but you can use it to also show where you're are, and any missing bugs.

    On the main, Jon Bentley's comments on Literate Programming are fair (that is, it creates a good environment for writing single-purpose code), but one needs to consider the context the program is written for.

    The form i use was specifically designed to allow all sorts of text-output, so the same file can make as output, eg .CMD, .REX, .TXT and .HTM output, which means that when you run the script you get a perfectly matched set of files, all correctly pointing to each other.

  • PHP-Nuke by Cbs228 (Score:2) Tuesday April 26 2005, @11:28PM
  • Longhorn program comments by Gary Destruction (Score:2) Tuesday April 26 2005, @11:29PM
  • You need comments by aCapitalist (Score:2) Tuesday April 26 2005, @11:46PM
  • Necessary on low-level code and interfaces by wtarreau (Score:2) Tuesday April 26 2005, @11:46PM
  • Linus' Opinion by Brainix (Score:2) Tuesday April 26 2005, @11:49PM
  • Good Code vs. Comments by shelfc (Score:2) Tuesday April 26 2005, @11:54PM
  • Does not apply to *slashdot* comments..... by eh2o (Score:2) Tuesday April 26 2005, @11:55PM
  • English is far more expressive by BAH Humbug (Score:1) Tuesday April 26 2005, @11:58PM
  • The psychology of computer programming by aCapitalist (Score:2) Wednesday April 27 2005, @12:03AM
  • Comment the contract... by dbc (Score:2) Wednesday April 27 2005, @12:05AM
  • Doxygen and commented code by AhaIndia (Score:1) Wednesday April 27 2005, @12:07AM
  • Actual code block (Score:4, Interesting)

    by GrouchoMarx (153170) on Wednesday April 27 2005, @12:08AM (#12355754)
    (http://slashdot.org/)
    I love telling this story...

    Last year I had a brief stint at a small software company that had just taken a project in-house that was developed by an outside contractor. My job was to take the code they'd just inherited (which no one there knew anything about) and add some features to it on a tight schedule. Documentation? What documentation?

    The extent of all of the code comments it had was the following (and no, I'm not making this up):

    if(...) {
    break; // break
    }

    If that wasn't bad enough, I knew the original developer personally. She was a former professor of mine and I'd worked for her company only a few months before she had taken that contract.

    As someone who has had to deal with code with descriptive names and no comments or docs to go with them: If you write such code, may you rot in the lowest level of hell along with traitors, used car salesmen, and people who answer cell phones during movies.
  • Depends on the comments... by The_Incubator (Score:2) Wednesday April 27 2005, @12:11AM
  • After coding for 22 years... (Score:3, Insightful)

    by coolgeek (140561) on Wednesday April 27 2005, @12:21AM (#12355834)
    (http://slashdot.org/)
    I think it's safe to say comments are for the benefit of those who have not assimilated their language, their APIs, etc. Sure, documenting interfaces is essential as well as how certain data structures are used. Most of the other comments I have seen are completely superfluous and only enhance the understanding of the uninitiated.
  • After 22 years of coding... by coolgeek (Score:2) Wednesday April 27 2005, @12:29AM
  • Comments? Pshaw! (Score:3, Interesting)

    by MsWillow (17812) on Wednesday April 27 2005, @12:38AM (#12355930)
    (http://home.comcast.net/~jeannenospam/ | Last Journal: Thursday October 18, @08:15PM)
    Comments, at ***BEST***, tell you what the author of the comment *hopes* was going on, at the time it was written. The code that follows may well not do anything of the sort; it may have, at one point, only to have been re-written by somebody else at a later date; it may still do what was stated, though that's no longer what's needed.

    Yes, it takes longer, trying to grok uncommented spaghetti code, but doing so on a regular basis will help you develop a reputation for being able to fix any bug, anywhere, any time. It even allowed me to track down a bug in a now-defunct version of Borland C's optimizer, that produced incorrect code when two seemingly unrelated options were selected. There ain't *NO* amount of commenting in the source code that would have helped there.

    Learn to do your job right. Don't depend on what was written in any comments. The compiler ignores comments; so should you. However, as damned few programmers actually know how to debug other people's code, and many are too lazy to read it like a compiler, do add comments to your own code. It makes the stuff easier for PHBs to read. ;)

    For those who want to grok code the way it's executed, I'd suggest you start like I did, by reading old obfuscated C code winners, and running them in your head, comparing what you got with what the computer got. You'll burn up a lot of scatch paper, but eventually, you'll see what I mean.
  • Purpose of comments (Score:5, Insightful)

    by Todd Knarr (15451) on Wednesday April 27 2005, @12:42AM (#12355944)
    (http://www.silverglass.org/)

    I'd say you're right, comments are more important. Clearly-written code should make how it's doing things obvious, yes. Comments, though, should say what is being done and why it's being done the way it is.

  • My opinion. Others exist, but this one is mine. by rice_burners_suck (Score:2) Wednesday April 27 2005, @12:46AM
  • I think most interesting is that.... by xquark (Score:1) Wednesday April 27 2005, @12:52AM
  • Good code by planetoid (Score:1) Wednesday April 27 2005, @01:00AM
  • Role of Syntax Hilighting by EEBaum (Score:2) Wednesday April 27 2005, @01:09AM
  • Comments are more important than code... by halleluja (Score:1) Wednesday April 27 2005, @01:14AM
  • Write in plain, uncluttered English by EEBaum (Score:2) Wednesday April 27 2005, @01:22AM
  • Improving... by Vo0k (Score:2) Wednesday April 27 2005, @01:50AM
  • Hpw to write unmaintainable code... by emilv (Score:1) Wednesday April 27 2005, @01:51AM
  • (Lambda (Comment) ()) by TaleriaKNT (Score:1) Wednesday April 27 2005, @01:57AM
  • literate programming by The Shrubber (Score:2) Wednesday April 27 2005, @01:57AM
  • If you need comments, you REALLY need unit tests by bADlOGIN (Score:2) Wednesday April 27 2005, @02:05AM
  • I prefer not commented code.... by KiroDude (Score:1) Wednesday April 27 2005, @02:05AM
  • You are by Angst Badger (Score:2) Wednesday April 27 2005, @02:22AM
  • code = WHAT, comments = WHY (Score:3, Interesting)

    by Anonymous Coward on Wednesday April 27 2005, @02:24AM (#12356439)
    The code should be written so that it is obviously WHAT it does.

    The comment should explain WHY.

    Not that I use that convention myself, but I often wish that I did :-) Like when I go "ok, it's doing that... But WHY?", I know that I should have put in a comment. But if I go "WHAT the f**k is it doing here", those lines should be rewritten.
  • Source code has no value by mcrbids (Score:2) Wednesday April 27 2005, @02:39AM
  • You know it when you see it ... by Keeper (Score:2) Wednesday April 27 2005, @02:41AM
  • variable names are sooooooo easy by speedplane (Score:2) Wednesday April 27 2005, @02:50AM
  • Comments add information not in the code by apposite (Score:2) Wednesday April 27 2005, @02:59AM
  • Blind Markers by Ulky (Score:1) Wednesday April 27 2005, @03:01AM
  • Why not How by codexus (Score:2) Wednesday April 27 2005, @03:15AM
  • you only need by progr (Score:1) Wednesday April 27 2005, @03:35AM
  • Depends on your definition of "uneccessary" by BigTom (Score:2) Wednesday April 27 2005, @04:49AM
  • Document how things interact by oglueck (Score:1) Wednesday April 27 2005, @04:49AM
  • No, no, no, no, no.... by measterbrook (Score:1) Wednesday April 27 2005, @05:07AM
  • This is the worst kind of SE article there is by lskutt (Score:1) Wednesday April 27 2005, @05:15AM
  • by beforewisdom (729725) on Wednesday April 27 2005, @05:36AM (#12357148)
    The comments from the TAs about near useless comments was interesting.

    Getting people to do any comments is a conversation in itself.

    However, the TAs brought up a good point. It is not enough to put ANY comments in a file, you need to put GOOD comments in, but who teaches people what good commenting is?

    What works for me is
    - putting a small paragraph at the top of file
    explaining, in general, what the file is for
    ( sort of like labeling a jar with the preserves
    you just put into it )

    - putting a 1-2 line description above each
    non-trivial function

    - putting a sentence fragment above each
    significant or non intuitive ( coding around a
    quirk ) block of code

    I get compliments on how easy it is to understand and maintain my code( often resulting in me getting to asked to write more code - versus the folks who intentionally obscure things in the pursuit of job security ). I can also go back to code I wrote years ago and understand what it is going on.

    I put comments in as I write the code, mostly, so it doesn't feel like a burden.

    If anyone has any other good idea, please do tell, someone somewhere is likely to give it a try.
  • Both are right/wrong by Sindri (Score:1) Wednesday April 27 2005, @05:51AM
  • But are they? by newandyh-r (Score:1) Wednesday April 27 2005, @05:55AM
  • I was generating Smalltalk code from documentation by crovira (Score:2) Wednesday April 27 2005, @06:30AM
  • Author is correct by DukeLinux (Score:2) Wednesday April 27 2005, @06:35AM
  • // time commenting could be time coding by Pastis (Score:2) Wednesday April 27 2005, @06:58AM
  • Who's right? by Wormholio (Score:1) Wednesday April 27 2005, @06:59AM
  • Ban all comments and insist on readable code by RobWalker (Score:1) Wednesday April 27 2005, @07:26AM
  • Comments can't be more important than code by SavageSMC (Score:1) Wednesday April 27 2005, @07:35AM
  • From a tech writer by clawhound (Score:2) Wednesday April 27 2005, @07:54AM
  • Like this? by Mintee (Score:1) Wednesday April 27 2005, @07:57AM
  • Programming languages are languages - be literate by dazedNconfuzed (Score:2) Wednesday April 27 2005, @08:17AM
  • Self-documenting code...written *when*? by whitroth (Score:2) Wednesday April 27 2005, @08:26AM
  • code example by sonofagunn (Score:1) Wednesday April 27 2005, @08:30AM
  • Simple steps that should always be followed by n_clero (Score:1) Wednesday April 27 2005, @08:42AM
  • A good heuristic for how to comment by Dixie_Flatline (Score:2) Wednesday April 27 2005, @08:53AM
  • My favorite uncommented code of all time: by chad.koehler (Score:1) Wednesday April 27 2005, @08:54AM
  • well duh by AviLazar (Score:2) Wednesday April 27 2005, @08:57AM
  • You are right by mwood (Score:2) Wednesday April 27 2005, @09:04AM
  • Business Rules and minimal comments by Maxo-Texas (Score:1) Wednesday April 27 2005, @09:06AM
  • You are right by oogoody (Score:1) Wednesday April 27 2005, @09:07AM
  • Clean the code, Comment the subtle or exceptional by tz (Score:2) Wednesday April 27 2005, @09:27AM
  • Unit test and UML diagrams by $nickname_212 (Score:1) Wednesday April 27 2005, @09:41AM
  • I agree by robyannetta (Score:2) Wednesday April 27 2005, @09:42AM
  • Good Self-documenting code? by sirgoran (Score:2) Wednesday April 27 2005, @10:11AM
  • "nothing to do" comments by timster121 (Score:1) Wednesday April 27 2005, @10:15AM
  • new technologies getting it backwards by kisrael (Score:2) Wednesday April 27 2005, @10:31AM
  • Justify the code by Skapare (Score:1) Wednesday April 27 2005, @10:33AM
  • computer-generated HTML - a good thing by matt me (Score:1) Wednesday April 27 2005, @10:41AM
  • Anyone who thinks code can be self-documenting... by bwcbwc (Score:1) Wednesday April 27 2005, @10:42AM
  • My philosophy is by robertjw (Score:2) Wednesday April 27 2005, @10:58AM
  • Documentation is like sex... by gorodish (Score:1) Wednesday April 27 2005, @11:35AM
  • A Reason for commenting "Hello World" by jtpalinmajere (Score:1) Wednesday April 27 2005, @11:39AM
  • Comments don't execute by Symbiot (Score:1) Wednesday April 27 2005, @11:51AM
  • Comments don't execute by Symbiot (Score:2) Wednesday April 27 2005, @11:57AM
  • Write the comments first by DennisInDallas (Score:1) Wednesday April 27 2005, @01:29PM
  • What vs. Why by CarlBarnpipe (Score:1) Wednesday April 27 2005, @02:03PM
  • I have a simple principle by gidds (Score:2) Wednesday April 27 2005, @02:54PM
  • Future programmers? by HTH NE1 (Score:2) Wednesday April 27 2005, @05:07PM
  • Five Simple Rules for Commenting Code by WillAffleckUW (Score:1) Wednesday April 27 2005, @06:15PM
  • Comments by MacWiz (Score:1) Wednesday April 27 2005, @11:25PM
  • Version Control Commit Messages by khanyisa (Score:1) Thursday April 28 2005, @02:33AM
  • by Fjornir (516960) on Tuesday April 26 2005, @09:20PM (#12354498)
    "Don't get suckered in by the comments -- they can be terribly misleading. Debug only code."
    --Dave Storer
    [ Parent ]
  • Re:No such thing as too many comments... by Desult (Score:2) Tuesday April 26 2005, @09:29PM
  • Re:Documentation encourages complacency of thought by electricsheep7 (Score:1) Tuesday April 26 2005, @09:30PM
  • Re:Documentation encourages complacency of thought by CreateWindowEx (Score:2) Tuesday April 26 2005, @09:31PM
  • Re:What about TDD lessons? by aduzik (Score:2) Tuesday April 26 2005, @10:36PM
  • Re:There are 10 types of programmers by Anonymous Coward (Score:1) Wednesday April 27 2005, @03:05AM
  • Re:Commenting ion Assembly Coders by DennisInDallas (Score:1) Wednesday April 27 2005, @01:48PM
  • 50 replies beneath your current threshold.
(1) | 2 | 3