Seeing that Uncle Bob is making a new version of Clean Code I decided to try and find this article about the original.

  • dandi8@fedia.io
    link
    fedilink
    arrow-up
    7
    arrow-down
    3
    ·
    3 months ago

    Comments should never be about what is being done. They should only ever be about why it is being done.

    If you write your code like suggested in the book, you won’t need to rely on possibly outdated comments to tell you what’s going on.

    Any comment about “what is being done” can be replaced with extracting the code in question to a separate, well-named method.

    • JackbyDev@programming.devOP
      link
      fedilink
      English
      arrow-up
      6
      ·
      3 months ago

      I disagree about comments should never be about what is being done. If what is being done is not obvious then they’re important. Take assembly code as an example. Or complicated bit operations. I agree the why is more important to document than the what but saying the what is never important seems misguided.

      Also, this may be a semantics thing, but oftentimes the code’s specification is in doc comments. I don’t believe you’re claiming code shouldn’t ever have specifications, this isn’t meant as a gotcha lol.

    • TehPers@beehaw.org
      link
      fedilink
      English
      arrow-up
      5
      arrow-down
      1
      ·
      3 months ago

      I think it’s good to document why things are done, but extracting things out into another function is just documenting what is being done with extra steps. This also comes with a number of problems:

      1. Not all languages are readable. Documenting what is being done is important in some C, or when working with some libraries that have confusing usage syntax.
      2. Not all people reading the code know the language or libraries well. Those people need guidance to understand what the code is trying to do. Function names can of course do this, but…
      3. Not all types can be named in all languages. Some languages have a concept of “opaque types”, which explicitly have no name. If parameter and return types must be specified in that language, working around that restriction may result in unnecessarily complicated code.
      4. Longer files (the result of having dozens of single-use functions) are less readable. Related logic is now detached into pointers that go all over the file all because of an allergic reaction to code comments, where a simple // or # would have made the code just as readable.
      5. Function names can be just as outdated as code comments. Both require upkeep. Speaking from personal experience, I’ve seen some truly misleading/incorrect function names.
    • lolcatnip@reddthat.com
      link
      fedilink
      English
      arrow-up
      4
      ·
      3 months ago

      A function name can be misleading just like a comment can, in the same scenarios and for the same reasons, plus it’s harder to update because you have to change it in at least two places.

      • dandi8@fedia.io
        link
        fedilink
        arrow-up
        2
        arrow-down
        1
        ·
        edit-2
        3 months ago

        And yet, outdated comments are far, far more common than outdated function names.

        Also, if you’re changing a comment which explains the “what”, you should likely change the method name, as well.

        It’s important for the client to know what the method does by looking at the name, so why would you duplicate your effort?

        • lolcatnip@reddthat.com
          link
          fedilink
          English
          arrow-up
          3
          ·
          3 months ago

          And yet, outdated comments are far, far more common than outdated function names.

          Because people don’t try to squeeze a complete description of what a function does into a single identifier, which is what you you would have to do if you want function names to take the place of comments. I for one don’t want to strip all the spaces and punctuation out of my comments so I can use them as function names, and I really didn’t want to read someone else’s code written in that style.