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. Code comments: How do you comment your code?

Code comments: How do you comment your code?

Scheduled Pinned Locked Moved The Lounge
csharpdatabasesysadminquestion
38 Posts 19 Posters 0 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.
  • M Mark_Wallace

    Nagy Vilmos wrote:

    I am trying to get the monkeys developers here to get in the habbit of writting comments BEFORE code. This might aseem stupid but it is very effective.

    Seems anything but stupid, to me. Some of us remember the days when writing code from pseudocode was the norm. It's amazing the number of improvements you can come up with (and logical errors you can spot) by writing down what things will do before writing the code for them.

    I wanna be a eunuchs developer! Pass me a bread knife!

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #21

    Join the cool kids - Come fold with us[^]

    M 1 Reply Last reply
    0
    • M Mark_Wallace

      Nelek wrote:

      editing function names (search and replace) with some cryptic / not easy names

      Has anyone ever asked you what Function FormatCDrive() actually does?

      I wanna be a eunuchs developer! Pass me a bread knife!

      D Offline
      D Offline
      Dalek Dave
      wrote on last edited by
      #22

      hmm, let me just try th

      ------------------------------------ No Good Deed Goes Unpunished Clare Boothe Luce

      1 Reply Last reply
      0
      • H Harvey Saayman

        I'm busy writing a high load TCP server from absolute scratch in C# I've just started to implement the protocol I've been designing over the past few weeks and I really want to make an effort to comment this well because its quite complex and maintenance will be much easier that way, especially if other programmers need to maintain it a few years down the line. I've decided to make use of regions more than inline code comments although the in-lines are still there (usually explaining why your in a specific part of an if statement) So how do you comment your code?

        Harvey Saayman - South Africa Software Developer .Net, C#, SQL you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming) 1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111

        R Offline
        R Offline
        Ravi Bhavnani
        wrote on last edited by
        #23

        I've been using a simple yet effective rule for writing comments for decades:

        • Remove all code except for block boundaries (i.e. everything except stuff in conditional expressions and opening and closing curly braces).
        • Do the remaining comments and "code shell" represent the psuedocode fairly accurately?
        • If yes, you're done. Else, you need more comments.

        It's amazing how well this rule works.  Without conciously trying, applying this rule caused me to move from writing few long-winded PhD thesis comment blocks to several small one-line phrases that increase readability many fold. /ravi

        My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

        1 Reply Last reply
        0
        • H Harvey Saayman

          I'm busy writing a high load TCP server from absolute scratch in C# I've just started to implement the protocol I've been designing over the past few weeks and I really want to make an effort to comment this well because its quite complex and maintenance will be much easier that way, especially if other programmers need to maintain it a few years down the line. I've decided to make use of regions more than inline code comments although the in-lines are still there (usually explaining why your in a specific part of an if statement) So how do you comment your code?

          Harvey Saayman - South Africa Software Developer .Net, C#, SQL you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming) 1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111

          S Offline
          S Offline
          Single Step Debugger
          wrote on last edited by
          #24

          I don’t comment my code, even the most complex parts. Nobody pays me for comments, they want functionality and reliability. What?

          The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.

          D 1 Reply Last reply
          0
          • D Dalek Dave

            John Simmons / outlaw programmer wrote:

            I comment the code profusely, and in the event that there is a sufficiently complex implementation, I write a separate "How It Works" document in Word, and make check that document into source control with the rest of the project.

            That seems very concise, but I, unfortunately, have never written anything so complex as to warrent that. However, I shall bear it in mind if ever I do.

            ------------------------------------ No Good Deed Goes Unpunished Clare Boothe Luce

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #25

            I do it mainly for myself so that I can remember the whats-and-whys more than anything else, and to provide a foundation of knowledge regarding the system, and the reasons for its existence.

            .45 ACP - because shooting twice is just silly
            -----
            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

            1 Reply Last reply
            0
            • S Single Step Debugger

              I don’t comment my code, even the most complex parts. Nobody pays me for comments, they want functionality and reliability. What?

              The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.

              D Offline
              D Offline
              Dalek Dave
              wrote on last edited by
              #26

              And you want to keep the contract, right?

              ------------------------------------ No Good Deed Goes Unpunished Clare Boothe Luce

              S 1 Reply Last reply
              0
              • realJSOPR realJSOP

                Regions aren't an adequate replacement for comments. I comment the code profusely, and in the event that there is a sufficiently complex implementation, I write a separate "How It Works" document in Word, and make check that document into source control with the rest of the project.

                .45 ACP - because shooting twice is just silly
                -----
                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                D Offline
                D Offline
                dan sh
                wrote on last edited by
                #27

                John Simmons / outlaw programmer wrote:

                I write a separate "How It Works" document in Word

                Cool! I also maintain a notepad file (just for myself) containing why I wrote the code block that way. I also try to maintain what I had tried and why it failed so that I don't end up doing same thing again.

                50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

                1 Reply Last reply
                0
                • D Dalek Dave

                  And you want to keep the contract, right?

                  ------------------------------------ No Good Deed Goes Unpunished Clare Boothe Luce

                  S Offline
                  S Offline
                  Single Step Debugger
                  wrote on last edited by
                  #28

                  Dalek Dave wrote:

                  And you want to keep the contract, right?

                  I definitely do, but this doesn’t involve losing hours and hours in writing useless comments.

                  /*
                  * This function returns the next document in the sequence!
                  *This is an extremely useful comment! It makes me feel real professional!
                  *My Mother will be real proud with me! And I’m going to use an automates toll to generate an utterly
                  *useless documentation for this application.
                  *
                  */
                  LPDISPATCH CDocumentation::GetTheNextDocumentWithPreview()
                  {
                  iDispatch->GetNextDocument(true);
                  }

                  The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.

                  1 Reply Last reply
                  0
                  • L Lost User

                    Join the cool kids - Come fold with us[^]

                    M Offline
                    M Offline
                    Mark_Wallace
                    wrote on last edited by
                    #29

                    Trollslayer wrote:

                    CalcNumber firstNumber = CalcNumber.create(3);
                    CalcNumber secondNumber = CalcNumber.create(2);
                    CalcNumber nutting = CalcNumber.create(0);

                    figureToPost = firstNumber + secondNumber;
                    figureToPost += nutting;

                    returnString = figureToPost.ToString();
                    fullPoint = ".";

                    String.Concat(returnString, fullPoint);

                    return returnString;

                    See? Works every time! Your pseudocode helped me construct it easily!

                    I wanna be a eunuchs developer! Pass me a bread knife!

                    1 Reply Last reply
                    0
                    • H Harvey Saayman

                      I'm busy writing a high load TCP server from absolute scratch in C# I've just started to implement the protocol I've been designing over the past few weeks and I really want to make an effort to comment this well because its quite complex and maintenance will be much easier that way, especially if other programmers need to maintain it a few years down the line. I've decided to make use of regions more than inline code comments although the in-lines are still there (usually explaining why your in a specific part of an if statement) So how do you comment your code?

                      Harvey Saayman - South Africa Software Developer .Net, C#, SQL you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming) 1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111

                      D Offline
                      D Offline
                      Daniel Grunwald
                      wrote on last edited by
                      #30

                      I comment the public API of my classes using XML documentation comments. Though often it's obvious what a method is doing given its name, so I only fill in a short summary and leave out the parameters/return value stuff. I do use regions to group methods in large classes. I think it's a sign that the class is too large, but that's unavoidable in some frameworks (e.g. when writing WPF controls). For the implementation inside methods, I usually don't write any comments initially. If I have to change the code to fix bugs, I'll add comments why the fix was necessary (unless the bug was just some kind of typo). Basically, you shouldn't need comments to see what the code is doing. Inline comments should explain why the code is doing it, but only if that isn't obvious given the method's name/documentation. I also make use heavy use of invariants, so my Debug.Assert statements can be seen as a kind of comment.

                      1 Reply Last reply
                      0
                      • H Harvey Saayman

                        I'm busy writing a high load TCP server from absolute scratch in C# I've just started to implement the protocol I've been designing over the past few weeks and I really want to make an effort to comment this well because its quite complex and maintenance will be much easier that way, especially if other programmers need to maintain it a few years down the line. I've decided to make use of regions more than inline code comments although the in-lines are still there (usually explaining why your in a specific part of an if statement) So how do you comment your code?

                        Harvey Saayman - South Africa Software Developer .Net, C#, SQL you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming) 1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111

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

                        I rarely do. I've found comments are pretty useless and mostly good for giving people warm and fuzzies. Instead, I use very descriptive names for functions, methods, classes and variables and keep my code as simple as possible. Same with my coworkers.

                        Harvey Saayman wrote:

                        (usually explaining why your in a specific part of an if statement)

                        See, that's idiotic. If your "if" statement is written so poorly that you don't know why you're in it, your programming technique sucks. Some code I just wrote:

                        ...
                        } while (m_curSelection != orgSelection && !m_pItems[m_curSelection].IsSelectable());

                        No comment is going to make that more clear.

                        1 Reply Last reply
                        0
                        • H Harvey Saayman

                          I'm busy writing a high load TCP server from absolute scratch in C# I've just started to implement the protocol I've been designing over the past few weeks and I really want to make an effort to comment this well because its quite complex and maintenance will be much easier that way, especially if other programmers need to maintain it a few years down the line. I've decided to make use of regions more than inline code comments although the in-lines are still there (usually explaining why your in a specific part of an if statement) So how do you comment your code?

                          Harvey Saayman - South Africa Software Developer .Net, C#, SQL you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming) 1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111

                          M Offline
                          M Offline
                          Marc Clifton
                          wrote on last edited by
                          #32

                          Harvey Saayman wrote:

                          I've decided to make use of regions

                          I despise regions!

                          Harvey Saayman wrote:

                          So how do you comment your code?

                          Don't tell me what the code does (I can figure that out quickly enough) tell me WHY it does it, and what the decisions were to lead up to that implementation. That brings a lot more understanding, IMO. Marc

                          Will work for food. Interacx

                          I'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner

                          G 1 Reply Last reply
                          0
                          • M Marc Clifton

                            Harvey Saayman wrote:

                            I've decided to make use of regions

                            I despise regions!

                            Harvey Saayman wrote:

                            So how do you comment your code?

                            Don't tell me what the code does (I can figure that out quickly enough) tell me WHY it does it, and what the decisions were to lead up to that implementation. That brings a lot more understanding, IMO. Marc

                            Will work for food. Interacx

                            I'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner

                            G Offline
                            G Offline
                            Gary Wheeler
                            wrote on last edited by
                            #33

                            Marc Clifton wrote:

                            I despise regions!

                            Unfortunately, I think most people think of regions as a documentation mechanism, rather than an editing tool. I use them to make the outlining more useful. With regions, I can more quickly collapse parts of the code out of the way so I can concentrate on the other bits.

                            Software Zen: delete this;

                            M 1 Reply Last reply
                            0
                            • H Harvey Saayman

                              I'm busy writing a high load TCP server from absolute scratch in C# I've just started to implement the protocol I've been designing over the past few weeks and I really want to make an effort to comment this well because its quite complex and maintenance will be much easier that way, especially if other programmers need to maintain it a few years down the line. I've decided to make use of regions more than inline code comments although the in-lines are still there (usually explaining why your in a specific part of an if statement) So how do you comment your code?

                              Harvey Saayman - South Africa Software Developer .Net, C#, SQL you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming) 1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111

                              E Offline
                              E Offline
                              Ennis Ray Lynch Jr
                              wrote on last edited by
                              #34

                              I hate regions. Ironically, the C++ TCP server I wrote doesn't have any comments while most of my code is usually extensively, logically commented.

                              Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

                              1 Reply Last reply
                              0
                              • H Harvey Saayman

                                I'm busy writing a high load TCP server from absolute scratch in C# I've just started to implement the protocol I've been designing over the past few weeks and I really want to make an effort to comment this well because its quite complex and maintenance will be much easier that way, especially if other programmers need to maintain it a few years down the line. I've decided to make use of regions more than inline code comments although the in-lines are still there (usually explaining why your in a specific part of an if statement) So how do you comment your code?

                                Harvey Saayman - South Africa Software Developer .Net, C#, SQL you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming) 1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111

                                N Offline
                                N Offline
                                N a v a n e e t h
                                wrote on last edited by
                                #35

                                I too hate regions.

                                Harvey Saayman wrote:

                                So how do you comment your code?

                                I hardly write comments because it is very hard to keep it updated when you change the code. IMO, most of the programmers can easily read code without the help of comments. All you need to ensure is the code is well written by using meaningful method and class names. :)

                                Best wishes, Navaneeth

                                S 1 Reply Last reply
                                0
                                • G Gary Wheeler

                                  Marc Clifton wrote:

                                  I despise regions!

                                  Unfortunately, I think most people think of regions as a documentation mechanism, rather than an editing tool. I use them to make the outlining more useful. With regions, I can more quickly collapse parts of the code out of the way so I can concentrate on the other bits.

                                  Software Zen: delete this;

                                  M Offline
                                  M Offline
                                  Marc Clifton
                                  wrote on last edited by
                                  #36

                                  Gary Wheeler wrote:

                                  With regions, I can more quickly collapse parts of the code out of the way so I can concentrate on the other bits.

                                  My "region" limit is files. If the code is getting so big in a particular class (let's assume here that there's one class per file) then it can be broken up into separate files, even with the same class, or perhaps the class design should be looked at. I don't like regions around fields, properties, events, etc., because mostly I'm not even looking at those anyways. But happily, I can set the editor to not collapse regions for those that do like regions, so I'm happy. :) Marc

                                  Will work for food. Interacx

                                  I'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner

                                  1 Reply Last reply
                                  0
                                  • N N a v a n e e t h

                                    I too hate regions.

                                    Harvey Saayman wrote:

                                    So how do you comment your code?

                                    I hardly write comments because it is very hard to keep it updated when you change the code. IMO, most of the programmers can easily read code without the help of comments. All you need to ensure is the code is well written by using meaningful method and class names. :)

                                    Best wishes, Navaneeth

                                    S Offline
                                    S Offline
                                    Single Step Debugger
                                    wrote on last edited by
                                    #37

                                    N a v a n e e t h wrote:

                                    I hardly write comments because it is very hard to keep it updated when you change the code. IMO, most of the programmers can easily read code without the help of comments. All you need to ensure is the code is well written by using meaningful method and class names.

                                    Quoted and fived! :)

                                    The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.

                                    1 Reply Last reply
                                    0
                                    • H Harvey Saayman

                                      I'm busy writing a high load TCP server from absolute scratch in C# I've just started to implement the protocol I've been designing over the past few weeks and I really want to make an effort to comment this well because its quite complex and maintenance will be much easier that way, especially if other programmers need to maintain it a few years down the line. I've decided to make use of regions more than inline code comments although the in-lines are still there (usually explaining why your in a specific part of an if statement) So how do you comment your code?

                                      Harvey Saayman - South Africa Software Developer .Net, C#, SQL you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming) 1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111

                                      R Offline
                                      R Offline
                                      Roger Wright
                                      wrote on last edited by
                                      #38

                                      Since I tend to get interrupted or distracted while working on a project, it often gets set aside for weeks at a time. I often forget what I was planning to do next, so I've adopted the practice of commenting the crap out of everything I do. Even before I start, out of long habit, I write a skeleton program (these days the IDE does that for me), then block it out with comments before I start writing any code. Those comments usually include any pseudocode I can think of to document my thoughts about how to build each section. When I'm done, I sometimes remove the unnecessary comments, and sometimes I don't. Since no one else ever sees it, what does it matter if I leave it cluttered by my notes? :-D

                                      "A Journey of a Thousand Rest Stops Begins with a Single Movement"

                                      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