• ImpulseDrive42@lemmy.world
    link
    fedilink
    arrow-up
    52
    arrow-down
    4
    ·
    2 years ago

    I had a old job that told me that code is “self documenting” if you write it “good enough”. And that comments were unnecessary.

    It always annoyed the heck out of me. Comments are imo more helpful than hurtful typically.

    Is it just me? Or am I weird? Lol.

    • Vigge93@lemmy.world
      link
      fedilink
      arrow-up
      20
      ·
      2 years ago

      Comment should describe “why?”, not “how?”, or “what?”, and only when the “why?” is not intuitive.

      The problem with comments arise when you update the code but not the comments. This leads to incorrect comments, which might do more harm than no comments at all.

      E.g. Good comment: “This workaround is due to a bug in xyz”

      Bad comment: “Set variable x to value y”

      Note: this only concerns code comments, docstrings are still a good idea, as long as they are maintained

      • balp@lemmy.world
        link
        fedilink
        arrow-up
        2
        ·
        2 years ago

        Docstring are user documentation, not comments. User documentation, with examples (tests), is always useful.

        • Vigge93@lemmy.world
          link
          fedilink
          arrow-up
          2
          ·
          2 years ago

          As long as it’s maintained. Wrong documentation can often be worse than no documentation.

    • homura1650@lemm.ee
      link
      fedilink
      arrow-up
      10
      ·
      2 years ago

      Have you ever worked in a place where every function/field needed a comment? Most of those comments end up being “This is the <variable name>, or this does <method name>”. Beyond, being useless, those comments are counter productive. The amount of screen space they take up (even if greyed out by the IDE) significantly hurts legability.

        • EpeeGnome@lemm.ee
          link
          fedilink
          English
          arrow-up
          4
          ·
          2 years ago

          The issue with having mandatory useless comments is that any actually useful comments get lost in the noise.

          • Alexstarfire@lemmy.world
            link
            fedilink
            arrow-up
            3
            ·
            2 years ago

            I get what you’re saying. Perhaps I just haven’t had too many variables and such that have had such comments. VsCode shows the comments on hover when you’re in other parts of the code base. Which makes most any comment useful because something that is obvious in one part of the code isn’t immediately obvious in another. Though, that necessitates making comments that actually help you figure that out.

    • Ephera@lemmy.ml
      link
      fedilink
      arrow-up
      9
      ·
      edit-2
      2 years ago

      In my opinion, it strongly depends on what you’re coding.

      Low-level code where you need to initialize array indices to represent certain flags? Absolutely comment the living shit out of that. → See response.
      High-level code where you’re just plumbing different libraries? Hell no, the code is just as easily readable as a comment.

      I do also think that, no matter where you lie in this spectrum, there is always merit to improving code to reduce the need for documentation:

      • Rather than typing out the specification, write a unit/integration test.
      • Rather than describing that a function should only be called in a certain way, make it impossible to do it wrongly by modelling this in your type system.
      • Rather than adding a comment to describe what a block of code does, pull it out into a separate function.
      • Rather than explaining how a snippet of code works, try to simplify it, so this becomes obvious.

      The thing with documentation is that it merely makes it easier to learn about complexity, whereas a code improvement may eliminate this complexity or the need to know about it, because the compiler/test will remember.

      This does not mean you should avoid comments like they’re actively bad. As many others said, particularly the “why” is not expressable in code. Sometimes, it is also genuinely not possible to clean up a snippet of code enough that it becomes digestable.
      But it is still a good idea, when you feel the need to leave a comment that explains something else than the “why”, to consider for a moment, if there’s not some code improvement you should be doing instead.

    • humbletightband@lemmy.dbzer0.com
      link
      fedilink
      arrow-up
      7
      ·
      2 years ago

      I follow these simple rules and encourage my colleagues to do so

      1. If I’m just shuffling jsons, then yes, the code should be self documented. If it’s not, the code should be rewritten.

      2. If I implement some complex logic or algorithm, then the documentation should be written both to tests and in the code. Tests should be as dull as possible.

      3. If I write multithreading, the start, interruption, end, and shared variables should be clearly indicated by all means that I have: comment, documentation, code clearness. Tests should be repeated and waits should not be over 50ms.

    • Daxtron2@startrek.website
      link
      fedilink
      arrow-up
      7
      ·
      2 years ago

      Its definitely a balance. Good code shouldn’t need much commenting, but sometimes you have to do something for a reason that isn’t immediately obvious and that’s when comments are most useful. If you’re just explaining what a snippet does instead of why you’re doing it that way, there’s probably more work to be done.

    • perviouslyiner@lemmy.world
      link
      fedilink
      arrow-up
      7
      ·
      edit-2
      2 years ago

      What they mean is that the variable names and function names are documentation.

      For example changing “for( i in getList() )” to “for( patient in getTodaysAppointments() )” is giving the reader more information that might negate the need for a comment.

    • Andromxda 🇺🇦🇵🇸🇹🇼@lemmy.dbzer0.comOP
      link
      fedilink
      English
      arrow-up
      6
      ·
      2 years ago

      I absolutely agree, and I too hate this stupid idea of “good code documenting itself” and “comments being unnecessary”.
      I have a theory where this comes from. It was probably some manager, who has never written a single line of code, who thought that comments were a waste of time, and employees should instead focus on writing code. By telling them that “good code documents itself”, they could also just put the blame on their employees.
      “Either you don’t need comments or your code sucks because it’s not self-documenting”
      Managers are dumb, and they will never realize that spending a bit of time on writing useful comments may later actually save countless hours, when the project is taken over by a different team, or the people who initially created it, don’t work at the company anymore.

      • ChickenLadyLovesLife@lemmy.world
        link
        fedilink
        English
        arrow-up
        5
        ·
        2 years ago

        I’ve never had a manager that was even aware of the comments vs. no comments issue. If I ever had, I would have just told them that a lack of comments makes the original coder harder to replace.

    • ඞmir@lemmy.ml
      link
      fedilink
      arrow-up
      5
      ·
      2 years ago

      Code is not self documenting when decision trees are created based on some methodology that’s not extremely obvious