Created
January 9, 2018 03:20
-
-
Save anacrolix/d5cb2dbc50ebbc167187fa439998d4bb to your computer and use it in GitHub Desktop.
condition style
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() | |
} |
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?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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