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

code comments

Scheduled Pinned Locked Moved The Lounge
question
65 Posts 27 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.
  • Richard DeemingR Richard Deeming

    TheGreatAndPowerfulOz wrote:

    what the code is doing but not why

    Because "check for null so we don't get a NullReferenceException when we access the properties of someObj" would be so much better! :rolleyes:


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

    T Offline
    T Offline
    TheGreatAndPowerfulOz
    wrote on last edited by
    #3

    Yeah, that's worse

    #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

    1 Reply Last reply
    0
    • T TheGreatAndPowerfulOz

      I hate code comments that say what the code is doing but not why.

      /* check for null */
      if (someObj != null) {...}

      Really? Argh!

      #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

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

      It gets better: /* check for null */ if (string.IsNullOrEmpty(something)) { ... }

      T 1 Reply Last reply
      0
      • L Lost User

        It gets better: /* check for null */ if (string.IsNullOrEmpty(something)) { ... }

        T Offline
        T Offline
        TheGreatAndPowerfulOz
        wrote on last edited by
        #5

        Indeedly!

        #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

        1 Reply Last reply
        0
        • T TheGreatAndPowerfulOz

          I hate code comments that say what the code is doing but not why.

          /* check for null */
          if (someObj != null) {...}

          Really? Argh!

          #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

          R Offline
          R Offline
          R Giskard Reventlov
          wrote on last edited by
          #6

          When no comments are actually better. :)

          T 1 Reply Last reply
          0
          • R R Giskard Reventlov

            When no comments are actually better. :)

            T Offline
            T Offline
            TheGreatAndPowerfulOz
            wrote on last edited by
            #7

            Yep. My favorite:

            // Declare variables

            #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

            R B 2 Replies Last reply
            0
            • T TheGreatAndPowerfulOz

              Yep. My favorite:

              // Declare variables

              #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

              R Offline
              R Offline
              R Giskard Reventlov
              wrote on last edited by
              #8

              I prefer:

              #region Private Members
              // This should always start at 101 or kittens will die!
              int thingy = 101;
              #endregion

              :)

              R 1 Reply Last reply
              0
              • T TheGreatAndPowerfulOz

                I hate code comments that say what the code is doing but not why.

                /* check for null */
                if (someObj != null) {...}

                Really? Argh!

                #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

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

                Well, you've got a lot of "developer" responses, so far, so how about I add a "someone who has to explain to your customers' developers why your developers are such @rseholes" response. You're absolutely right. Because there are a thousand reasons for checking for null values, there are a thousand things that could be added to expand on what the statement does, e.g.

                /* check for null, because if someObj is null, then bad data has been received from DataReceiver */
                if (someObj != null) {...}

                /* check for null because if someObj is not null, at this point, then something has gone wrong. Look for errs 132-189*/
                if (someObj != null) {...}

                /* check for null because if someObj is null, then someSubObj is defective*/
                if (someObj != null) {...}

                There's almost never an excuse for a line of doc that says exactly the same as the line of code. If the who/why/where/when/what is not needed, then the doc is not needed. If the who/why/where/when/what is needed, then the doc is needed, and just keep in mind that some other poor b*st*rd has to try and make your program useful.

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

                M 1 Reply Last reply
                0
                • M Mark_Wallace

                  Well, you've got a lot of "developer" responses, so far, so how about I add a "someone who has to explain to your customers' developers why your developers are such @rseholes" response. You're absolutely right. Because there are a thousand reasons for checking for null values, there are a thousand things that could be added to expand on what the statement does, e.g.

                  /* check for null, because if someObj is null, then bad data has been received from DataReceiver */
                  if (someObj != null) {...}

                  /* check for null because if someObj is not null, at this point, then something has gone wrong. Look for errs 132-189*/
                  if (someObj != null) {...}

                  /* check for null because if someObj is null, then someSubObj is defective*/
                  if (someObj != null) {...}

                  There's almost never an excuse for a line of doc that says exactly the same as the line of code. If the who/why/where/when/what is not needed, then the doc is not needed. If the who/why/where/when/what is needed, then the doc is needed, and just keep in mind that some other poor b*st*rd has to try and make your program useful.

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

                  M Offline
                  M Offline
                  Member 12345678
                  wrote on last edited by
                  #10

                  I am one of the other poor b*st*rd now. The original developers think their code is so beautiful and wonderful that no comment is needed to explain. They think I should ask if I do not understand. It will take longer than my lifetime to finish project this way.

                  F M 2 Replies Last reply
                  0
                  • R R Giskard Reventlov

                    I prefer:

                    #region Private Members
                    // This should always start at 101 or kittens will die!
                    int thingy = 101;
                    #endregion

                    :)

                    R Offline
                    R Offline
                    Rajeev Jayaram
                    wrote on last edited by
                    #11

                    :laugh: :laugh:

                    My blog - www.FaceLaptop.com

                    1 Reply Last reply
                    0
                    • T TheGreatAndPowerfulOz

                      I hate code comments that say what the code is doing but not why.

                      /* check for null */
                      if (someObj != null) {...}

                      Really? Argh!

                      #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

                      Sander RosselS Offline
                      Sander RosselS Offline
                      Sander Rossel
                      wrote on last edited by
                      #12

                      I really dislike code comments in general[^] X| Currently, I have a coworker who writes comments like:

                      // Declare a variable.
                      int i;
                      // Assign the variable.
                      i = 42;

                      He doesn't do it all the time, but such comments are common practice. Learn to friggin code so you don't have to remind yourself of how to declare and assign a friggin variable! :mad:

                      Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

                      Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                      Regards, Sander

                      Z S 2 Replies Last reply
                      0
                      • T TheGreatAndPowerfulOz

                        I hate code comments that say what the code is doing but not why.

                        /* check for null */
                        if (someObj != null) {...}

                        Really? Argh!

                        #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

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

                        Not everybody can be a master of the obvious. :-)

                        The language is JavaScript. that of Mordor, which I will not utter here
                        This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                        "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                        1 Reply Last reply
                        0
                        • T TheGreatAndPowerfulOz

                          I hate code comments that say what the code is doing but not why.

                          /* check for null */
                          if (someObj != null) {...}

                          Really? Argh!

                          #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

                          B Offline
                          B Offline
                          BillWoodruff
                          wrote on last edited by
                          #14

                          Well, gosh ... if I only had a Baht (about 2.86 US cents) for every time I've written: if(whatever != null) ... However, is it, perhaps, useful to distinguish two scenarios: 1. checking for null when there's no need to throw an error if the whatever is null, and, the code context makes it absolutely clear what's going on. 2. checking for null when you absolutely should throw an error on null I recognize there are "purists" who would like to see you throw errors in all cases. And, how about this: disclaimer: _this is an experiment I did a while ago; I am not indirectly expressing the opinion that anyone should use something like th_is:

                          public static class ErrorExtensions
                          {
                          // non-generic version of 'IsNonNull omitted here

                           // note: uses optional compiler directive 'ErrorsToConsole
                          
                           public static bool IsNonNull<T>(this T refobj, bool throwerror = false, string message = null)
                          where T : class
                          {
                              if (refobj == null)
                              {
                                  string mess = string.Format("instance of {0} is null: {1}",typeof(T).Name, message);
                          
                                  if (throwerror)
                                  {
                                      throw new NullReferenceException(mess);
                                  }
                                  else
                                  {
                          

                          #if ErrorsToConsole
                          Console.WriteLine(mess);
                          #endif
                          }

                                  return false;
                              }
                          
                              return true;
                          }
                          

                          }

                          «There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008

                          S 1 Reply Last reply
                          0
                          • T TheGreatAndPowerfulOz

                            I hate code comments that say what the code is doing but not why.

                            /* check for null */
                            if (someObj != null) {...}

                            Really? Argh!

                            #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

                            F Offline
                            F Offline
                            F ES Sitecore
                            wrote on last edited by
                            #15

                            I prefer this style of coding

                            try
                            {
                            someObj.SomeMethod();
                            }
                            catch (Exception)
                            {
                            Response.Redirect("http://www.codeproject.com/search.aspx?q=Object+reference+not+set+to+an+instance+of+an+object&sbo=qa");
                            }

                            C L 2 Replies Last reply
                            0
                            • Richard DeemingR Richard Deeming

                              TheGreatAndPowerfulOz wrote:

                              what the code is doing but not why

                              Because "check for null so we don't get a NullReferenceException when we access the properties of someObj" would be so much better! :rolleyes:


                              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                              S Offline
                              S Offline
                              Slacker007
                              wrote on last edited by
                              #16

                              LMAO at this one. +24

                              1 Reply Last reply
                              0
                              • F F ES Sitecore

                                I prefer this style of coding

                                try
                                {
                                someObj.SomeMethod();
                                }
                                catch (Exception)
                                {
                                Response.Redirect("http://www.codeproject.com/search.aspx?q=Object+reference+not+set+to+an+instance+of+an+object&sbo=qa");
                                }

                                C Offline
                                C Offline
                                charlieg
                                wrote on last edited by
                                #17

                                Here we go :). After so many years of coding, I've learned to cull back on the wordiness of the code. Still, I have a few wonder brothers who "read code so fluently" that things get opaque pretty quick. I try to stay away from the "//declare variables" nonsense and lean toward higher level explanations of code blocks. One thing that *will* set me off is when people don't use version control. So, you'll get interspersed lines of code that are commented out with new lines of code that fix a problem. One leaves this in the source in case if you need to remember what you changed. FFS, use "show changes".

                                Charlie Gilley Do not forget Jacques Hamel "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759

                                D 1 Reply Last reply
                                0
                                • B BillWoodruff

                                  Well, gosh ... if I only had a Baht (about 2.86 US cents) for every time I've written: if(whatever != null) ... However, is it, perhaps, useful to distinguish two scenarios: 1. checking for null when there's no need to throw an error if the whatever is null, and, the code context makes it absolutely clear what's going on. 2. checking for null when you absolutely should throw an error on null I recognize there are "purists" who would like to see you throw errors in all cases. And, how about this: disclaimer: _this is an experiment I did a while ago; I am not indirectly expressing the opinion that anyone should use something like th_is:

                                  public static class ErrorExtensions
                                  {
                                  // non-generic version of 'IsNonNull omitted here

                                   // note: uses optional compiler directive 'ErrorsToConsole
                                  
                                   public static bool IsNonNull<T>(this T refobj, bool throwerror = false, string message = null)
                                  where T : class
                                  {
                                      if (refobj == null)
                                      {
                                          string mess = string.Format("instance of {0} is null: {1}",typeof(T).Name, message);
                                  
                                          if (throwerror)
                                          {
                                              throw new NullReferenceException(mess);
                                          }
                                          else
                                          {
                                  

                                  #if ErrorsToConsole
                                  Console.WriteLine(mess);
                                  #endif
                                  }

                                          return false;
                                      }
                                  
                                      return true;
                                  }
                                  

                                  }

                                  «There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008

                                  S Offline
                                  S Offline
                                  Slacker007
                                  wrote on last edited by
                                  #18

                                  IsNotNull? Also, you should not be purposely throwing an error from an extension method, as part of some divine plan. Just check if it is null or not, that is all. Leave the implementation to the calling party - this helps with code reuse (I may not want to do a console.writeline). My 2 unsolicited cents on your post. :-D

                                  L B 2 Replies Last reply
                                  0
                                  • S Slacker007

                                    IsNotNull? Also, you should not be purposely throwing an error from an extension method, as part of some divine plan. Just check if it is null or not, that is all. Leave the implementation to the calling party - this helps with code reuse (I may not want to do a console.writeline). My 2 unsolicited cents on your post. :-D

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

                                    Christ! Can't you read? He wants 2.86 cents.

                                    Michael Martin Australia "I controlled my laughter and simple said "No,I am very busy,so I can't write any code for you". The moment they heard this all the smiling face turned into a sad looking face and one of them farted. So I had to leave the place as soon as possible." - Mr.Prakash One Fine Saturday. 24/04/2004

                                    1 Reply Last reply
                                    0
                                    • F F ES Sitecore

                                      I prefer this style of coding

                                      try
                                      {
                                      someObj.SomeMethod();
                                      }
                                      catch (Exception)
                                      {
                                      Response.Redirect("http://www.codeproject.com/search.aspx?q=Object+reference+not+set+to+an+instance+of+an+object&sbo=qa");
                                      }

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

                                      Why try to penalize and educate the end user for the sins of a poor coder?

                                      L 1 Reply Last reply
                                      0
                                      • T TheGreatAndPowerfulOz

                                        I hate code comments that say what the code is doing but not why.

                                        /* check for null */
                                        if (someObj != null) {...}

                                        Really? Argh!

                                        #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

                                        R Offline
                                        R Offline
                                        realJSOP
                                        wrote on last edited by
                                        #21

                                        What's more disturbing is that C# *still* doesn't allow this:

                                        if (!someObj) {...}

                                        which would preclude them from having to come up with the bullsh|t "?" operator.

                                        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                                        -----
                                        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                                        -----
                                        When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                                        L Richard DeemingR N K 4 Replies Last reply
                                        0
                                        • Sander RosselS Sander Rossel

                                          I really dislike code comments in general[^] X| Currently, I have a coworker who writes comments like:

                                          // Declare a variable.
                                          int i;
                                          // Assign the variable.
                                          i = 42;

                                          He doesn't do it all the time, but such comments are common practice. Learn to friggin code so you don't have to remind yourself of how to declare and assign a friggin variable! :mad:

                                          Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

                                          Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                                          Regards, Sander

                                          Z Offline
                                          Z Offline
                                          ZurdoDev
                                          wrote on last edited by
                                          #22

                                          Sander Rossel wrote:

                                          Currently, I have a coworker who writes comments like:

                                          Perhaps he used to teach first level coding in high school. :-\

                                          There are only 10 types of people in the world, those who understand binary and those who don't.

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