4 ms·
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,
by Kapow 9y ago
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.
- crooked-v 9y agoAnd 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).
- aiham 9y agoWhat's the advantage over JS's ternary operator?
- pmarreck 9y agoI 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)
- crooked-v 9y agoThe same pattern should be usable for things like switch statements.