Skip to content

Instantly share code, notes, and snippets.

@getify getify/1.js
Created Apr 9, 2019

Embed
What would you like to do?
if..else if.. else clauses
// 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...
// 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
// option 2:
if (x === 0) {
// ..
}
else if (x === 1) {
// ..
}
else if (x === 2) {
// ..
}
else /* if (x === 3) */ {
// ..
}
// note: Istanbul will *NOT* complain here
// 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
}
// 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
}
// 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
}
// 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
// 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
}
@joelnet

This comment has been minimized.

Copy link

joelnet commented Apr 9, 2019

If it's my own project, and I do this throughout the entire project, I prefer:

const abc = x =>
  x === 0 ? zero()
  : x === 1 ? one()
  : x === 2 ? two()
  : x === 3 ? three()
  : other()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.