-
-
Save anacrolix/d5cb2dbc50ebbc167187fa439998d4bb to your computer and use it in GitHub Desktop.
// which would you do? comment below | |
// distinct ifs | |
if n == 1 { | |
single() | |
} | |
if n > 1 { | |
multi() | |
} | |
// else if | |
if n == 1 { | |
single() | |
} else if n > 1 { | |
multi() | |
} | |
// switch | |
switch { | |
case n == 1: | |
single() | |
case n > 1: | |
multi() | |
} |
hard without context. can you exit early from first if? will cases ever overlap? like n > 1
& isEven(n)
should both run for n?
probably switch I guess
Suppose that a new function three
might appear soon, with the obvious condition for its being called.
So I would choose the first, unless brevity was a priority. There's no indication that calling any of the functions should preclude calling of the others, or that the calls should be ordered so I would not introduce that assumption into the code unless I knew that to be true.
Plus the logic does not become entangled. single
is called if n is 1, and multi
if n is > 0, and three
if n == 3.
single is called if n is 1, and multi if n is > 0, and three if n == 3.
typo that mutli is called if n > 0 :)
context is needed, as should multi and three both be called if n == 3?
I would go with the switch, especially if I knew other case could be needed