3 ms·
> That’s a lot simpler isn’t it? WTF?? No it isn't. You have this magic initial value and have to figure out which code paths have mutated it and which haven't
by lmm 10y ago
> That’s a lot simpler isn’t it?
WTF?? No it isn't. You have this magic initial value and have to figure out which code paths have mutated it and which haven't. Mutable variables make code far far more complex to read.
I'm all for single return but through the use of expressions, not variables; I would probably express the given function as:
expression() {
return
a ?
(b && c) ?
2
:
1
:
0
;
}
(Ideally I'd use a language that allowed a sensible conditional expression rather than one that relies on bizarre symbols, but we work with what's available)
- roblabla 10y agoIn OCaml : let expression a b c = match (a, b, c) with | (true, true, true) => 2, | (true, true, _) => 1, | (false, _, _) => 0 I like how pattern matching allows you to create something very close to a truth table. It allows you to visually see what's going on. EDIT: Just realized that the two code examples in the parent's link don't even do the same... Look at the truth table, and look at the case of 1 1 0. The first code would indeed return 0, but not the second one. Derp.