Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Commentaries - above or below the code?

Commentaries - above or below the code?

Scheduled Pinned Locked Moved The Lounge
questiondata-structures
64 Posts 46 Posters 46 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lutoslaw

    A programmingcommenting question. I have been writing commentaries above a related line of code, like this:

    // The Init() method we call here initializes an array of points
    Init();

    However, I have seen a big sample of code written by one of my professors recently. The commentaries was placed below a line.

    Init();
    // The Init() method we call here initializes an array of points

    Sincerely, I have found it very clear and understable. Did anybody encounter such approach to commenting code? Is it recommended?

    Greetings - Jacek

    M Offline
    M Offline
    Michael Doyle
    wrote on last edited by
    #47

    Having both taught computer programming and done it for a living, I find that comments serve two distinct purposes. In the classroom, a comment explains "here is what this code does"; because, when teaching, you introduce an idea and then you explain it. It is appropriate to put such comments immediately following the line of code, but only for didactic purposes. In the production world, a comment explains "here is what this code is for"; and should go above the code (or, if very brief, next to it).

    1 Reply Last reply
    0
    • D dmpthekiller

      Hahahaha... Totally agree!!! Comments above or on the same line (if it's a very simple comment)... Edit: And what about useless comments???... Have you ever wrote those???... Like: // Check if a > b if (a > b) { (...) } Hahahaha... It seems too stupid, but I must confess I've done it a couple of times...

      modified on Thursday, November 12, 2009 6:49 AM

      G Offline
      G Offline
      gisTimmy
      wrote on last edited by
      #48

      The position of useless comments shouldn't really matter. :)

      S R 2 Replies Last reply
      0
      • R Russell Jones

        What are these green things in your code ;-) Code should be written in such a way that it is self documenting!

        J Offline
        J Offline
        johannesnestler
        wrote on last edited by
        #49

        :laugh: Nowadays I write big visualization apps for plants and machines - the code would be a miracle without comments - not because of messy code - but you wouldn't understand why it was done this way (the code doesn't explain the machine - you see?). When I was a junior developer I thought like you (of course we are all proud of our perfect coding style) - but when I worked on my first big C++ project (~ 1 Million lines of code - without any comment!) I realized how stupid this ideology was... (It wasn't all my code, and comments where forbidden by the style-guide for faster builds (the whole app had about 35 Million lines)) Now I'm a big fan of comments - sometimes I write them before i write any code, to get a clear structure of the problem - if you can describe a problem in normal words it's easy to write the code. So everyone can understand my code (even my boss or my wife ;P ). If you ever have to take over a well documented piece of code from someone else, you will see how nice it is just to skim over the comments to see WHAT the code does - later you can read the "self documenting" code to see HOW it was done. btw: Comments before the code! :rose:

        R D 2 Replies Last reply
        0
        • J johannesnestler

          :laugh: Nowadays I write big visualization apps for plants and machines - the code would be a miracle without comments - not because of messy code - but you wouldn't understand why it was done this way (the code doesn't explain the machine - you see?). When I was a junior developer I thought like you (of course we are all proud of our perfect coding style) - but when I worked on my first big C++ project (~ 1 Million lines of code - without any comment!) I realized how stupid this ideology was... (It wasn't all my code, and comments where forbidden by the style-guide for faster builds (the whole app had about 35 Million lines)) Now I'm a big fan of comments - sometimes I write them before i write any code, to get a clear structure of the problem - if you can describe a problem in normal words it's easy to write the code. So everyone can understand my code (even my boss or my wife ;P ). If you ever have to take over a well documented piece of code from someone else, you will see how nice it is just to skim over the comments to see WHAT the code does - later you can read the "self documenting" code to see HOW it was done. btw: Comments before the code! :rose:

          R Offline
          R Offline
          Russell Jones
          wrote on last edited by
          #50

          just in case there's any confusion I do put comments in my code when it matters. Excessive comments, especially where 90% of them are meaningless just cause people to ignore them and you might as well not have added any. I'd rather write a big chunk when I get to a point that really needs explaining and leave the shorter (trivial) methods without any comments at all.

          1 Reply Last reply
          0
          • J johannesnestler

            :laugh: Nowadays I write big visualization apps for plants and machines - the code would be a miracle without comments - not because of messy code - but you wouldn't understand why it was done this way (the code doesn't explain the machine - you see?). When I was a junior developer I thought like you (of course we are all proud of our perfect coding style) - but when I worked on my first big C++ project (~ 1 Million lines of code - without any comment!) I realized how stupid this ideology was... (It wasn't all my code, and comments where forbidden by the style-guide for faster builds (the whole app had about 35 Million lines)) Now I'm a big fan of comments - sometimes I write them before i write any code, to get a clear structure of the problem - if you can describe a problem in normal words it's easy to write the code. So everyone can understand my code (even my boss or my wife ;P ). If you ever have to take over a well documented piece of code from someone else, you will see how nice it is just to skim over the comments to see WHAT the code does - later you can read the "self documenting" code to see HOW it was done. btw: Comments before the code! :rose:

            D Offline
            D Offline
            Dan Neely
            wrote on last edited by
            #51

            johannesnestler wrote:

            When I was a junior developer I thought like you (of course we are all proud of our perfect coding style) - but when I worked on my first big C++ project (~ 1 Million lines of code - without any comment!) I realized how stupid this ideology was... (It wasn't all my code, and comments where forbidden by the style-guide for faster builds (the whole app had about 35 Million lines))

            Did having a few percent of the lines be comments actually have a meaningful impact on the build time; or like most style guide content, was it the product of someone who took a role writing in word because he couldn't hack it as a coder?

            3x12=36 2x12=24 1x12=12 0x12=18

            1 Reply Last reply
            0
            • L Lutoslaw

              A programmingcommenting question. I have been writing commentaries above a related line of code, like this:

              // The Init() method we call here initializes an array of points
              Init();

              However, I have seen a big sample of code written by one of my professors recently. The commentaries was placed below a line.

              Init();
              // The Init() method we call here initializes an array of points

              Sincerely, I have found it very clear and understable. Did anybody encounter such approach to commenting code? Is it recommended?

              Greetings - Jacek

              P Offline
              P Offline
              p51dfltln
              wrote on last edited by
              #52

              i like the VS XML generating comments - which go above.

              1 Reply Last reply
              0
              • G gisTimmy

                The position of useless comments shouldn't really matter. :)

                S Offline
                S Offline
                Sterling Camden independent consultant
                wrote on last edited by
                #53

                Yes, it does matter. They should be positioned in the recycle bin.

                1 Reply Last reply
                0
                • L Lutoslaw

                  A programmingcommenting question. I have been writing commentaries above a related line of code, like this:

                  // The Init() method we call here initializes an array of points
                  Init();

                  However, I have seen a big sample of code written by one of my professors recently. The commentaries was placed below a line.

                  Init();
                  // The Init() method we call here initializes an array of points

                  Sincerely, I have found it very clear and understable. Did anybody encounter such approach to commenting code? Is it recommended?

                  Greetings - Jacek

                  S Offline
                  S Offline
                  Stuart Rubin
                  wrote on last edited by
                  #54

                  I always defer to my favorite software book Code Complete for these kinds of questions. McConnell spends a whole chapter talking about best practices in writing comments. (This may seem silly to some, but if you read the book, it makes a TON of sense.) Chapter 32 states plainly "Use comments to prepare the reader for what is to follow", and explains the well-established convention of putting comments above the code it's documenting. Further (and this is the fun part you can show your professor), he claims "This idea isn't always taught in programming classes, but it's a well-established convention in commercial practice." Well-established conventions, until there is a clear technical reason to do otherwise, should be followed! Also, there has been some discussion hinting that well-crafted code should not need comments. My thought is all about how fast you can look at code to quickly understand what it's supposed to do. Your brain doesn't waste time translating variable names (even if they're well named), doing math, finding control loop conditions, etc. Stuart

                  1 Reply Last reply
                  0
                  • L Lutoslaw

                    A programmingcommenting question. I have been writing commentaries above a related line of code, like this:

                    // The Init() method we call here initializes an array of points
                    Init();

                    However, I have seen a big sample of code written by one of my professors recently. The commentaries was placed below a line.

                    Init();
                    // The Init() method we call here initializes an array of points

                    Sincerely, I have found it very clear and understable. Did anybody encounter such approach to commenting code? Is it recommended?

                    Greetings - Jacek

                    A Offline
                    A Offline
                    Alan Burkhart
                    wrote on last edited by
                    #55

                    A couple of lines for general explanation above the code block, and short 2 or 3 word comments at the end of appropriate lines to explain what a particular variable or called function is for. Always worked for me.

                    1 Reply Last reply
                    0
                    • L Lutoslaw

                      A programmingcommenting question. I have been writing commentaries above a related line of code, like this:

                      // The Init() method we call here initializes an array of points
                      Init();

                      However, I have seen a big sample of code written by one of my professors recently. The commentaries was placed below a line.

                      Init();
                      // The Init() method we call here initializes an array of points

                      Sincerely, I have found it very clear and understable. Did anybody encounter such approach to commenting code? Is it recommended?

                      Greetings - Jacek

                      T Offline
                      T Offline
                      Trevortni
                      wrote on last edited by
                      #56

                      Personally, my comments generally come before the code, but it's because of the way I code. I don't set out to write comments; rather, as I enter a new conceptual block, I find that it helps clear my mind to write a quick explanation of what I'm going to do. Then I find it easier to focus on what needs to be done within the conceptual block. Thus, the comments I write actually direct the code, rather than the other way around. I don't need to go back over the code and add comments this way. Basically, if there's a comment in the code, it's because it was needed to write the code; if there's not, it's because the conceptual block was straight-forward enough that this guide wasn't necessary for my initial thoughts. I suppose you could say my coding style is self-commenting. Or is my commenting style self-coding? :)

                      1 Reply Last reply
                      0
                      • E Electron Shepherd

                        Or even better, rename the function so you don't need the comment.

                        Server and Network Monitoring

                        R Offline
                        R Offline
                        RichardM1
                        wrote on last edited by
                        #57

                        That's right. The old 'self documenting code' joke is not always a joke. Take time and space to make variable names mean something. Make the method name be really indicative of what it does. I use long, descriptive variable and method names, and my experience is that they are more likely to stay up to date than the comments are, and less likely to be re-used in an inappropriate manner. 'int i;' will get used/reused on demand, but 'int windowIdx; will generally only be used for the index of a window, into a list of windows. But comments are required where the code can not be made easy to understand, because it is actually very complex algorithm, it has been optimized, or for some other reason obfuscated.

                        Silver member by constant and unflinching longevity.

                        1 Reply Last reply
                        0
                        • 1 1 21 Gigawatts

                          Comments below the line of code? Sure, if you're the sort of person who wears their underpants on top of their trousers.

                          "People who don't like their beliefs being laughed at shouldn't have such funny beliefs." ~ Anon "If you can't explain it simply, you don't understand it well enough" ~ Albert Einstein Currently reading: 'The Greatest Show on Earth', by Richard Dawkins.

                          R Offline
                          R Offline
                          RichardM1
                          wrote on last edited by
                          #58

                          Wait a minute, is there some problem with that? :wtf:

                          Silver member by constant and unflinching longevity.

                          1 Reply Last reply
                          0
                          • G gisTimmy

                            The position of useless comments shouldn't really matter. :)

                            R Offline
                            R Offline
                            RichardM1
                            wrote on last edited by
                            #59

                            Sure it matters, they should be in the bit bin.

                            Silver member by constant and unflinching longevity.

                            1 Reply Last reply
                            0
                            • P peterchen

                              "You botched it", basically, for different values of "it".

                              Personally, I love the idea that Raymond spends his nights posting bad regexs to mailing lists under the pseudonym of Jane Smith. He'd be like a super hero, only more nerdy and less useful. [Trevel]
                              | FoldWithUs! | sighist | µLaunch - program launcher for server core and hyper-v server

                              R Offline
                              R Offline
                              RichardM1
                              wrote on last edited by
                              #60

                              peterchen wrote:

                              "You botched it", basically, for different all values of "it".

                              FTFY

                              Silver member by constant and unflinching longevity.

                              1 Reply Last reply
                              0
                              • S Schmuli

                                As almost everyone has replied previously, generally comments appear above the code or inline. In the world of academia, where real-world applications, programming teams and programmers are sparse to non-existent, you may find lots of things that are different to what really goes out in the real-world. That being said, although I may be wrong, there is one time when I will put a comment after the line of code, and that is in the case of 'else'.

                                // This explains what will happen when 'condition' is true
                                if( true )
                                {
                                ...
                                }
                                else
                                {
                                // This explains what happens in other cases
                                }

                                I'm not sure what others do in this case, but then again, it is very specific, only appears inside a function block, and is pretty clear when reading the code.

                                R Offline
                                R Offline
                                RichardM1
                                wrote on last edited by
                                #61

                                Yes, commenting if/else statements is a pain. I comment if (without else) before the test, and if/else after the contained curly bracket. I don't like it, I just have not found a better way.

                                Silver member by constant and unflinching longevity.

                                1 Reply Last reply
                                0
                                • C Chris Maunder

                                  Everyone, for ever, has placed it above. Let's just stick to the accepted pattern and move along.

                                  cheers, Chris Maunder The Code Project Co-founder Microsoft C++ MVP

                                  R Offline
                                  R Offline
                                  RichardM1
                                  wrote on last edited by
                                  #62

                                  Chris - say it with me now: "Change" Change, it's not just for good, it's for its own sake.

                                  Silver member by constant and unflinching longevity.

                                  1 Reply Last reply
                                  0
                                  • D Dalek Dave

                                    I agree with the consensus, above. It is easier to read, and tells you what to expect.

                                    ------------------------------------ In science, 'fact' can only mean 'confirmed to such a degree that it would be perverse to withhold provisional assent.' I suppose that apples might start to rise tomorrow, but the possibility does not merit equal time in physics classrooms. Stephen J Gould

                                    K Offline
                                    K Offline
                                    kirankss
                                    wrote on last edited by
                                    #63

                                    The comment should alway be either above or to the right of the code.

                                    1 Reply Last reply
                                    0
                                    • L Lutoslaw

                                      A programmingcommenting question. I have been writing commentaries above a related line of code, like this:

                                      // The Init() method we call here initializes an array of points
                                      Init();

                                      However, I have seen a big sample of code written by one of my professors recently. The commentaries was placed below a line.

                                      Init();
                                      // The Init() method we call here initializes an array of points

                                      Sincerely, I have found it very clear and understable. Did anybody encounter such approach to commenting code? Is it recommended?

                                      Greetings - Jacek

                                      P Offline
                                      P Offline
                                      patbob
                                      wrote on last edited by
                                      #64

                                      Above the code that implements it is best. That allows the reader to go from generalitites to specifics by reading top to bottom, just like an outline, or a story, or any other type of explanatory writing. I think you should your the entire class to turn in an assignment with the comments below the code they comment. They can't mark down for it since the professor provides examples comemnted that way. If they actually read your source code, they'll probably try to mark it down though.

                                      patbob

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups