• Scratch@sh.itjust.works
    link
    fedilink
    English
    arrow-up
    1
    ·
    8 months 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
      0
      ·
      8 months 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 > )

      • Scratch@sh.itjust.works
        link
        fedilink
        English
        arrow-up
        1
        ·
        8 months ago

        The site doesn’t define what a code smell is, though. It’s just a list of Don’t Do’s.

        That’s kind of the nuance I would be hoping for.

        Something like:

        Code Smells are clues that something is amiss. They are not things that always must be ‘fixed’. You as an engineer will, through experience in your own codebase and reading of others, develop a sense of the harm imparted by and the cost of fixing Code Smells. It is up to you and your team to decide what is best for your codebase and project.

        (The rule of 3 formatting was intentional, given the community we’re in)

  • 1hitsong@lemmy.ml
    link
    fedilink
    arrow-up
    0
    ·
    8 months 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.

    • curbstickle@lemmy.dbzer0.com
      link
      fedilink
      arrow-up
      0
      ·
      8 months 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.

      • pcouy@lemmy.pierre-couy.frOP
        link
        fedilink
        arrow-up
        0
        ·
        edit-2
        8 months ago

        It’s often a good idea to make the code itself very explicit through verbose function and variable names, rather than writing comments that could lead to inconsistencies between code and comments (by not updating the comments at the same time as the code) (see Fallacious Comments and “What” Comments from the catalog)

        • curbstickle@lemmy.dbzer0.com
          link
          fedilink
          arrow-up
          1
          ·
          8 months 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.