• Scratch@sh.itjust.works
    link
    fedilink
    English
    arrow-up
    4
    ·
    10 hours ago

    I think to present rules like this as hard rules, with little explanation and no nuance is harmful to less experienced engineers.

    A prime example here is the Duplicated Code one. Which takes an absolute approach to code duplication, even when the book that is referenced highlights the Rule of Three:

    The Rule of Three
    Here’s a guideline Don Roberts gave me: The first time you do something,
    you just do it. The second time you do something similar, you wince at the
    duplication, but you do the duplicate thing anyway. The third time you do
    something similar, you refactor.
    Or for those who like baseball: Three strikes, then you refactor.
    

    I’ve seen more junior devs bend over backwards, make their code worse and take twice as long to adhere to some rules that are really more what you’d call guidelines than actual rules.

    Sure, try to avoid code duplication, but sometimes duplicating code is better than the wrangling you’d need to do to remove it.

    Making extra changes also leaves extra room for bugs to creep in. So now you need to test the place you were working, and anywhere else you touched because of the refactoring.

    • pcouy@lemmy.pierre-couy.frOP
      link
      fedilink
      arrow-up
      1
      ·
      6 hours ago

      Well it’s in the name, they are code smells, not hard rules.

      Regarding the specific example you cited, I think that with practice it becomes gradually more natural to write reusable functions and methods on the first iteration, removing the need for later DRY-related refactorings.

      PS : I love how your quote for the Rule of Three is getting syntax highlighted xD (You can use markdown quotes by starting quoted lines with > )

  • 1hitsong@lemmy.ml
    link
    fedilink
    arrow-up
    25
    ·
    3 days ago

    I’ve been rallying against clever code for years!

    Sure, it makes you have less lines for your l33t code solutions, but in the real world, it sacrifices the maintainability of code that others will eventually work on.

    Between a clever 1 line fix and maintainable 10 line fix, I’ll choose the 10 line every time.

    • MonkderVierte@lemmy.ml
      link
      fedilink
      arrow-up
      5
      ·
      2 days ago

      10 lines is a bit much, that’s hardly more readable than one.

      Then again, it depends on the language.

    • curbstickle@lemmy.dbzer0.com
      link
      fedilink
      arrow-up
      10
      arrow-down
      1
      ·
      3 days ago

      As an extensive commenter, I completely agree.

      I need to know wtf I was doing, making it convoluted to save a few lines is pointless.

        • curbstickle@lemmy.dbzer0.com
          link
          fedilink
          arrow-up
          8
          arrow-down
          3
          ·
          2 days ago

          “Some people do a bad job commenting and updating comments, so lets not do comments” is not an approach that works for me.

          Most of my code is at the prototype level. I’m concepting something out, usually paired with hardware.

          If someone can’t follow what I’m doing, its going to lead to problems. If a change happens to the hardware being controlled, code will not be good enough on its own.

          Rather than being accepting of bad commenting practice, make comments (and updating them properly) part of good practice. In my experience, It saves time in the long run and leads to better code at the end.

          • pcouy@lemmy.pierre-couy.frOP
            link
            fedilink
            arrow-up
            8
            ·
            1 day ago

            That’s not what I said. I said that comments can often (but not always) be replaced with good and explicit names.

            This can be pushed to some extreme by making functions that only get called at a single place in the code, just for the sake of being able to give a name to the code that’s inside (instead of inlining it and adding a comment that conveys the same informations as the function’s signature)

            It’s definetly not for everyone, but for beginners/juniors it gives something objective they can aim for when trying to build good coding habits

            • curbstickle@lemmy.dbzer0.com
              link
              fedilink
              arrow-up
              6
              arrow-down
              1
              ·
              edit-2
              1 day ago

              I am going to disagree, comments should be an explanation.

              The code is what’s being done, a comment should be why its being done.

              • pcouy@lemmy.pierre-couy.frOP
                link
                fedilink
                arrow-up
                6
                ·
                edit-2
                19 hours ago

                I’m not sure how we disagree. At least, I don’t disagree with you. My whole comment was talking about “what” comments. “Why” comments are a very good thing to have where they’re needed

                • curbstickle@lemmy.dbzer0.com
                  link
                  fedilink
                  arrow-up
                  2
                  arrow-down
                  2
                  ·
                  19 hours ago

                  Not updating comments with code is what I’m talking about - that’s not a comment problem, thats a programmer problem.

                  If they aren’t updating the “why”, that programmer is the problem, not comments.

    • FizzyOrange@programming.dev
      link
      fedilink
      arrow-up
      3
      ·
      20 hours ago

      Any specific ones? I’ve seen this before and I thought I would feel the same way as you before I read them, but actually the vast majority are pretty basic things that are not really arguable.

      It’s definitely nice to have a list like this to point inexperienced colleagues to in code reviews. It’s a bit more authoritative than “trust me bro, I’ve written a lot of code”.

      • Boomkop3@reddthat.com
        link
        fedilink
        arrow-up
        3
        ·
        17 hours ago

        To preface, I think it’s best to focus on what the right approaches are. Not on what to avoid. And when you see a student making a mistake, showing them how a different approach is handier (if possible) is what I suggest you do.

        Having something to point at doesn’t help much

        vertical separation

        This one argues against organizing your code in a way that shares variables are in one place. There are arguments to be made either way, but normally you’d scope your variables in a way that the ones specific to a particular bit of code are not accessible from elsewhere.

        null check

        Suggest writing a custom class to do what most languages can solve with inheritance or even better: the ? syntax.

        inconsistent names / styles

        Yes, it can be annoying. No, clarity is more important than insisting on removing that extra underscore.

        complicated Boolean expression

        They’re advocating the use of a function to replace an expression. Sometimes this works, but the task of a boolean expression is not always easily expressed in a couple words. And so you can end up with misleading function names. Instead, just put a comment in the code.

        callback hell

        Not even a code smell. It’s an issue from back when languages like JavaScript didn’t support promises yet, but callbacks were popular. Cose got hard to read with a little complexities.

        • FizzyOrange@programming.dev
          link
          fedilink
          arrow-up
          1
          ·
          25 minutes ago

          There are arguments to be made either way, but normally you’d scope your variables in a way that the ones specific to a particular bit of code are not accessible from elsewhere.

          Sounds like you agree with that one to me? I’m not sure I follow their arguments about regions there (I’ve never used regions), but the example of declaring a variable in a block way before it is every used is spot on. I’ve seen code written like that and 99% of the time it’s a bad idea. I think a lot of it comes from people who learnt C where you have to do that (or maybe Javascript which has weird rules for var).

          Suggest writing a custom class to do what most languages can solve with inheritance or even better: the ? syntax.

          Yeah I’ll give you that one. They even suggest using Optional as a solution, which is what their “smelly” code did in the first place!

          Yes, it can be annoying. No, clarity is more important than insisting on removing that extra underscore.

          Not sure what your point is here. Of course inconsistent naming is a code smell. Do you want inconsistent names?

          They’re advocating the use of a function to replace an expression. Sometimes this works, but the task of a boolean expression is not always easily expressed in a couple words. And so you can end up with misleading function names. Instead, just put a comment in the code.

          Erm, yeah that’s why this is a code smell. They aren’t saying never have complex boolean expressions - just that if you do you’d better have a good reason because probably you’d be better off splitting it up into named parts.

          callback hell - Not even a code smell. It’s an issue from back when languages like JavaScript didn’t support promises yet, but callbacks were popular.

          Indeed, so now it is a code smell.

            • StrikeForceZero@programming.dev
              link
              fedilink
              arrow-up
              2
              ·
              11 hours ago

              At that point I would argue composition/traits are the way to go.

              “This extends Draggable”. That’s great but now we can’t extend “Button” to override the click handler.

              Traits: You wanna have Health, and do Damage, but don’t want to implement InventoryItem? No problem. You wanna be an Enemy and InventoryItem? Go for it. What’s this function take? Anything that implements InventoryItem + Consumable

                • StrikeForceZero@programming.dev
                  link
                  fedilink
                  arrow-up
                  1
                  ·
                  edit-2
                  8 hours ago

                  Yeah Interfaces would be the next best thing.

                  The only reason why traits are considered better is because in languages like rust it can enable static dispatch. Whereas interfaces in C#, Java, Typescript, (and C++ via abstract classes, not templates) are always dynamic dispatch.

    • BradleyUffner@lemmy.world
      link
      fedilink
      English
      arrow-up
      11
      ·
      3 days ago

      A code smell isn’t supposed to be automatically bad. A smell is an indication that something might be wrong. Sometimes using a smelly pattern is legitimately the only way to do something.

    • pcouy@lemmy.pierre-couy.frOP
      link
      fedilink
      arrow-up
      6
      ·
      edit-2
      2 days ago

      Apart from the fact that, as another commenter said, “smells” are not “rules”, I think most of these points come down to developing good habits, and ultimately save a lot of time in the long run by initially spending some time thinking about maintainability and preventing/limiting technical debt accumulation.