• @[email protected]
      link
      fedilink
      433 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/…

      • @[email protected]
        link
        fedilink
        13 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)

      • @[email protected]
        link
        fedilink
        English
        -23 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
      link
      fedilink
      133 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

      • @[email protected]
        link
        fedilink
        43 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

    • @ByGourou
      link
      83 months ago

      Sorry, I want to make an app that works, not a perfect art piece.

      • @[email protected]
        link
        fedilink
        43 months ago

        The app working isn’t good enough, it needs to be maintainable. From a professional perspective, unmaintainable code is useless code.

        Code that mutates everywhere is generally harder to reason about and therefore harder to maintain, so just don’t do it (unless there’s literally no other practical way, but genuinely these are very rare cases)

        • @ByGourou
          link
          13 months ago

          I personally disagree, forcing yourself to use non mutable variables only leads to longer and more convoluted code.

          • @[email protected]
            link
            fedilink
            23 months ago

            Fair play, I guess we’re probably just gonna disagree.

            In my experience I’d say mutable code (larger than anything other than toy examples) always results in more time spent fixing bugs down the line, predominantly because it’s objectively harder for humans to reason about multiple one to many relationships rather than multiple one to one relationships. I’d say because you need to think about all possible states of the set of mutable variables in your code in order to completely understand it (and I don’t just mean understanding the intended purpose of the code, I mean understanding everything that code is capable of doing), that usually results in a more convoluted implementation than the pretty linear way you typically read functional code.

            Longer code is practically always better if it’s easier to understand than the shorter alternative. Software engineers aren’t employed to play code golf, they’re employed to write maintainable software. Though I’ll say ultra high performance applications might be the exception here—but 99% of engineers aren’t doing anything like that.

            I’m always happy to be convinced otherwise, but I’ve never seen a convincing argument

    • @[email protected]
      link
      fedilink
      3
      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.

        • @[email protected]
          link
          fedilink
          13 months ago

          Legit question because i think I’m misunderstanding. But if its a const, how are you able to swap or replace it?

          • @[email protected]
            link
            fedilink
            53 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.

              • @[email protected]
                link
                fedilink
                73 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.

                • @[email protected]
                  link
                  fedilink
                  13 months ago

                  Hm I’m having trouble visualizing this do you know a quick little example to illustrate this?

      • @[email protected]
        link
        fedilink
        13 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.

  • @xmunk
    link
    153 months ago

    The only const in life is to const all the things.

    • @[email protected]
      link
      fedilink
      303 months ago

      In functional programming, everything is seen as a mathematical function, which means for a given input there is a given output and there can be no side effects. Changing a variable’s value is considered a side effect and is thus not possible in pure functional programming. To work around this, you typically see a lot of recursive and higher order functions.

      Declaring all values as const values is something you would do if you’re a diehard functional programmer, as you won’t mutate any values anyway.

        • @[email protected]
          link
          fedilink
          103 months ago

          Depends on how deep down the rabbit hole you want to go :p

          • creating a new variable that contains the updated value
          • recursion (e.g. it’s not possible to make a loop that increments i by 1, but it is possible to turn that loop into a function which calls itself with i+1 as argument)
          • avoiding typical types of operations that would update variable values. For example instead of a for loop that updates every element of a list, a functional programmer will use the map function, which takes a list and a function to apply to each element of that list to create an updated list. There’s several more of these very typical functions that are very powerful once you get used to using them.
          • monads (I’m not even gonna try to explain them as I hardly grasp them myself)
          • ShieldGengar
            link
            33 months ago

            A monad is just a monoid in the category of endofunctors, what’s the problem?

  • @[email protected]
    link
    fedilink
    93 months ago

    I oscillate between using more functional paradigms and more object-oriented ones. is that normal?
    I use a linter BTW(TypeScript) if that is a useful info.

    • @[email protected]
      link
      fedilink
      93 months ago

      I think using both is normal. Closures and objects are duals of each other. Do whatever is understandable and maintainable, neither paradigm is magic.

    • @[email protected]OP
      link
      fedilink
      33 months ago

      I also do that. Very simple stuff, especially of those that are easy to optimize for the compiler, are often very close to functional programming paradigms.

    • @[email protected]
      link
      fedilink
      23 months ago

      I use a combination of both. Objects are declared const, all members are set in the constructor, all methods are const. It doesn’t really work for some types of programs (e.g. GUIs) but for stuff like number crunching it’s great.

      • @[email protected]
        link
        fedilink
        23 months ago

        I heavily use classes while working on back end, and when I’m making a really self-contained logic, such as a logger or an image manipulation service.
        but since most frontend stuff heavily leans on functional side, I go with it