• @[email protected]
    link
    fedilink
    21 month ago

    that static variable is local to that function

    Yes I know how static storage durations work. It’s still global state, which is a code smell. Actually I’d go as far as to say global state is just bad practice, not just a smell. Occasionally it’s the only option, and it’s definitely the lazy option which I won’t claim to never take!

    • @[email protected]
      link
      fedilink
      11 month ago

      Aaand… you didn’t even bother to google it :/

      This is not about storage durations, and it’s local to a function

      • @[email protected]
        link
        fedilink
        11 month ago

        I don’t need to Google anything. I have 30 years experience writing C & C++.

        This is not about storage durations

        Yes it is.

        https://en.cppreference.com/w/c/language/storage_duration

        it’s local to a function

        Only the visibility is local. The data is still global state. You can call that function from anywhere and it will use the same state. That’s what global state means.

        https://softwareengineering.stackexchange.com/a/314983

        Some of the biggest issues with global state are that is makes testing difficult and it makes concurrent code more error-prone. Both of those are still true for locally scoped static variables.

        • @[email protected]
          link
          fedilink
          1
          edit-2
          1 month ago

          Again, it’s an easy refactor to make it not global. There are cases where that extra abstraction work simply does not add value.

          With your background, you should know that

          • @[email protected]
            link
            fedilink
            11 month ago

            it’s an easy refactor to make it not global

            I have enough experience to know that making global state non-global is usually anything but easy.

            • @[email protected]
              link
              fedilink
              01 month ago

              Damn, suppose I won’t just pass it as a pointer from the call site. That’d be so difficult to add an int to a struct

              30 years my ass

              • @[email protected]
                link
                fedilink
                11 month ago

                Yes, and then pass the context from the call sites of that function, and all the way up to main(). Oh look you’re refactored the entire app.

                That’s best cases too, you’d better hope your program isn’t actually a shared library running in a SystemVerilog simulator with state instantiated from separate modules via DPI, or whatever.

                30 years my ass

                lol when you have 30 years experience you will have actually tried to do this a few times and realised it isn’t usually as trivial as you hope it would be.