Created
February 23, 2014 23:56
-
-
Save getify/9179137 to your computer and use it in GitHub Desktop.
if..else if..else --> you used block-less else!
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
// If I understand JS grammar and the parsing of it correctly, | |
// `if .. else if ..` is a case of using else without { }, as | |
// it's treated almost the same as `if .. else { if .. }`. | |
// | |
// Those two have *slightly* different parse trees, but the | |
// nesting is clear that `if` statements only have one | |
// consequent and one alternate, so `else if ..` has to be treated | |
// as `else { if ..`. | |
// | |
// Bottom line? If you use `else if`, you're basically committing | |
// the "sin" of curly-brace-less `if`, the same "sin" everyone | |
// complained about with the iOS/OSX #gotofail. | |
// So.. this code: | |
if (foo) { | |
// .. | |
} | |
else if (bar) { | |
// .. | |
} | |
else if (bam) { | |
// .. | |
} | |
else { | |
// .. | |
} | |
// ..is actually treated as: | |
if (foo) { | |
// .. | |
} | |
else { | |
if (bar) { | |
// .. | |
} | |
else { | |
if (bam) { | |
// .. | |
} | |
else { | |
// .. | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment