6 ms·
Immutable variables are too weird for me.
by MrFantastic 6y ago
Immutable variables are too weird for me.
- andy_ppp 6y agoThe runtime takes care of making immutability fast (enough), you realise how good it is when debugging code that does lots of mutations via references in other languages. Also it helps to reason about parallel code because nothing is being mutated from another thread.
- toast0 6y agoI agree it's weird, but you get used to it and now I'll occasionaly write code without reassigning to variables in 'normal' languages... I still miss for loops though.
- dnautics 6y agoIf you are willing to get over it, you'll find that huge classes of errors disappear. It's a very nice feature that lets you cut a bunch of bs, clarify your code, and make everything much more comprehensible. When you can mutate your values, you have to read any given block of code with a ton of suspicion (and possibly trace through control flow, keeping branches in your head, god I don't miss doing that). If your values are immutable, you don't have to do that. The one place where it screws you over in erlang is the REPL, which I think is why Elixir chose to allow you to mutate the variable binding, (but the underlying values are still immutable). In my main code, I mark any variables that I will mutate binding (almost never) with a ! postfix sigil (idea stolen from Julia/Ruby)
- yawaramin 6y agoYeah but it's a critical piece of how Erlang and its BEAM VM are able to deliver on their concurrency and fault-tolerance guarantees. Turns out that allowing variable values to change randomly throughout the system is really bad for building correct concurrent systems.
- bmitc 6y agoI'm not for sure exactly what you mean by immutable variables, but if you are referring to Erlang not being able to rebind variables, then you might check out Elixir, which does have variable rebinding. All data in Erlang and Elixir is immutable but the rebinding of variables is handled differently between the two. http://blog.plataformatec.com.br/2016/01/comparing-elixir-and-erlang-variables/ http://blog.plataformatec.com.br/2016/01/comparing-elixir-an...
- smabie 6y agoI legitimately don't see why you even need mutation like, 95% of the time. Like what are you doing such that a variable, itself, needs to change?
- sriram_malhar 6y agoImmutability is very very common. It makes reasoning much simpler. Source code control systems don't mutate in place. They create new versions. All modern relational databases (Oracle, Sql Server, Postgres) don't modify records in place. They create new versions. All cloud based file systems (google docs) create new versions. When you have this mindset of 'any mutation needs to go into a different version, a new variable', it simplifies a whole lot of reasoning when things don't work as expected. Try it out.