• QuazarOmega@lemy.lol
    link
    fedilink
    English
    arrow-up
    4
    ·
    edit-2
    1 month ago

    Making up an example on the spot is kinda difficult for me, but I’d look at it this way with a bold statement, you should hope that most code won’t need comments. Let’s exclude documentation blocks that are super ok to be redundant as they should give a nice, consistent, human readable definition of what x thing does (function, constant, enum, etc.) and maybe even how to use it if it’s non-intuitive or there are some quirks with it.
    After that, you delve in the actual meat of the code, there are ways to make it more self explanatory like extracting blocks of stuff into functions, even when you don’t think it’ll be used again, to be used with care though, as not to make a million useless functions, better is to structure your code so that an API is put into place, enabling you to write code that naturally comes out high level enough to be understood just by reading, this thing is very difficult for me to pinpoint though, because we think of high level code as abstractions, something that turns the code you write from describing the what rather than the how, but really, it’s a matter of scope, a print statement is high level if the task is to print, but if the task is to render a terminal interface then the print becomes low level, opposite is also true, if you go down and your task is to put a character onto stdout, then the assembly code you’d write might be high level. What I mean to say is that, once you have defined the scope, then you can decide what level of knowledge to expect of the reader when looking at your code, from there, if some process feels fairly convoluted, but it doesn’t make sense to build an abstraction over it, then it is a good place to put a comment explaining why you did that, and, if it’s not really clear, even what that whole block does

    • jonathanvmv8f@lemm.ee
      link
      fedilink
      arrow-up
      1
      ·
      1 month ago

      Interesting to see your opinion on how commenting shouldn’t be mandatory. I specifically go the extra mile to ensure my code is readable for everyone, by naming my variables and functions to be as self-explanatory as possible and breaking down long expressions to store chunks in variables. This is why I was feeling confused as to what more I could add to explain my code better, though I must admit there are still considerable complex portions in some of my projects that would appreciate similar simplification.

      • QuazarOmega@lemy.lol
        link
        fedilink
        English
        arrow-up
        3
        ·
        1 month ago

        Yes, I feel like some kind of bell should ring in your brain when something needs to be commented, most often if you struggled to write out the solution or you had to do a lot of digging from various places to achieve the final resulting piece of code, it doesn’t make a lot of sense to pressure yourself into thinking you should comment everything, because some knowledge has to be assumed, nowadays you could even add that if someone completely extraneous to the codebase entered without any knowledge, they could feed the parts of code they need to understand into some LLM to get a feel for what they’re looking at, with further feedback from actual devs though, you never know what random bs they might write.
        Good one on the variables to store results of expressions, I agree with that method, though I always forget to do that because I get so lost in the pride of writing that convoluted one-liner that I think, “oh yeah, this is perfectly beautiful and understandable 😇”, I have to check myself more on that.

        complex portions in some of my projects that would appreciate similar simplification

        So I’m not alone on that haha.

        This is why […] better

        Sorry, what’s the subject of that?

        • flashgnash@lemm.ee
          link
          fedilink
          arrow-up
          2
          ·
          1 month ago

          This is unfortunate for new programmers cause I think it’s some kind of learned instinct rather than a hard rule

        • jonathanvmv8f@lemm.ee
          link
          fedilink
          arrow-up
          2
          ·
          edit-2
          1 month ago

          This is why […] better

          Sorry, what’s the subject of that?

          I was just referring to my original question i.e. how I should write comments in my code to explain its working if I have already done so in the code itself