Last active
December 15, 2015 21:36
-
-
Save martynchamberlin/de791192f320aaa1d54b to your computer and use it in GitHub Desktop.
Simplifying complex conditional expressions
This file contains 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
/** | |
* 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