• blaue_Fledermaus@mstdn.io
      link
      fedilink
      arrow-up
      12
      ·
      4 hours ago

      Static types are great, but not exactly what would have helped here, any decent language or at least a linter should catch the use of a not declared identifier.

        • Strykker@programming.dev
          link
          fedilink
          arrow-up
          5
          arrow-down
          2
          ·
          2 hours ago

          It’s python, just use type hinting already and your linter will catch that.

          Also some winters can look at the use of food and see the type being passed in.

          • Ephera@lemmy.ml
            link
            fedilink
            arrow-up
            6
            ·
            2 hours ago

            Autocorrect got you pretty bad, there.

            I was very confused, why we’re suddenly talking about rationing food during winter. 🙃

          • FizzyOrange@programming.dev
            link
            fedilink
            arrow-up
            4
            ·
            2 hours ago

            Yes you can use static type hinting and the static type checker (Mypy or Pyright) will catch that. Linters (Pylint) won’t.

        • ripcord@lemmy.world
          link
          fedilink
          arrow-up
          1
          arrow-down
          2
          ·
          3 hours ago

          Not with an example that simple and poor, no.

          If you have done the minimum and at least set a type hint, or if your ide is smart enough to check what calls the function and what it passes, then it’ll be flagged.

          • Ephera@lemmy.ml
            link
            fedilink
            arrow-up
            3
            ·
            2 hours ago

            How would you make it non-awful, without specifying static types?

            I guess, a unit test would catch it, but needing 100% test coverage to catch typos isn’t exactly great…

            • BrianTheeBiscuiteer@lemmy.world
              link
              fedilink
              arrow-up
              1
              ·
              edit-2
              2 hours ago

              What’s the purpose of foo? Why an ambiguous single character variable? What if the property was there but the value was null? Why not use (assuming JS) optional chaining?

              I’d approach it more like this:

              function getWhatevrProp(userData) (
                const default = { whatevr: "n/a" };
              
                return { ...default, ...userData }.whatevr;
              }
              

              Sorry, read too fast the first time. It’s more likely Python. I also don’t know Python well enough to give recommendations on that.