Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror
×
Programming Technology

Comments are More Important than Code 1021

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

Comments Filter:
  • Raskin (Score:5, Informative)

    by kebes ( 861706 ) on Tuesday April 26, 2005 @10:27PM (#12354575) Journal
    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.
  • by John Seminal ( 698722 ) on Tuesday April 26, 2005 @10:32PM (#12354608) Journal
    Why on earth do we even have commenting? I mean, we went through the whole programming language concept precisely to make instructions to computers human-readable. Ideally, commenting should be obsolete - the language and its syntax should make it obvious what needs to be done.

    If it turns out that after all that, our code still isn't intelligible, then isn't this some monumental failure of C, Java, BASIC and the rest, whose whole raison d'etre is to make weird things make sense?

    This is not algebra in high school where you lose points for not showing your work. This is the real world where an application might have 100's of lines of codes, maybe 1000's. It might be a bunch of classes in java working together. And then there is the logic, the work flow.

    Plus, many companies use peer review for raises and advancement. If your peers have a hard time understanding your code, it will hurt you later.

    Most people today don't work 30 years for one company. That means someone else will get stuck with your code.

    Now either you will give that person months to go through all the code, to try and figure out how it works, or you will have code with comments that will help him hit the ground running.

  • by Anonymous Coward on Tuesday April 26, 2005 @10:34PM (#12354623)
    From Knuth's page:

    The main idea is to regard a program as a communication to human beings rather than as a set of instructions to a computer. Your program is also viewed as a hypertext document, rather like the World Wide Web. (Indeed, Knuth used the word WEB for this purpose long before CERN grabbed it!)

    CWEB is a version of WEB for documenting C, C++, and Java programs. WEB was adapted to C by Silvio Levy in 1987, and since then both Knuth and Levy have revised and enhanced the system in many ways, notably to support C++ and ANSI C. Thus CWEB combines TeX with today's most widely used professional programming languages.

    look it up people...

  • by Bob9113 ( 14996 ) on Tuesday April 26, 2005 @10:38PM (#12354659) Homepage
    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.
  • by Anonymous Brave Guy ( 457657 ) on Tuesday April 26, 2005 @11:03PM (#12354834)
    I always prefer long variable names over short or abbreviated ones. The only exception is "i" and "j" for loop counters.

    I take it you don't write much maths code, then? ;-)

    There's a reason mathematicians don't write this:

    coord2 = gradient * coord1 + offset

    and they write this instead:

    y = mx + c

    The same reasoning is just as applicable when writing code.

  • by Univac_1004 ( 643570 ) on Tuesday April 26, 2005 @11:14PM (#12354905) Journal
    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.

  • by lifeblender ( 806214 ) on Tuesday April 26, 2005 @11:16PM (#12354916)
    I have one major suggested change for you: Comment your thoughts. If you spend half a day or more figuring out how to solve a problem, then your solution AND the thought process that let you get there belong as comments. This can be several pages. If you do this, you will never repeat mental work, and anyone (including you) reading your code will have one hell of a guide. Also, when there is a ton of code, comments are a guide. You did a lot of work just deciding on the organization of the code, so document that with comments too.

    Basically, I write down anything that I don't want to have to figure out again. It saves me so much time with large projects. You have to keep this level of agressiveness up to keep the comments up to date, but if you get used to putting your thought processes on paper, then you'll get used to editting them on paper, too.
  • by localman ( 111171 ) on Tuesday April 26, 2005 @11:21PM (#12354947) Homepage
    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.
  • by el-spectre ( 668104 ) on Tuesday April 26, 2005 @11:36PM (#12355041) Journal
    Yeah, but mathematicians have set letters (or symbols) they use to represent things (m = slope, for example). Programmers don't have that, except for i and j :)
  • by Anonymous Coward on Tuesday April 26, 2005 @11:48PM (#12355152)
    in vim, with C code, you can just do:

    gg=G

    and it will reindent a file for you....

    I hate programmers who refuse to learn about their tools.
  • by chromatic ( 9471 ) on Tuesday April 26, 2005 @11:58PM (#12355233) Homepage

    That was MJD's POD is not Literate Programming [perl.com].

  • by emurphy42 ( 631808 ) on Wednesday April 27, 2005 @12:36AM (#12355521) Homepage
    Er, that's a nonexistent article. Fortunately, a Wikipedia search on "orthogonal" did lead to another article that does exist. [faqs.org]
  • Linus' Opinion (Score:2, Informative)

    by Brainix ( 748988 ) <brainix@gmail.com> on Wednesday April 27, 2005 @12:49AM (#12355607) Homepage
    I can't believe noone has posted about this yet. Click here [reptiles.org] (or check /usr/src/linux/Documentation/CodingStyle) for Linus' witty and insightful views on this topic. Chapter 5 in particular.
  • by Jeremi ( 14640 ) on Wednesday April 27, 2005 @12:51AM (#12355617) Homepage
    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].

  • by Anonymous Coward on Wednesday April 27, 2005 @01:01AM (#12355703)
    Orthogonal functions are those that do not overlap in functionality. Then all your operations on data sets will be composed of distinct function calls that won't require rewriting when you need to do something different to the data. Writing a "Sum accounts and sort by name" function is bad, writing a sum function and a sort function and using them in the right order is the correct way to do things.
  • by crazyphilman ( 609923 ) on Wednesday April 27, 2005 @01:46AM (#12355954) Journal
    I'm stuck doing VB6 on a legacy app, right? And the previous programmer was a complete jackass. For example, in this one function I'm working on, he's just doing a very simple SQL select pulling down two values in one record, and returning true if the record exists, and false if it doesn't.

    First of all, he didn't properly handle errors, so if anything goes wrong, his object just sits there in memory (no garbage collection in VB6!). Our servers have been crapping out lately, so this was a valuable discovery for me. Second, when he fetches his data from Oracle, he inexplicably sets the lock type to "adLockPessimistic" (with a static, client-side recordset that doesn't get updated!!!) which locks the affected records until the recordset is done with them. Which I suspect means that if there's an error and the recordset doesn't get closed or set to nothing, the records will be locked until some timeout occurs. Another tip about why the server might be choking (lots of users hitting the same records, ha ha, what fun). And, on top of that, he's not cleaning up his objects, so it's not like the recordset ever releases the records in the first place. I mean, at some point they're getting released, but it's not HIS code that's doing it.

    So, I rewrote the whole thing correctly, fixing the error handling and making sure objects were cleaned out, switching to a static recordset with optimistic locking, etc, shortening and speeding up the code. I might speed it up some more by making it readonly, I dunno. Thinking about it.

    And, so I'd remember what I did and why, I added brief notes stating why I chose the lock and cursor types I did, why I used adUseClient instead of server, etc. In other words, I pointed out the nonobvious stuff that the code didn't explain.

    I'm hoping that the next programmer to come along, who might not be that good at ADO, will read what I wrote and understand why it should be done a certain way.

    I think good comments are about context, and sharing the thought process of the original programmer, and adding nonobvious info.

    Sorry I didn't have code to share, but it's at work and it's late. :(

  • by Anonymous Coward on Wednesday April 27, 2005 @01:47AM (#12355959)
    Obviously you didn't learn shit. NO MAGIC NUMBERS means:

    ERROR = -1;

    if( f(x) != ERROR ) ...

    and not:

    MINUSONE = -1;

    if( f(x) != MINUSONE ) ...

    Testing mystery numbers is bad practice. Any mystery number that you test for has some symbolic meaning. Like true, false, null... and so on.

  • by HomeworkJunkie ( 877015 ) on Wednesday April 27, 2005 @02: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.
  • by FireFury03 ( 653718 ) <slashdot.nexusuk@org> on Wednesday April 27, 2005 @06:07AM (#12357070) Homepage
    people who don't know how to indent their code, and want you to help debug it!

    So tell them to go away and come back when it's properly indented. Point them at the tools they need to automagically indent their badly indented code. They'll never learn if you don't tell them that they have an unreadable coding style and explain to them why intenting is a Good Thing.
  • by ajs ( 35943 ) <ajsNO@SPAMajs.com> on Wednesday April 27, 2005 @08:59AM (#12357924) Homepage Journal
    I tend to follow these rules about documentation:
    • Document every subroutine/method/macro/functional unit
    • If you find yourself having to document a free-standing chunk of code, consider modularizing
    • Write system/functional/etc. documentation seperately (in the same file in some langauges, but NEVER IN THE CODE) to give yourself a chance to abstract the concepts that the code represents.
    • Add inline comments to free-staning code without modularizing, only when the reader's assumptions might be violated (e.g. I tend to document my use of POSIX time routines because they LOOK like Y2K bugs, but are not).
    • When documenting routines, include the meaning of parameters and the meaning of the return value (if any) as well as the general function of the routine
    • Avoid globals where possible, but document each when used
    • Keep documentation well formatted so that it does not obstruct code visually.
    Following these rules, I have never produced code which someone who knew the language I was programming in could not read and understand. When I write Perl code and use some large Schwartzian Transform, someone who does not know Perl might be confused, but I can't really stop and document every line of code for everyone who doesn't know the language. If I'm doing something particularly hairy that might not look sane to a reader who knows the language, then I comment it.

    There's code that I wrote 15 years ago that someone came to me a few years ago to ask about using in a college class, so I guess it works out ok ;-)
  • by Neurotoxic666 ( 679255 ) <[moc.liamtoh] [ta] [666cixotoruen]> on Wednesday April 27, 2005 @10:12AM (#12358620) Homepage
    Maybe he should've wrote it appropriately:
    All generalisms are wrong. // Joke
  • by jim3e8 ( 458859 ) on Wednesday April 27, 2005 @12:10PM (#12360254) Homepage
    gg go to first line
    = indent
    G until end of document

    vim has an excellent help system. :help 30.2 takes you right to the relevant section (30.2). Lo and behold, "If you have really badly indented code, you can re-indent the whole file with: gg=G".

    This is what it means, "learn your tools."

Gravity brings me down.

Working...