[0] https://stackoverflow.com/questions/14426366/what-is-an-idio...
if cond1 then
myVar = 1
else if cond2 and cond3 then
myVar = 10
else if cond2 and not cond3 then
myVar = 100
else
myVar = 4
end if
The VB.Net style also makes it easier to identify mismatched blocks because the "enders" are named differently. (One can also put the assignment on the same line as the conditional, but I generally don't recommend it.)Some may complain it's slightly verbose, but few if any will claim it's not clear.
myVar =
cond do
cond1 -> 1
cond2 && cond3 -> 10
cond2 && !cond3 -> 100
true -> 4
end
ML languages have similar constructs.That’s a problem. One I’ve seen all too often, but a problem, nonetheless.
myVar =
(!cond1) && (!cond2) && (!cond3) ? myValue_0 : // case 0 0 0 blabla
(!cond1) && (!cond2) && (cond3) ? myValue_1 : // case 0 0 1 bla
(!cond1) && (cond2) && (!cond3) ? myValue_2 : // case 0 1 0 blablabla
(!cond1) && (cond2) && (cond3) ? myValue_3 : // case 0 1 1
(cond1) && (!cond2) && (!cond3) ? myValue_4 : // case 1 0 0
(cond1) && (!cond2) && (cond3) ? myValue_5 : // case 1 0 1
(cond1) && (cond2) && (!cond3) ? myValue_6 : // case 1 1 0
(cond1) && (cond2) && (cond3) ? myValue_7 : // case 1 1 1
defaultValue; // uninteresting default (null, -1...)In case of more complex data generation you could use supplier pattern and combine it with strategy.
No need for any if else statements at all.
Sure it requires a lot more of work but is future proof and clear.
Foo(int x){return x+1} becomes Foo(Supplier x){return x() + 1}
Thats my cause.
(Does not make me any less right)
(You might consider reflecting on your tone and message. It presupposes bad faith on the other party, is dismissive and condescending, and you are probably less right than you think you are.)
It sounds interesting, that was all.
with 3 conditions you have 2^3 possibilities to check: are you really really sure (!cond1 && !cond2 && cond3) should give you defaultValue ? Do you really want 1 even if !cond2 ? etc...
When possible these conditions should be avoided in the first place (depending on context of course)