4 ms·
The point wasn’t to encourage shadowing bindings from a parent scope, only to illustrate that it is not the same thing as mutable references. Whether to, and wh
by borromakot 2y ago
The point wasn’t to encourage shadowing bindings from a parent scope, only to illustrate that it is not the same thing as mutable references. Whether to, and when to, use rebinding is a different conversation, but in some cases it can lead to subtle bugs just like you described.
- cies 2y agoI still think the difference to me as a programmer is largely semantic (except when the performance has to be considered): "rebinding" or "in-place mutation", in both cases it is a mutable variable to me.
- borromakot 2y agoThere is a guarantee that you have when rebinding is the only option. with rebinding: def thing() do x = 10 do_something(x) # x is 10, non negotiably, it is never not 10 end with mutability def thing() do x = 10 do_something(x) # who knows what x is? Could be anything. end Additionally, because rebinding is a syntactic construct, you can use a linter to detect it: https://hexdocs.pm/credo/Credo.Check.Refactor.VariableRebinding.html https://hexdocs.pm/credo/Credo.Check.Refactor.VariableRebind...
- cies 2y agoI find this a much better example than the example shown in the article. I even find it quite useful when explained like this. Thanks!