Last active
July 6, 2018 13:10
-
-
Save AmyStephen/6818939 to your computer and use it in GitHub Desktop.
In Defense of Positive Logic over Happy Path
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
<?php | |
/** | |
* Option 1: Happy Path (! $this) | |
*/ | |
// 1. The world begins. | |
if (! $this) { | |
$result = 'No complaints. This is easy to understand and follow.'; | |
} | |
// 2. As time goes on, it is not unusual to add conditions... | |
if (! $this) { | |
$result = 'Later, an else is added and IF NOT $this ELSE $that is not intuitive.'; | |
} else { | |
$result = 'But, rarely do developers rearrange an IF statement to add a condition.'; | |
} | |
// 3. NOTs and ORs are toxic, buggy, unintuitive | |
if (! $this || $that) { | |
$result = 'As with most logic, conditions grow in complexity until finally refactoring occurs.'; | |
$result .= 'Now what started as a simple if NOT is confusing, not at all intuitive. '; | |
$result .= 'Did the dev intend to code if this is false or that is true? '; | |
$result .= 'Or, was the intention that if either was false?'; | |
$result .= 'Combining nots and ors is difficult logic to follow, easy to create a bug.'; | |
} | |
/** | |
* Option 2: Positive Path -- IF $this THEN that | |
*/ | |
// 1. Starts | |
if ($this) { | |
} else { | |
$result = 'Annoying to those taught "happy path" but otherwise easy to understand.'; | |
} | |
// 2. 2nd Condition added | |
if ($this) { | |
$result = 'See how easily the second condition slides in?'; | |
} else { | |
$result = 'This code does not change and it is still intuitive.'; | |
} | |
// 3. Positive logic avoids NOTs and ORs which are very difficult to follow | |
if ($this && $that) { | |
} else { | |
$result = 'Oh. I guess they meant if either this or that are false. Why not say so?'; | |
$result .= 'Throughout the evolution of the code, the structure and flow never changed.'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment