News for hackers. Stuff that matters.
Re:Avoid Else, Return Early (2013) by pmarreck on Monday March 26, 2018 @01:24PM (#16680665)
I've actually gone from avoiding else to requiring else, which is sort of a necessity when you're in a functional immutable environment where the result of a logical expression is a value (and often assigned as such), since it avoids undefined situations.

(This example comes from Elixir.)

value = if boolean, do: this_value, else: other_value

is undefined in some circumstances unless an "else" is included.

This pattern might seem weird to OOP folks (the assignment, not the ternary-operator-style logic), but in Elixir-land, it is not only considered superior to assigning inside if/then logic, you actually get a compiler warning if you assign/bind to a variable inside a logical expression because (again) that value becomes undefined as soon as you leave local scope (or re-acquires the value it had in the outer scope, thanks to immutability).

And if you think about it, values can only travel in one direction using this scheme, which further simplifies reasoning about information flows.

I would argue that this is also superior to early-exit as having one entry point and one exit point is much easier to reason about, and because the extra logical complexity you're ostensibly trying to avoid writing by early-exiting is actually still there, it's just obfuscated... and that complexity should be made obvious (in which case, if it's extensive, it would be a code smell... consider the example of a function with 25 early exits that "looks flat" visually, but which actually has 25 different branches)

Re:Avoid Else, Return Early (2013) by Kapow on Monday March 26, 2018 @02:02PM (#16681008)
That's just a ternary operator. I use that in Java all the time.

    value = boolean ? this_value : other_value;
If the branching logic gets too complicated, I usually move it into a function (private method) with a return in each branch.
Re:Avoid Else, Return Early (2013) by crooked-v on Monday March 26, 2018 @03:05PM (#16681537)
And in Javascript, once do-expressions go from a proposal to a common part of the language, this will be a thing:

  let value = do {if (boolean) { this_value } else { other_value }}
...which is even closer to the Elixir style than JS's ternary operator (which is identical to Java's).
Re:Avoid Else, Return Early (2013) by aiham on Monday March 26, 2018 @06:29PM (#16683240)
What's the advantage over JS's ternary operator?
Re:Avoid Else, Return Early (2013) by pmarreck on Monday March 26, 2018 @07:40PM (#16683770)
I guess it depends on whether "if" is treated as an expression which returns a value, which I don't think is the case due to the fact that I don't think there is an implicit value return in JS (such as in Ruby; the final line in any scope is the value that scope returns; same as in Elixir)
Re:Avoid Else, Return Early (2013) by crooked-v on Wednesday March 28, 2018 @02:19PM (#16700313)
The same pattern should be usable for things like switch statements.