• mindlesscrollyparrot@discuss.tchncs.de
    link
    fedilink
    arrow-up
    2
    ·
    2 months ago

    The author mentions that some of the changes broke things, but it’s a long way into the article before the word “test” appears. It’s only point 6/7 of his recommendations.

    Making changes with no test coverage is not refactoring. It’s just rewriting. Start there.

  • BehindTheBarrier@programming.dev
    link
    fedilink
    English
    arrow-up
    0
    ·
    edit-2
    2 months ago

    I largely agree with this nodding along to many of the pitfalls presented. Except numbers 2s good refactor. I hope I won’t sound too harsh/picky for an example that perhaps skipped renaming for clarity on the other parts, but I wanted to mention it.

    While I don’t use javascript and may be missing some of the norms and context of the lanugage, creating lamda functions (i don’t know the js term) and then hardcoding them into a function is barely an improvement. It’s fine because they work well with map and filter, but it didn’t address the vague naming. Renaming is refactoring too!

    isAdult is a simple function with a clear name, but formatUser and processUsers are surprisingly vague. formatUser gives only adult FormattedUsers, and that should probably be highlighted in the name of formatUser now that it is a resuable function. To me, it seems ripe for mistaken use given that it is the filter that at a glance handles removing non-adult users before the formatting, while formatUser doesn’t appear to exepct only adult users from it’s naming or even use! Ideally, formatUser should have checked the age on it’s own and set isAdult true/false accordingly, instead of assuming it will be used only on adult Users.

    Likewise, the main function is called processUsers but could easily have been something more descriptive like GetAdultFormattedUsers or something similar depending on naming standards in js and the context it is used in. It may make more sense in the actual context, but in the example a FormattedUser doesn’t have to be an adult, so a function processing users should clarify that it only actually creates adult formatted users since there is a case where a FormattedUser is not an adult.

    • Eager Eagle@lemmy.world
      link
      fedilink
      English
      arrow-up
      0
      ·
      2 months ago

      Totally agree. The hardcoded isAdult: true repeated in all #2 examples seems like a bug waiting to happen; that should be a property dynamically computed from the age during access time, not a static thing.

      • nous@programming.dev
        link
        fedilink
        English
        arrow-up
        0
        ·
        2 months ago

        Or just a function. IMO computer properties are an anti pattern. Just adds complexity and confusion around what is going on - all to what? Save on a () when you access the value?

        • astrsk@fedia.io
          link
          fedilink
          arrow-up
          1
          ·
          2 months ago

          Properties are great when you can cache the computation which may be updated a little slower than every time it’s accessed. Getter that checks if an update is needed and maybe even updates the cached value then returns it. Very handy for lazy loading.

          • nous@programming.dev
            link
            fedilink
            English
            arrow-up
            0
            arrow-down
            1
            ·
            2 months ago

            Functions can do all this. Computed properties are just syntactic sugar for methods. That is it. IMO it makes it more confusing for the caller. Accessing a property should be cheap - but with computed properties you don’t know it will be. Especially with caching as your example. The first access can be surprisingly slow with the next being much faster. I prefer things to not do surprising things when using them.

            • astrsk@fedia.io
              link
              fedilink
              arrow-up
              1
              ·
              2 months ago

              I get that, it’s a valid point. But in OOP, objects can be things and do things. That’s kinda the whole point. We’re approaching detailed criticism of contextless development concepts though so it kinda doesn’t matter.

  • Snarwin@fedia.io
    link
    fedilink
    arrow-up
    0
    ·
    2 months ago

    I appreciate that this article highlights the value of using of named functions in functional-style code. Too often, programmers assume that “functional programming” means using lambdas everywhere, when in my experience, lambdas are actually a (very mild) code smell.

    • nous@programming.dev
      link
      fedilink
      English
      arrow-up
      0
      ·
      2 months ago

      lambdas are actually a (very mild) code smell

      Lambdas alone are not a code smell. That is like saying Objects or Functions or even naming things is a code smell just because you can use them in bad ways. It is just to broad a statement to be useful. At best you might say that large/complex anonymous lambdas are a code smell - just like large/complex and badly named functions are. You need to be specific about code smells, otherwise you are basically saying code is a code smell.

      • Snarwin@fedia.io
        link
        fedilink
        arrow-up
        1
        ·
        2 months ago

        Well, remember, a code smell isn’t something that’s inherently bad, it’s “a hint that something might be wrong”.

        I’m not saying that anyone should flag lambdas as a problem in code review, just that when you see one, it’s probably worth taking a second to ask yourself if a named function would make more sense.