Skip to content

Instantly share code, notes, and snippets.

@martynchamberlin
Last active December 15, 2015 21:36
Show Gist options
  • Save martynchamberlin/de791192f320aaa1d54b to your computer and use it in GitHub Desktop.
Save martynchamberlin/de791192f320aaa1d54b to your computer and use it in GitHub Desktop.
Simplifying complex conditional expressions
/**
* The problem
*/
var A = true;
var B = true;
var C = false;
var D = false;
if ( A && B || C && ! D ) {
// If you ever need to update this conditional, you're screwed
}
/**
* The solution
*
* First, group things together in new variables, improving your previous variable names.
* If you're going to use this elsewhere, an even better idea is to break it out into a
* separate function.
*/
var timerWorks = A && B;
var batterIsLow = !C || D;
var D = A && B;
if ( timerWorks && !batteryIsLow ) {
// see how much easier it is to know when this condition will pass?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment