6 ms·
> all immutable programming became easy Switching to the functional style of logic from the algorithmic style can be slightly tricky but as for immutability it
by qwerty456127 5y ago
> all immutable programming became easy
Switching to the functional style of logic from the algorithmic style can be slightly tricky but as for immutability itself - since the introduction of for-each loops which freed us from incrementing counters I could never understand why is immutability not the default in all the popular languages. Programming algorithmically I rarely need to change a variable even in C# or Python. The fact they don't have a syntax for defining single-assign values (like val in Scala) annoyed me since I learnt Scala (which is extremely easy to start with - you can use it just as "better Java", advanced functional programming tricks are entirely optional) and realized I mostly use "val" naturally. If a variable I defined is changed - it most likely is by a mistake or some unexpected behavior taking place. Both C# and Python already have "var" to define a variable with its type inferred, why won't they add "val" to tell the compiler it can only be initialized once and never assigned again? To me it seems trivial for a compiler developer to add and also not breaking anything while bringing quite a lot of value by eliminating a whole class of bugs.
- Roonerelli 5y agocouldn't you use the readonly modifier in c#?
- qwerty456127 5y agoI do, occasionally. But it looks annoyingly verbose (which to me already feels enough to avoid using it too much) and IIRC has some limitations (I can't remember) as compared to Scala's concise and universal `val valueName = value`.
- Cederfjard 5y agoIs ”algorithmic” a synonom for ”imperative” in this case? I haven’t seen this usage before.
- qwerty456127 5y agoYes. "Imperative" probably is the right word. I don't read much programming literature. You probably are correct.
- qwerty456127 5y agoI mean I understand "an algorithm" as a sequence of commands and a functional program as a pipeline of functions.
- dimitrios1 5y ago> I could never understand why is immutability not the default in all the popular languages My take (could be wrong, happy to be corrected): Because most PL designers want speed to be pre-eminent in their language. One of the quickest way to never have your language adopted by large corporation is to have it be slower, without some obvious great benefit in productivity. That's the catch though: Arguing for increase in productivity is harder, because it is qualitative, and ultimately subjective. Plus it's a crowded market these days in language productivity land, and unfortunately most of those languages are dynamically typed. Showing some benchmarks on paper is easy to do, and an easy way to sell a language. So how does this all tie back in? Well, if you want to go fast, you need mutability (and up until most recent times: shared mutable memory, although this appears to be changing to non-shared mutation models). As memory and workloads get bigger and bigger, the cost of copying increases.
- Kinrany 5y agoImmutability being the default does not prevent mutability.
- qwerty456127 5y agoObviously. It's just makes it an available option and the default. And this already is helpful.
- kaba0 5y agoImmutability is not inherently slower. It provides for much better reasoning that may even result in superior performance. Of course retroactively introducing them to a largely mutable language may not prove to have better performance, but eg. Haskell is most definitely not slow.
- omegalulw 5y ago> I could never understand why is immutability not the default in all the popular languages At what point do you shift the blame for bad code from the language to the programmer? If you made variables const by default bad programmers will simply mark everything mutable. The only way you enforce proper constness tags (or mutable tags) is through code reviews and teaching people better. So the default doesn't really matter.