• RageAgainstTheRich@lemmy.world
    link
    fedilink
    arrow-up
    4
    arrow-down
    1
    ·
    edit-2
    3 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
      3
      arrow-down
      3
      ·
      3 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
          3
          ·
          3 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
              5
              ·
              3 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
                  ·
                  3 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.

                  • madcaesar@lemmy.world
                    link
                    fedilink
                    arrow-up
                    1
                    ·
                    3 months ago

                    Ok how about this then, I frequently do something like this:

                    let className = 'btn'
                      if (displayType) {
                        className += ` ${displayType}`
                      }
                      if (size) {
                        className += ` ${size}`
                      }
                      if (bordered) {
                        className += ' border'
                      }
                      if (classNameProp) {
                        className += ` ${classNameProp}`
                      }
                    

                    How would this be made better with a functional approach? And would be more legible, better in anyway?