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 41 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.
  • N Naruki 0

    Never is an absolute, so the chances are you are wrong. Eventually. :-)

    Narf.

    J Offline
    J Offline
    Joe Woodbury
    wrote on last edited by
    #35

    I was speaking past tense. Henceforth, I will never use comments and therefore never see this. :)

    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
      Schmuli
      wrote on last edited by
      #36

      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 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

        J Offline
        J Offline
        Jakob Olsen
        wrote on last edited by
        #37

        Don't comment that line.... :laugh: Rename the function to InitArrayOfPoints() instead

        Bitmatic - C# & .NET programming

        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
          Anton Afanasyev
          wrote on last edited by
          #38

          I can see why things like this may be done - the programemr first writes code, then comments it. This may (theoretically) benefit the original programmer in that the code is fresh in his memory, and writing a comment to reflect what it does may "force" him to spot subtle errors. (Of course, same can be said about the traditional "above"-style commenting, but I think the "below"-style makes sense here). I think the "below" style would also benefit a new programmer trying to understand or debug the code - she reads the code, understands (hopefully) it, and then reads the intention - if they dont match, well, there's clearly a problem with the code. How's that for a possible "why?" ?

          "Impossible" is just an opinion.

          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

            U Offline
            U Offline
            User 4483848
            wrote on last edited by
            #39

            At university I was always told that comments should go before the code rather than after. It's the way our brains work, top to bottom, left to right. Comments below the line is just weird, and looks weird. The comment looks far too wordy though. Surely 'initializes points' is enough. If it was me I would do InitPoints(); or maybe just Init(); if it was a small application. The function name is enough to guess what it does.

            1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              Generally, comments have always been written above or on the same line as the code. My objection to having comments below the relevant code is that it would be awkward if it was consistant:

              Init();
              // Init blah de blah

              is fine, but

              private MyClass[] GetMyClassInstances(int count, bool why, string whathaveyou...)
              {
              ... body of long function
              }
              /// GetMyClass does whatever it does
              /// Parameters: whatever they are

              is just asking for trouble!

              No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones "Rumour has it that if you play Microsoft CDs backwards you will hear Satanic messages.Worse still, is that if you play them forwards they will install Windows"

              D Offline
              D Offline
              dmpthekiller
              wrote on last edited by
              #40

              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 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

                C Offline
                C Offline
                caposada
                wrote on last edited by
                #41

                Definitely above. The point is that a coder can come along later and read the comment first (understand what to expect) and then read the code. Besides, now it's the convention. To use something else would likely lead to confusion. Chris.

                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
                  Thomas Vanderhoof
                  wrote on last edited by
                  #42

                  Above...unless you really don't like the person who will be maintaining 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

                    B Offline
                    B Offline
                    BunnyFaber
                    wrote on last edited by
                    #43

                    I have a habit of putting comments to the right of the code.

                    Init(); // Here we do some thing with other things that do stuff to things

                    Probably a habit leftover from my assembler days. Which would be just yesterday, in fact.

                    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
                      TrudyH
                      wrote on last edited by
                      #44

                      As someone who has only taken a few programming classes, and learned everything else on my own my opinion may be a bit skewed. :omg: I prefer the comments above the code for two reasons. First I have learned a lot from studying other peoples code and having the comments above the code helps me follow along and figure out what they are doing and then how they are doing it. Second in my own code it helps me to go back later and kind of follow the logic of what I am trying to do so I can go back later to see if I missed something. Kinda of a "Do this, like this, then do that, like this . . ." This probably isn't as important for most of you but as someone who doesn't code for a living, but has to code to make their living it can be such a big help. ;) T

                      1 Reply 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!

                        R Offline
                        R Offline
                        rcampbell12
                        wrote on last edited by
                        #45

                        As much as possible, yes. The mistake is when you believe no commenting is necessary because your code is so wonderfully written. Can't tell if you really take it that far, of course.

                        R 1 Reply Last reply
                        0
                        • R rcampbell12

                          As much as possible, yes. The mistake is when you believe no commenting is necessary because your code is so wonderfully written. Can't tell if you really take it that far, of course.

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

                          To be honest when i write code it falls into 2 distinct camps: The stuff I understand and the stuff I don't. There's no point commenting the former and I wouldn't know where to start with the latter ;-)

                          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

                            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
                                          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