Skip to content

Instantly share code, notes, and snippets.

@joepie91
Last active September 30, 2016 15:23
Show Gist options
  • Save joepie91/203aaa8e36e1eb958fe7 to your computer and use it in GitHub Desktop.
Save joepie91/203aaa8e36e1eb958fe7 to your computer and use it in GitHub Desktop.
Braces for conditionals
// The wrong way:
if (something === true)
doAThing();
// Requirements change, now you need to do two things:
if (something === true)
doAThing();
doASecondThing();
/* You now have a bug, and potentially a vulnerability. doASecondThing will ALWAYS run, whether your if
* statement is true or not. It's too easy to overlook this, and *will* go wrong at some point.
* Case in point: https://www.imperialviolet.org/2014/02/22/applebug.html */
// The correct way:
if (something === true) {
doAThing();
}
// Which, if requirements change, becomes:
if (something === true) {
doAThing();
doASecondThing();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment