Comments in code are quite often a code smell. Let’s see what is suboptimal about comments and talk about some strategies to avoid them.

  • war@kbin.social
    link
    fedilink
    arrow-up
    0
    ·
    10 months ago

    I can’t remember the last time I read anything as profoundly fucking retarded as this brainless article. Jesus fucking Christ, I’m speechless.

  • ZILtoid1991@kbin.social
    link
    fedilink
    arrow-up
    0
    ·
    10 months ago

    There were times I’ve done line-by-line commenting to reverse engineer code I’ve written in the past. Also very important in case of Assembly and similar problematic languages.

    I’ve also done some line-by-line stuff to show how my engine is initialized, so people will understand why a given line is there and not just have to know what lines they must type in.

  • magic_lobster_party@kbin.social
    link
    fedilink
    arrow-up
    0
    ·
    10 months ago

    Comments should be used when it’s hard to convey the intent using only code. For example, maybe there’s some unintuitive border case that must handled. Then it could be useful to add some explanation with a comment.

    I also like to add comments when I find some clever solution/workaround on the web. For example, some stack overflow answer or Wikipedia page of some algorithm.

    • Big P@feddit.uk
      link
      fedilink
      English
      arrow-up
      0
      ·
      10 months ago

      Comments are also useful when something appears to be wrong but there’s a good reason why it’s like that

  • ono@lemmy.ca
    link
    fedilink
    English
    arrow-up
    0
    ·
    10 months ago

    I guess one could understand why the author believes this if his experience has been (mostly) limited to poorly commented code.

  • pirate526@kbin.social
    link
    fedilink
    arrow-up
    0
    ·
    10 months ago

    While I agree with some of the premises here, I personally disagree that comments are even mostly a problem (a code smell). IMO they’re just as often bad as code is. A developer in a rush, or simply not taking enough care in their work, can produce both bad code and bad comments.

    Perhaps someone who is trying to take care can do more harm in the comments area, when they should be perhaps looking at writing self documenting code, but in my experience they usually go hand in hand.

    I use quite a lot of comments in my code and I wouldn’t regard it as code smell or even messy. I often use comments to logically separate more complex sections of functionality… or discussing how it works and why it’s necessary to exist in the first place. Code can’t always tell you why it’s there…

    I also use docblocks in some libraries, even though types are available, as the published package benefits from having an API document published alongside it. The comments there facilitate its construction.

    I know this article wasn’t bashing every use of comments in code but I feel like it didn’t account for all the positive uses of them either. Teaching developers that a language feature is just mostly bad is irresponsible - we should be encouraging good comment use alongside clear code.

    • burliman@lemm.ee
      link
      fedilink
      arrow-up
      0
      ·
      10 months ago

      The code is the “what”, the comments are the “why”. It’s the mantra I use when making them. So in a fundamental way, they never repeat themselves.

  • mindlessscrollingparrot@kbin.social
    link
    fedilink
    arrow-up
    0
    ·
    10 months ago

    DRY is not an inviolable rule. Some of the most tedious code to debug is because somebody thinks puppies will die if they write the same pair of lines in two different places.

  • darklypure@kbin.social
    link
    fedilink
    arrow-up
    0
    ·
    10 months ago

    Having spent many times having to walk Technical Architects through code, explaining what it does, or trying to introduce new coders to software development, I comment line by line. It’s not just experienced coders that have to look at your code. I will never not do this.

    • burliman@lemm.ee
      link
      fedilink
      arrow-up
      0
      ·
      10 months ago

      This is a little bit of a case in point of why documentation should not be written by the teacher. It should be written by the student. The student takes no knowledge for granted and so will only repeat themselves insofar as they need to. The teacher will either repeat themselves consistently, or just leave massive gaps due to their preexisting knowledge. Even though it wouldn’t be practical for a coder to have some student document their code, I feel like this still illustrates the point. Also it means you have to constantly put yourselves in the shoes of some future student, which seems like a good practice anyway.