• hstde@feddit.de
      link
      fedilink
      arrow-up
      13
      ·
      1 year ago

      There’s a comment for you to explain the why.

      Rule of thumb: code explains the how and what, comments explain the why.

    • magic_lobster_party@kbin.social
      link
      fedilink
      arrow-up
      5
      ·
      1 year ago

      Those cases are rare. Often the most basic solution is good enough.

      If you have to write complex code, then you should write a comment (write the name of the algorithm for example).

      • Test_Tickles@lemmynsfw.com
        link
        fedilink
        English
        arrow-up
        2
        ·
        1 year ago

        Rare?
        Where do you guys work that all you do is write basic AI generatable code?
        The only thing I can think is that you are a bunch of freelance devs who never have to maintain anything or add functionality to old code. Either that or you are all new and are just full of theoretical bullshit that you read on the internet.

          • Test_Tickles@lemmynsfw.com
            link
            fedilink
            English
            arrow-up
            2
            ·
            1 year ago

            Yes. I mean often enough that I wouldn’t call it rare.

            You are a front-end js/ts devel, aren’t you? That makes sense. I can understand why you would have such a skewed view of programming. When everything you write is disposable and might be scrapped every 2 - 3 years, comments would seem like nonsense and a waste of time.

            But that is definitely not everyone’s experience. More than half the code I have written has had a minimum 15 year life expectancy. Comments are essential to remember what I was doing in whatever random language I had to use at a given point. I might not comment on “x++;” but I sure as shit will on “x += (xDelta * yDelta + 31) / 32;” Actually, that’s not true, if the logic is complex enough for the rest of the code chunk, I might just comment on “x++;” to make it clear what x is in this case and why it needs to unconditionally be incremented here. Even if the reason seems ridiculously obvious right now. Because that shit might not be obvious at all in 10 years.

            • magic_lobster_party@kbin.social
              link
              fedilink
              arrow-up
              1
              ·
              1 year ago

              My job title is actually a data scientist. I’ve seen few pieces of code that couldn’t have been made more explainable by just using a more clear and concise naming of variables and functions. Don’t try to be so overly clever with your single letter variables and Greek alphabet. Just explain what it is with a good name.

              If I’m lucky I get to write a cool new algorithm once per quarter or so. Usually it’s just a standard algorithm that has an explanation in a Wikipedia page, so I just give the name of the algorithm and a link to that page.

              Most of the time we’re just doing basic data processing building on the preexisting solutions. These generally don’t need comments.

              The worst code is usually when someone has tried to be overly clever (including myself). Often a simple and straightforward solution had been overlooked. Simple solutions are easier to understand and maintain. Anyone can just look at the code and get a sense of what’s going on without any comments. In many cases a simple solution has also more accurate and faster to compute.

              In my work, having explainable results far outweighs anything else, and you don’t get that by writing difficult to understand code.