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. Programming Question

Programming Question

Scheduled Pinned Locked Moved The Lounge
wpfcsharpcombusinessarchitecture
88 Posts 42 Posters 3 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 Maximilien

    (c++ centric answer) Stupid/Verbose/Out of sync comments are worse than no comments. All functions and parameter names should be self-descriptive; use proper user defined types instead of POD (if you have a student ID, let the type be a StudentID instead of a int) All functions should be as simple as possible; they should do 1 thing only; methods should be const if they do not modify the data, Special cases should be fixed by proper code design than by adding a comment in the code (i.e. parameter can be NULL or not, or sometimes ... ), if a method cannot receive a NULL pointer, just change the type to a reference; let the client of that function clean up its own code to not have a null pointer. When there are good coding practice, comments are not "that" necessary, needed in some instances, but not always necessary.

    Nihil obstat

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

    Maximilien wrote:

    Stupid/Verbose/Out of sync comments are worse than no comments.

    This is similar to the many many dev. arguments where the adage is something done badly is bad - well, of course bad comments are bad - so is bad code! Ill maintained comments can be bad - so can ill maintained code. CODE and COMMENTS are all part of the code base - i don't think they should be thought of as something separate and they should be included in code reviews. A comment that says // Update the Customer for a method public void UpdateCustomer() is obviously a waste of time, and is often the sort of comment that people who don't think they should be made to comment code put in - in a sort of "see, i told you commenting was a waste of time!" But what does UpdateCustomer() do? Does it update the local customer object, persist it to the database, change some properties? Evidently UpdateCustomer is an insufficient comment and an insufficient method name - but ... I think in a sense there are two types of comment... 1. Code comments. This is the sort of comment you are talking about, I think. 2. Business comments - this explains to the programmer the business of the function or code block. A method name of

    public StockItem FindTheStockItemWithTheEarliestSellByDateThatIsEitherOfTheTypePassedOrOneOfTheGenericTypesSimilarToThatTypeThatIsAcceptableByTheCustomer(StockType stockType, Customer customer, Date dateToCheck)
    {
    }

    Is simply less legible than the equivalent

    /*
    Find the stock item with the earliest sel by date that is either of the type passed, or one of the generic types, similar to that type, that is acceptable by the customer
    */
    public StockItem FindStock((StockType stockType, Customer customer, Date dateToCheck)
    {

    }

    MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

    1 Reply Last reply
    0
    • L Lost User

      "My code doesn't need comments because it is self documenting, all methods are small and have single functionality, and any business documentation should be provided by the specification and not the code." Discuss.

      MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

      G Offline
      G Offline
      Gary R Wheeler
      wrote on last edited by
      #27

      ... and that's when the fight started.

      Software Zen: delete this;

      1 Reply Last reply
      0
      • A AspDotNetDev

        Simples:

        double CalculateTax(double fine) {
        var notAComment1 = "Calculate the tax based on the rate from the database.";
        return fine * GetTaxRate();
        }

        Seriously, though, I like comments, even for private members. And I like everything (except for constants and member variables, which typically have a corresponding property) to have a comment. I mostly read the comments rather than the code when I'm quickly navigating through code. I don't want a funky looking uncommented section to slow me down. I also write XML comments so intellisense helps me avoid having to go to the definition to find more info.

        Thou mewling ill-breeding pignut!

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

        AspDotNetDev wrote:

        even for private members

        So you tatoo your privates?

        If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams
        You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering.-Wernher von Braun
        Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein

        H 1 Reply Last reply
        0
        • L Lost User

          "My code doesn't need comments because it is self documenting, all methods are small and have single functionality, and any business documentation should be provided by the specification and not the code." Discuss.

          MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

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

          In general I agree with the goal of having self-documenting code. However, that's not always possible or feasible. Judicious, and appropriate commenting is as important as well architected and well implemented code. The comments should inform me why you're doing what you're doing, not what you're doing. For that, I can read the code. If it's not immediately obvious from the code why something is being done, then a comment, as short as possible, is necessary. Finally, when the code is updated, please, please update the comments. I've seen too much code where the comments no longer apply.

          If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams
          You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering.-Wernher von Braun
          Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein

          J 1 Reply Last reply
          0
          • L Lost User

            "My code doesn't need comments because it is self documenting, all methods are small and have single functionality, and any business documentation should be provided by the specification and not the code." Discuss.

            MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #30

            _Maxxx_ wrote:

            because it is self documenting, all methods are small and have single functionality

            All of those are opinions.

            _Maxxx_ wrote:

            and any business documentation should be provided by the specification and not the code.

            If in fact the business specification is a technical specification and well done it is possible that is true. But since most business specifications are not technical specifications the first part of the assertion becomes questionable. And since many are not kept completely up to date throughout a project the second part assertion is also questionable. Not to mention of course that the second part is...again...an opinion.

            T 1 Reply Last reply
            0
            • _ _Damian S_

              I initial and date all my code changes in a comment near the change - you have no idea how many times I have been able to point to that in code for a client and say "No, noone has changed this since..." which is particularly handy if something strange goes on...

              Silence is golden... but duct tape is silver!! Booger Mobile - My bright green 1964 Ford Falcon - check out the blog here!! | If you feel generous - make a donation to Camp Quality!!

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #31

              I use source control and check in with relevent comments - you have no idea how many times I have been able to point to that in code for a client and say "No, noone has changed this since..." which is particularly handy if something strange goes on.

              1 Reply Last reply
              0
              • M Maximilien

                (c++ centric answer) Stupid/Verbose/Out of sync comments are worse than no comments. All functions and parameter names should be self-descriptive; use proper user defined types instead of POD (if you have a student ID, let the type be a StudentID instead of a int) All functions should be as simple as possible; they should do 1 thing only; methods should be const if they do not modify the data, Special cases should be fixed by proper code design than by adding a comment in the code (i.e. parameter can be NULL or not, or sometimes ... ), if a method cannot receive a NULL pointer, just change the type to a reference; let the client of that function clean up its own code to not have a null pointer. When there are good coding practice, comments are not "that" necessary, needed in some instances, but not always necessary.

                Nihil obstat

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #32

                Maximilien wrote:

                Stupid/Verbose/Out of sync comments are worse than no comments.

                However that is also a good indication that code reviews are not occurring or at least they are not being done correctly.

                T 1 Reply Last reply
                0
                • T TheGreatAndPowerfulOz

                  In general I agree with the goal of having self-documenting code. However, that's not always possible or feasible. Judicious, and appropriate commenting is as important as well architected and well implemented code. The comments should inform me why you're doing what you're doing, not what you're doing. For that, I can read the code. If it's not immediately obvious from the code why something is being done, then a comment, as short as possible, is necessary. Finally, when the code is updated, please, please update the comments. I've seen too much code where the comments no longer apply.

                  If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams
                  You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering.-Wernher von Braun
                  Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein

                  J Offline
                  J Offline
                  jschell
                  wrote on last edited by
                  #33

                  ahmed zahmed wrote:

                  ...is as important as well architected and well implemented code.

                  Yes but that there is probably the reason comments are needed because after all how many developers do you come across who are willing to say that they are creating code that is poorly architected and poorly implemented? Excluding some (but far from all) beginning programmers I have very seldom come across anyone that felt that they were producing poor code. So one might then conclude that no one should comment code because everyone is producing great code. And then there is also the implicit problem that presumes that that good is the only possible solution that many people would come up with. Thus everyone should understant it in the first place because it is the only possible solution. Rather than one of ten or even one of 100.

                  T 1 Reply Last reply
                  0
                  • J jschell

                    ahmed zahmed wrote:

                    ...is as important as well architected and well implemented code.

                    Yes but that there is probably the reason comments are needed because after all how many developers do you come across who are willing to say that they are creating code that is poorly architected and poorly implemented? Excluding some (but far from all) beginning programmers I have very seldom come across anyone that felt that they were producing poor code. So one might then conclude that no one should comment code because everyone is producing great code. And then there is also the implicit problem that presumes that that good is the only possible solution that many people would come up with. Thus everyone should understant it in the first place because it is the only possible solution. Rather than one of ten or even one of 100.

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

                    Nothing you've said, while true, negates what I've said. In fact, your statements support what I've said. Note that I said good comments are as important as good code, that goes at least doubly-so for "bad" code. Again, explain why you're doing what you're doing, not what. And yes, I've worked with some people who've admitted to producing "bad" code (myself included). Usually it was only meant to be a short-lived workaround, demo, or prototype, but got left in the product by time crunch or management decision. And, btw, the only viable, real solutions are the ones I come up with! :laugh: ;P :rolleyes:

                    If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams
                    You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering.-Wernher von Braun
                    Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein

                    1 Reply Last reply
                    0
                    • J jschell

                      Maximilien wrote:

                      Stupid/Verbose/Out of sync comments are worse than no comments.

                      However that is also a good indication that code reviews are not occurring or at least they are not being done correctly.

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

                      :thumbsup::thumbsup::thumbsup::thumbsup::thumbsup:

                      If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams
                      You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering.-Wernher von Braun
                      Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein

                      1 Reply Last reply
                      0
                      • J jschell

                        _Maxxx_ wrote:

                        because it is self documenting, all methods are small and have single functionality

                        All of those are opinions.

                        _Maxxx_ wrote:

                        and any business documentation should be provided by the specification and not the code.

                        If in fact the business specification is a technical specification and well done it is possible that is true. But since most business specifications are not technical specifications the first part of the assertion becomes questionable. And since many are not kept completely up to date throughout a project the second part assertion is also questionable. Not to mention of course that the second part is...again...an opinion.

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

                        opinions, there're like assholes: everyone's got one and they're usually full of shit! :laugh:

                        If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams
                        You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering.-Wernher von Braun
                        Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein

                        1 Reply Last reply
                        0
                        • L Lost User

                          _Maxxx_ wrote:

                          cow-worker

                          Someone who works the cow?

                          K Offline
                          K Offline
                          kmoorevs
                          wrote on last edited by
                          #37

                          :laugh: :laugh:

                          "Go forth into the source" - Neal Morse

                          1 Reply Last reply
                          0
                          • L Lost User

                            "My code doesn't need comments because it is self documenting, all methods are small and have single functionality, and any business documentation should be provided by the specification and not the code." Discuss.

                            MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

                            X Offline
                            X Offline
                            Xittenn
                            wrote on last edited by
                            #38

                            I see nothing wrong with this approach. I work in scientific computing and rarely see commented code. I do not comment my own code. I used to work with Microsoft Dynamics, nothing was commented. IIRC when working with the Source Engine or UT2004 in GD none of the source code had comments. Any comments I have ever seen have simply reflected what was obvious from the function name. If you can't read code how are you doing your job? Naming convention is far more important and far less intrusive. Good documentation in my opinion is far more helpful. If you want to comment use hyperlinks to electronic documentation.

                            R 1 Reply Last reply
                            0
                            • T TheGreatAndPowerfulOz

                              AspDotNetDev wrote:

                              even for private members

                              So you tatoo your privates?

                              If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams
                              You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering.-Wernher von Braun
                              Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein

                              H Offline
                              H Offline
                              Harry Neethling
                              wrote on last edited by
                              #39

                              :wtf: Can't talk out of experience but that must hurt like there is no tomorrow. :laugh:

                              1 Reply Last reply
                              0
                              • L Lost User

                                "My code doesn't need comments because it is self documenting, all methods are small and have single functionality, and any business documentation should be provided by the specification and not the code." Discuss.

                                MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

                                M Offline
                                M Offline
                                Mark Hurd
                                wrote on last edited by
                                #40

                                In-line comments are debatable: it depends upon how "self-documenting" your names and language are. Pre-ambles are a must, especially when your IDE will give you intellisense when they're present.

                                Regards, Mark Hurd, B.Sc.(Ma.) (Hons.)

                                1 Reply Last reply
                                0
                                • L Lost User

                                  "My code doesn't need comments because it is self documenting, all methods are small and have single functionality, and any business documentation should be provided by the specification and not the code." Discuss.

                                  MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

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

                                  Comments are great when done well. Unfortunately this is rarely the case... The following is a real world example...

                                  // Save the customer.
                                  product.Save();

                                  Another one:

                                  // i is the index.
                                  int i = 0;

                                  Just call the friggin variable 'index' and drop the comment!

                                  // See mail QQ (it wasn't actually QQ, they were two other initials).
                                  this.DoSomething();

                                  Who the hell is QQ and what mail did he send? It's also nice to find change logs in code, comments just for the sake of commenting, comments that describe code that isn't there anymore, a piece of comment that's littered all over your codebase... Comments are lines of code that should be maintained like any other line of code. I hate it when it's perfectly clear to me what a piece of code does, but someone thought it would be nice to still comment it. Now I have to read/adjust the code AND the comment... Comments get out of date, comments lie, comments can be as obscured and unclear as the code itself and comments can be an excuse to write bad code (the code is commented, so it's clear what the code does, right?)... In my opinion comments are a necessary evil at best. The code should speak for itself. Code does not lie and is never outdated. Now it's up to the programmer to make it as clear and readable as possible (unfortunately this is not easy and just straight impossible for some programmers...). But then again, writing good comments is not easy either. I always imagine a writer writing a book. Would he have to comment his paragraphs?

                                  // What I mean to say in the following section is that Frodo takes his ring to Mount Doom to destroy it, but is attacked by the creature we know as Gollum.
                                  (actual text) jkfhadj.kbvadulvkb;jlbn av,lnvaduk aa;vn ;ub jb kgbfleukg vbvs
                                  dfgg afgh;a jafhuhg jdbakjbfjgdfgbukfha afdgfhfdh g fdgfadfgafg fgafdgarerthgaer9df/d.kj fgjdakh
                                  adfg f8oyfagh ahg hgafoihlawr;ahbnfbjn afad;jkh gfnadjkh89fadyg

                                  Unfortunately that is what a lot of code really looks like... :sigh:

                                  It's an OO world.

                                  public class Naerling : Lazy<Person>{
                                  public void DoWork(){ throw new NotImplementedException(); }
                                  }

                                  1 Reply Last reply
                                  0
                                  • L Lost User

                                    I think comments are worth their weight so ling as they are written well - describing the business reasons not the technology (unless the tech is crafty, unusual or complex) when I sit down to write a method, I start by calling it something

                                    public double CalculateTax(double fine)
                                    {
                                    }

                                    Then I comment it

                                    ///
                                    /// Calculate the tax, taking into account the fine passed.
                                    /// Requires that the tax rate is retrievable from the TaxService
                                    ///

                                    Then I might write some test code just to get it building

                                    ///
                                    /// Calculate the tax, taking into account the fine passed.
                                    /// Requires that the tax rate is retrievable from the TaxService
                                    ///
                                    public double CalculateTax(double fine)
                                    {
                                    // TODO: Perform the tax calculation
                                    return 34567.89;
                                    }

                                    Then I start to flesh out the method by way of comments

                                    ///
                                    /// Calculate the tax, taking into account the fine passed.
                                    /// Requires that the tax rate is retrievable from the TaxService
                                    ///
                                    public double CalculateTax(double fine)
                                    {
                                    // Get the tax rate using the appropriate service
                                    // calculate the fine (I think it is just fine * tax rate but need to check with spec!)
                                    }

                                    Then, finally, I write the code

                                    ///
                                    /// Calculate the tax, taking into account the fine passed.
                                    /// Requires that the tax rate is retrievable from the TaxService
                                    ///
                                    public double CalculateTax(double fine)
                                    {
                                    // Get the tax rate using the appropriate service
                                    double taxRate = GetTaxRate();
                                    // calculate the fine
                                    tax = taxRate * fine;

                                    return tax ;
                                    

                                    }

                                    That way, I can remember where I was if I get interrupted, the comments aren't an afterthought, they are a part of the process and, if I get hit by the Programmer bus, someone else should be able to see what I was doing. Obv. the example is small and trivial, but that's how I work and I fail to understand the 'don't need comments' brigade. What I do hate is/...

                                    // Multiply the rate by the amount
                                    return rate * amount;

                                    which is simply a case of bad commenting in my book - it is not necessary to comment every step

                                    MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

                                    M Offline
                                    M Offline
                                    M Towler
                                    wrote on last edited by
                                    #42

                                    I am more of an adherent to the "comments are bad" brigade, so will offer a counter view. To be more precise I agree more with the statement that "all comments are apologies [for not making the code self documenting]". I do like statements of intent, not implementation. In the example given, I like the function comment, it states the intent and required preconditions.

                                    ///
                                    /// Calculate the tax, taking into account the fine passed.
                                    /// Requires that the tax rate is retrievable from the TaxService
                                    ///

                                    Whereas the following two appear to me to be worthless and merely clutter up the code. I can tell the first line is getting the tax rate, by the call to the self documenting function, and I know the rest is calculating the value because it is obviously a calculation and it corresponds with what the statement of intent in the function documentation was.

                                    // Get the tax rate using the appropriate service
                                    double taxRate = GetTaxRate();
                                    // calculate the fine

                                    Comments like the above make code harder to read IMO just due to volume of text. More importantly they are often not updated perfectly when code is maintained, especially when scripted edits are performed; I have been misled in the past by reading the comments and the two not corresponding and this has cost me time, so I would prefer to just read the code and not be distracted. Also where they are a repeat of the code they fail the DRY principle. Like yourself I do write comments during the process of implementation, if I want to sketch out some pseudo code in a comment then slowly turn it into code (just in case I win the lottery and someone else has to finish it off). The difference for me is that once I have finished the code, the comments will have been almost entirely replaced by the code.

                                    G J 2 Replies Last reply
                                    0
                                    • M M Towler

                                      I am more of an adherent to the "comments are bad" brigade, so will offer a counter view. To be more precise I agree more with the statement that "all comments are apologies [for not making the code self documenting]". I do like statements of intent, not implementation. In the example given, I like the function comment, it states the intent and required preconditions.

                                      ///
                                      /// Calculate the tax, taking into account the fine passed.
                                      /// Requires that the tax rate is retrievable from the TaxService
                                      ///

                                      Whereas the following two appear to me to be worthless and merely clutter up the code. I can tell the first line is getting the tax rate, by the call to the self documenting function, and I know the rest is calculating the value because it is obviously a calculation and it corresponds with what the statement of intent in the function documentation was.

                                      // Get the tax rate using the appropriate service
                                      double taxRate = GetTaxRate();
                                      // calculate the fine

                                      Comments like the above make code harder to read IMO just due to volume of text. More importantly they are often not updated perfectly when code is maintained, especially when scripted edits are performed; I have been misled in the past by reading the comments and the two not corresponding and this has cost me time, so I would prefer to just read the code and not be distracted. Also where they are a repeat of the code they fail the DRY principle. Like yourself I do write comments during the process of implementation, if I want to sketch out some pseudo code in a comment then slowly turn it into code (just in case I win the lottery and someone else has to finish it off). The difference for me is that once I have finished the code, the comments will have been almost entirely replaced by the code.

                                      G Offline
                                      G Offline
                                      greldak
                                      wrote on last edited by
                                      #43

                                      M Towler wrote:

                                      Whereas the following two appear to me to be worthless and merely clutter up the code. I can tell the first line is getting the tax rate, by the call to the self documenting function, and I know the rest is calculating the value because it is obviously a calculation and it corresponds with what the statement of intent in the function documentation was. // Get the tax rate using the appropriate service double taxRate = GetTaxRate(); // calculate the fine

                                      That comment adds meaning to the code - it tells me that the rate is obtained from a service as opposed to being determined within the function. Your argument would have more vilidity if the function was renamed from GetTaxRate to GetTaxRateFromAppropriateService although even then I would expect some comment within the function to define what "Approprate" means

                                      M P 2 Replies Last reply
                                      0
                                      • L Lost User

                                        "My code doesn't need comments because it is self documenting, all methods are small and have single functionality, and any business documentation should be provided by the specification and not the code." Discuss.

                                        MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

                                        7 Offline
                                        7 Offline
                                        77465
                                        wrote on last edited by
                                        #44

                                        Code can and should to be self documenting, thus the comments explaining what it does are both unnecessary and harmful. The problem is that claiming that does not automatically make the code self documenting. However, comments explaining why the code does what it does are absolutely necessary. The code itself is not the best place for such comments, they are easier to use when placed into a separate document. Thus, if nothing but "code is self documenting" is said about comments, it is likely the coder does not understand the job. The "provided by specification" part makes me think that is the fact here since specification cannot answer the why. The big WHY is being understood while coding.

                                        R 1 Reply Last reply
                                        0
                                        • G greldak

                                          M Towler wrote:

                                          Whereas the following two appear to me to be worthless and merely clutter up the code. I can tell the first line is getting the tax rate, by the call to the self documenting function, and I know the rest is calculating the value because it is obviously a calculation and it corresponds with what the statement of intent in the function documentation was. // Get the tax rate using the appropriate service double taxRate = GetTaxRate(); // calculate the fine

                                          That comment adds meaning to the code - it tells me that the rate is obtained from a service as opposed to being determined within the function. Your argument would have more vilidity if the function was renamed from GetTaxRate to GetTaxRateFromAppropriateService although even then I would expect some comment within the function to define what "Approprate" means

                                          M Offline
                                          M Offline
                                          M Towler
                                          wrote on last edited by
                                          #45

                                          I feel that level of commenting or descriptive naming would be breaking encapsulation, by exposing the implementation of GetTaxRate. I will go further and say that it would be repeating some of the documentation of GetTaxRate, so fails DRY. This piece of code is simply doing a calculation, so it calls a function to get the rate. It does not really need to have knowledge of where the rate came from to do its task, so it is best not to give it this knowledge. Having minimal information in this function means it will not become out of date when the implementation of GetTaxRate changes, such as to add a local cache of the value obtained from the service, using a database of rates or some other alteration. In summary, what I am really saying is that comments of this type raise the cost of maintenance, as each later change will have to find and change multiple comments in addition to the code itself, or else risk misleading future maintainers.

                                          L 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