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

    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