-
-
Save getify/bf9b6802205326bc687fb0b85bc90d35 to your computer and use it in GitHub Desktop.
if..else if.. else clauses
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
// note: mathematically, `x` can only ever be 0, 1, 2, or 3 | |
var x = someNumber % 4; | |
// let's consider some options for an if..else if..else clause series... |
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
// option 1: | |
if (x === 0) { | |
// .. | |
} | |
else if (x === 1) { | |
// .. | |
} | |
else if (x === 2) { | |
// .. | |
} | |
else if (x === 3) { | |
// .. | |
} | |
// note: else {} clause omitted because it's impossible to reach, and | |
// Istanbul will complain about never reaching it |
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
// option 2: | |
if (x === 0) { | |
// .. | |
} | |
else if (x === 1) { | |
// .. | |
} | |
else if (x === 2) { | |
// .. | |
} | |
else /* if (x === 3) */ { | |
// .. | |
} | |
// note: Istanbul will *NOT* complain here |
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
// option 3: | |
if (x === 0) { | |
// .. | |
} | |
else if (x === 1) { | |
// .. | |
} | |
else if (x === 2) { | |
// .. | |
} | |
else { | |
if (x === 3) { | |
// .. | |
} | |
// note: else {} clause omitted because it's impossible to reach, and | |
// Istanbul will complain about never reaching it | |
} |
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
// option 4: | |
if (x === 0) { | |
// .. | |
} | |
else if (x === 1) { | |
// .. | |
} | |
else if (x === 2) { | |
// .. | |
} | |
else if (x === 3) { | |
// .. | |
} | |
else { | |
// impossible to get here | |
// note: because this else {} clause cannot be reached, Istanbul will | |
// complain about never reaching it | |
} |
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
// option 5: | |
if (x === 0) { | |
// .. | |
} | |
else if (x === 1) { | |
// .. | |
} | |
else if (x === 2) { | |
// .. | |
} | |
else if (x === 3) { | |
// .. | |
} | |
else { | |
throw new Error("impossible to get here"); | |
// note: because this else {} clause cannot be reached, Istanbul will | |
// complain about never reaching it | |
} |
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
// option 6: | |
if (x === 0) { | |
// .. | |
} | |
if (x === 1) { | |
// .. | |
} | |
if (x === 2) { | |
// .. | |
} | |
if (x === 3) { | |
// .. | |
} | |
// note: all 4 omitted else clauses are actually "reached" in this | |
// construct, so Istanbul will *NOT* complain | |
// note 2: in this case, each independent `if` test clause automatically | |
// (mathematically) excludes the previous ones -- `x` can never be two or | |
// more values at the same time. general test clauses would need to explicitly | |
// exclude previous conditions, like: | |
// if (A) { .. } | |
// if (!A && B) { .. } | |
// if (!(A || B) && C) { .. } | |
// if (!(A || B || C) && D) { .. } | |
// note 3: this option is also a bit more "dangerous" in that if one of the | |
// if blocks reassigned `x`, then it could potentially (and accidentally) match | |
// more than one clause, unlike an if..else if series |
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
// option 7: | |
switch (true) { | |
case (x === 0): { | |
// .. | |
break; | |
} | |
case (x === 1): { | |
// .. | |
break; | |
} | |
case (x === 2): { | |
// .. | |
break; | |
} | |
case (x === 3): | |
default: { | |
// .. | |
} | |
// note: some linter configs may complain about the lack of a `break` | |
// (aka, "fall-through") in this construct | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If it's my own project, and I do this throughout the entire project, I prefer: