News for hackers. Stuff that matters.
Re:Should function arguments be reassignable or mutable? by mabbo on Monday January 01, 2018 @08:46AM (#16046023)
I feel the author isn't going far enough- make all variables immutable by default unless explicitly declared otherwise, and even then highly discouraged.

Where mutable variables are needed, I prefer to hide that logic inside a method that exists solely to do that dirty business on an abstract level, not specific to my business logic. That let's me test the 'dirty' mutable method in an isolated place and removes a whole class of mutability-related bugs from my business logic code.

Working on Java, this used to be nigh-impossible, but the Java 8 additions have truly made my life easier.

Re:Should function arguments be reassignable or mutable? by SideburnsOfDoom on Monday January 01, 2018 @09:12AM (#16046108)
I agree up to a point.

Firstly, "Immutable by default" is recognised as a good design feature now for modern languages. But there is plenty of code in existing languages like Java, JavaScript, C, C# etc. which does not follow this rule.

Secondly, the "principle of least surprise" suggests that function arguments should work like local vars in that language. So if local vars are immutable by default, then arguments should be as well. Or if local vars can be optionally declared as immutable, then function arguments should be as well.

I can't really say what's the best for every/any language. It's not one size fit all. There are always design tradeoffs and differing schools of thought. But these days it looks a lot like "immutable by default, unless there's a good reason otherwise" is a good starting point.

Re:Should function arguments be reassignable or mutable? by cuspycode on Monday January 01, 2018 @10:46AM (#16046466)
Historical anecdote: Algol-68 disassociated variable assignment from mutability already 50 years ago. Whenever mutability was desired, a reference to a mutable value holder was used.

Example of immutable assignment: int n = 2

Example of reference to mutable value: ref int n = (heap int := 2)

However, in order to make Algol-68 code look more similar to Algol-60, they introduced backwards-compatible shorthands, so for example the declaration int n; was actually a shorthand for ref int n = (loc int);

This shorthand magic probably contributed to why Algol-68 syntax was perceived to be difficult.

Footnote: loc and heap were the two memory allocation operators available in Algol-68.

Re:Should function arguments be reassignable or mutable? by sli on Tuesday January 02, 2018 @10:47AM (#16052488)
Rust takes a similar approach with let some_var and let mut some_var, respectively.
Re:Should function arguments be reassignable or mutable? by mortoray on Monday January 01, 2018 @09:59AM (#16046265)
I, the author, am tending to agree now. :)
Re:Should function arguments be reassignable or mutable? by lozenge on Monday January 01, 2018 @10:02AM (#16046273)
Agreed. Erlang is another language with single assignment, i.e. every variable assignment target must have a new name. It's incredibly easy to see what calculations and conditions affect the final outcome.
Re:Should function arguments be reassignable or mutable? by maxxxxx on Monday January 01, 2018 @11:32AM (#16046658)
How do you do a for loop in Erlang or something that sums up a value ?
Re:Should function arguments be reassignable or mutable? by testvox on Monday January 01, 2018 @12:34PM (#16046984)
You don't usually write for loops in Erlang, as a functional language loops are better expressed as recursive functions.

Here is an example of using a recursive function in Erlang to sum up a factorial.

  fac(1) ->
      1;
  fac(N) ->
      N * fac(N - 1).
Re:Should function arguments be reassignable or mutable? by maxxxxx on Monday January 01, 2018 @01:08PM (#16047149)
Does Erlang handle the stack better? In most languages this would blow up if N got big enough.
Re:Should function arguments be reassignable or mutable? by bearhall on Monday January 01, 2018 @01:38PM (#16047305)
Yes. Erlang has tail recursion which helps prevent that http://learnyousomeerlang.com/recursion
Re:Should function arguments be reassignable or mutable? by dragonwriter on Monday January 01, 2018 @01:43PM (#16047339)
As the article notes, what Erlang has is Last Call Optimization, this is significantly more general than tail recursion optimization.
Re:Should function arguments be reassignable or mutable? by whichdan on Monday January 01, 2018 @04:16PM (#16048096)
In addition to recursion, you can use `foldl` and `foldr` (or similar functional methods)

http://erlang.org/doc/man/lists.html