• 9point6@lemmy.world
    link
    fedilink
    arrow-up
    46
    arrow-down
    10
    ·
    4 months ago

    Const everything by default

    If you need to mutate it, you don’t, you need to refactor.

    • noli@programming.dev
      link
      fedilink
      arrow-up
      40
      arrow-down
      1
      ·
      4 months ago

      Dogmatic statements like this lead to bad, messy code. I’m a firm believer that you should use whatever style fits the problem most.

      Although I agree most code would be better if people followed this dogma, sometimes mutability is just more clean/idiomatic/efficient/…

      • 9point6@lemmy.world
        link
        fedilink
        arrow-up
        3
        arrow-down
        2
        ·
        4 months ago

        I agree somewhat, but I’d also say any codebase needs some level of “dogmatic” standard (ideally enforced via tooling). Otherwise you still end up with bad, messy code (I’d even say messier, as you don’t even get consistency)

      • Corbin@programming.dev
        link
        fedilink
        English
        arrow-up
        2
        arrow-down
        4
        ·
        4 months ago

        Define your terms before relying on platitudes. Mutability isn’t cleaner if we want composition, particularly in the face of concurrency. Being idiomatic isn’t good or bad, but patterned; not all patterns are universally desirable. The only one which stands up to scrutiny is efficiency, which leads to the cult of performance-at-all-costs if one is not thoughtful.

    • lad@programming.dev
      link
      fedilink
      arrow-up
      12
      ·
      4 months ago

      I’d agree with the first half, but not the second. Sometimes mutability allows for more concise code, although in most cases it’s better to not mutate at all

      • 9point6@lemmy.world
        link
        fedilink
        arrow-up
        5
        arrow-down
        1
        ·
        4 months ago

        I feel like I should maybe have put a “probably” in there

        After all “there’s no silver bullet”, but in anything but a few edge cases, the rule applies, IMO

    • RageAgainstTheRich@lemmy.world
      link
      fedilink
      arrow-up
      4
      arrow-down
      1
      ·
      edit-2
      4 months ago

      That is a… strange take.

      Random example, imagine a variable that holds the time of the last time the user moved the mouse. Or in a game holding the current selected target of the player. Or the players gold amount. Or its level. Or health. Or current position.

      • frezik@midwest.social
        link
        fedilink
        arrow-up
        4
        arrow-down
        3
        ·
        4 months ago

        In all those cases, the answer is to swap in a new variable and throw the old one away.

          • frezik@midwest.social
            link
            fedilink
            arrow-up
            5
            ·
            4 months ago

            It’s only a const within a function. You can pass the value to another function and changing it as it’s passed. For example:

            const int foo = 1
            other_func( foo + 1)
            

            In functional programming, you tend to keep track of state on the stack like this.

              • frezik@midwest.social
                link
                fedilink
                arrow-up
                7
                ·
                4 months ago

                Keeping state managed. The data for the function will be very predictable. This is especially important when it comes to multithreading. You can’t have a race condition where two things update the same data when they never update it that way at all.

                  • frezik@midwest.social
                    link
                    fedilink
                    arrow-up
                    1
                    ·
                    4 months ago

                    Rather than me coming up with an elaborate and contrived example, I suggest giving a language like Elixir a try. It tends to force you into thinking in terms of immutability. Bit of a learning curve if you’re not used to it, but it just takes practice.

      • whotookkarl@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        4 months ago

        I think the general idea would be to take the original const, and create a new const with the new location applied. Destroy the original when it’s no longer needed or scoped. State maintained through parameters passed to the move function e.g. move(original const, new location) -> new const object instead of stateful members in the object like move(mutable, new location) -> updated mutable.