Slashdot Mirror


Successful Strategies for Commenting Your Code

LilG writes "Over at Particletree, Ryan Campbell writes about Successful Strategies for Commenting Your Code. His essay gives advice and examples on proper commenting, and details some different strategies."

29 of 500 comments (clear)

  1. Huh? by tntguy · · Score: 5, Funny

    These comments you speak of, they seem foreign and strange to me.

    1. Re:Huh? by bluelip · · Score: 3, Funny

      "The code was hard to write. The code should be hard to read."

      Was that some form of early DMCA or IP lockdown?

      --

      Yep, I never spell check.
      More incorrect spellings can be found he
    2. Re:Huh? by MHobbit · · Score: 4, Funny

      A TRUE Klingon warrior does not comment his code!

      *ducks*

      --
      Debugging? Klingons do not debug. Bugs are good for building character in the user.
  2. My favorite code comment not written by me by dcarey · · Score: 4, Funny

    I was doing some maintenance on someone else's code and came across this nasty set of like 8 nested if/elses. It was a bloody horrible hack. But the best part of all was the comment right at the top: /* Oh, fuck */

    --

    -- (Score:i , Imaginary)

    1. Re:My favorite code comment not written by me by lukewarmfusion · · Score: 4, Funny

      So you found that, eh? *evil chuckle*

      I was in a similar spot a month or two ago and found some other company's comments - unhelpful as always - in the form of typed out sound effects.

      # This function goes vroooooooommm-pop!

      I have no idea why the developers put such comments there other than to entertain themselves as they sifted through their horribly written Perl.

    2. Re:My favorite code comment not written by me by Abcd1234 · · Score: 4, Interesting

      Well, there's nothing wrong with entertaining comments, as long as that's not *all* you have. In my experience, useful comments interspersed with a bit of humour can make code analysis and maintenence at least a little less boring.

  3. Example by dduardo · · Score: 3, Funny

    /*This loop starts at x is equal to 1 and continues while x is less then 5. x is incremented by 1 each time.*/ for( int x=1; x5; x++ ) { printf("Hello World\n");/*Prints "Hello World"*/ }

  4. var names by rockytriton · · Score: 3, Insightful

    My best strategy is intelligent variable names and clean simple code. -- http://www.dreamsyssoft.com

  5. Practice what you preach by ChrisF79 · · Score: 5, Funny

    I viewed the source on the site and nothing was commented :)

    --
    Finance tutorials and more! Understandfinance
  6. Comments are more important than Code? by jetkust · · Score: 4, Funny

    Comments are more important than Code?

    I once tried writing code that was completely made up of comments. It was easy to write and all, but didn't work very well.

    1. Re:Comments are more important than Code? by NialScorva · · Score: 4, Funny

      At least you had no compile errors or core dumps, and I bet you didn't have any exploitable vulnerabilities either.

    2. Re:Comments are more important than Code? by Not_Wiggins · · Score: 4, Interesting

      Actually, I start my programming projects by writing all the comments first; in this way, I get the pseudo-code ideas down and write the associated code under each comment. It helps me track where I am in a class/function, what I'm trying to accomplish (what parts still need to be written), and it nicely leaves an understandable trail without having to back-fill the code with comments once the project is "done."

      By front-loading the process with comments, I find it really helps not just the maintainability but it also keeps my programming tasks on track.

      --
      Diplomacy is the art of saying, "Nice doggie!" until you can find a rock.
  7. Don't comment by i.r.id10t · · Score: 5, Funny

    Don't comment at all, and just run it thru The Commentator!

    http://www.cenqua.com/commentator/

    --
    Don't blame me, I voted for Kodos
  8. I was skeptical at first regarding the topic by Anonymous Coward · · Score: 5, Insightful

    But when reading the article I was glad that the very first item was SELF DESCRIBING CODE. The most important part of code documentation is the code iteself IMHO.

    I think it was Fowler who once went as far as to state that when you find yourself needing to document code in order for it to be understood (vs. thoroughness, completness, or document generation) you probably need to refactor your code.

    How many times have we come across code like this?

    public void bigFunction1()...

    public void bigFunction2()...

  9. Commenting by Hamster+Of+Death · · Score: 4, Insightful

    Write the comments first, then add the code. That way you'll get a better grasp on the problem you're solving. If your comments don't explain the problem or someone else can't solve the problem using your comments then you'll probably need to rethink your approach.
    And for crying out loud update the comments when you change code!
    *grumbles about 10 year old code and 15 year old comments*

    1. Re:Commenting by bmwm3nut · · Score: 3, Insightful

      exactly! in fact, this was taught to me by a senior programmer who said: "you should never document your code, your should code your document." in projects where i've followed that rule, i've had many fewer problems than ones where i jumped into the code quickly.

  10. really being stressed in schools by StarvingSE · · Score: 3, Informative

    The next generation of professional coders will most likely be good commenters as well. I know from experience in my computer science classes, professors will mark programming assignments down significantly if comments are not included, or if they are hard to read. Most of the time they want all functions to be commented to explain what their parameters mean, how the function works, and the format of the output.

    --
    I got nothin'
  11. No by rbarreira · · Score: 5, Funny

    No, that first one should be something like:

    This loop starts at 1, and to 5 it counts. It doesn't count to 6, nor it does count to 8. It does not count to 3 or 4, except in passage to 5. When it reaches number 5, the fifth number...

    --

    The AACS key is NOT 0xF606EEFD628B1CA427BEA93A9CA9773F
    1. Re:No by legirons · · Score: 3, Funny
      Not in the real world... more like:
      //----------
      // ReadSalaryData - reads table of salary values from database
      // Parameters:
      // - DB, database handle
      // - Data, pointer to list
      // Returns: Success
      //---------
      void DeleteUsername(char *Name)
      {
      ...
  12. A comment on comments by Jeffrey+Baker · · Score: 5, Insightful
    Dear Doxygen and JavaDoc users,

    The following is an example of useless documentation:

    frobWoggleFfloofMoing

    public void frobWoggleFfloofMoing(String, String, String)

    Frobs the Woggle

    ...

    You see, running Doxygen over your header files may produce some output in HTML format, but it doesn't produce what I like to call "documentation." For instance, documentation would explain what is a Woggle, and when should it be Frobbed?

    Thank you, and have a nice life.

  13. Why I comment code... by Saeed+al-Sahaf · · Score: 4, Interesting

    I don't know why people (in general) don't like to comment code. I comment mostly for me, so a year or two down the line I'll know why I did something (I've found over the years of writing code, I may think I'll remember, but that often does not end up the case). In the end this selfish purpose ends up helping when other people need to maintain my code (other people fucking around with my code? Never).

    --
    "Who are in control, they are not in control of anything - they don't even control themselves!" - Glen Beck
  14. Things to always remember when commenting by WillAffleckUW · · Score: 4, Funny

    1. Never spelchezk.
    2. Use randomly chosen variable names, or objects that resemble your favorite Orcs and Trolls from LotR - after all, everyone knows that a Lothlorien object will have farseeing ability, so it's obvious.
    3. When instantiating something for the first time, never explain it - real programmers read the original object source.
    4. If you do something complex, write a short pithy comment like /* magic occurs */
    5. If you do something easy but you were drinking too much hot cocoa, write a long verbose description, and also mention how good the hot cocoa was.
    6. Always include song lyrics to what you're listening to while you wrote the code.
    7. Object inheritence means never having to explain the code.
    8. Repetition is the best way to reinforce obvious things - so repeat the obvious thing since it's the best way to reinforce it.
    9. If you break up with your girl/boyfriend, write about it in the comments - people really want to know.
    10. If you're updating or modifying code, write your opinion about the original code in the comments. Use nasty words if you can.

    --
    -- Tigger warning: This post may contain tiggers! --
  15. All Depends... by eno2001 · · Score: 3, Funny
    ...on who is going to be reading the code after you are gone. Sadly, due to differing levels of expertise, there is no optimum method of commenting code. For some, this is a very useful comment (I'm using pound sign comment designation as an example):

    # The main function starts here

    or...

    # This is a loop and it will run while a certain
    # condition exists.

    or...

    # Don't forget to remove this section after
    # I'm gone - Dan - 04/25/1995

    Think about the children! ;P

    --
    -"...bad old ideas look confusingly fresh when they are packaged as technology" - Jaron Lanier (Digital Maoism on Edge.o
  16. How & Whys by Anonymous Coward · · Score: 4, Insightful

    It's been said before, but I find the best advice I've ever gotten on commenting code is very simple -

    Comment the why, not the how.

    Hopefully the code should be clear enough to pick out the how, but *why* you are doing something is never going to be apparent from the code itself - at best someone might be able to infer it, but that becomes especially tricky when time passes or a new person signs on.

  17. What makes a good Comment? by vivin · · Score: 4, Interesting

    Good comments should explain these areas:

    a) What you're doing.
    b) Why you're doing it.
    c) How you're doing it.

    I took three assembly programming classes in College. The last one was on the 68k, where we wrote an embedded OS.

    Assembly code isn't all that intuitive, and writing comments is especially important. Our professor allocated 20% of our grade on each lab to comments. In addition to accurately describing what we were doing, he checked our grammar. One thing he always stressed is that too many engineers these days don't know how to write comments. Grammar is important in getting the message across unambiguously.

    In general, if a person can read your comment and then figure out how to translate what you said into code, then your comment is pretty good. It should give an idea of what you're trying to do, why you're doing it, and how you're doing it.

    One of my professor's grad students translated a program from MACRO32 into C++, and all without even knowing MACRO32. He looked through the comments to figure out what they were doing. They were so specific that he could easily translate blocks of code over to C++.

    Comments are very important - and I should definitely comment MY code. I can't remember the number of times I've come back to code that I've written and thought "WTF am I doing here? WTF was I smoking when I wrote this?!"

    --
    Vivin Suresh Paliath
    http://vivin.net

    I like
    1. Re:What makes a good Comment? by mOdQuArK! · · Score: 5, Insightful
      You really should comment every line of assembly, even when you just increment a register,

      As long as you're commenting the _real_ effect of the instruction, and not just mindlessly repeating what the mnemonic already tells the reader. I've seen lots of stupid assembly-language comments like "increments the A register". Gee, thanks - I couldn't have figured that out from the "INC A" mnemonic.

    2. Re:What makes a good Comment? by idontgno · · Score: 3, Informative
      As long as you're commenting the _real_ effect of the instruction, and not just mindlessly repeating what the mnemonic already tells the reader.

      Or worse yet, lying.

      One pre-paleolithic piece of mainframe assembler I (and my colleagues) once worked with was an intricate sequence of shift operations designed to isolate a 5-bit segment in the middle of a 36-bit word in one of the accumulators. It was documented with a breathtakingly detailed block of comments, complete with little pictures of the contents of the register at each step in the process (composed of dashes and vertical bars and everything--ASCII documentation art).

      The code wasn't working, but there was absolutely no way that it was the code's fault. It was just FM (F'ing Magic) that it was broke. We were convinced of this. That is, until one of my supervisors, a crusty old communications sergeant, actually studied the instruction string of the segement of code in question and noticed that in the last step, the actual answer was being shifted the wrong direction--right out of the register and into the bit bucket.

      If only we could have compiled the comments...

      This, then, is Johnson's Postulate on Documentation: "Read the source code, too."

      --
      Welcome to the Panopticon. Used to be a prison, now it's your home.
  18. zerg by Lord+Omlette · · Score: 5, Insightful

    Ask someone else to look at your code. Every time they pause while scrolling, touch their chin, squint their eyes, furrow their brows, etc., it means you need a comment.

    --
    [o]_O
  19. Re:Don't do it! by 1nt3lx · · Score: 3, Informative

    This works quite well, but there are some consequences for this action:

    1. You are likely to be passed by on promotions because self inflicted developer dependence for this application.
    2. You will have to figure it out later, after you've forgotten what all that magic gobly-gook does.

    magic gobly-gook: effective, efficient, and incredibly dense code produced wilst 'in the zone'