Skip to content

Instantly share code, notes, and snippets.

@billforward-alex
Created July 29, 2016 11:17
<?php
public function eatIfHungry() {
// states we need to think about:
// - isHungry
// - !isHungry
if (!$this->isHungry()) {
// states we need to think about:
// - !isHungry
return;
}
// states we need to think about:
// - isHungry
// nice! we permanently eliminated a branch.
// the number of states we need to consider only ever decreases
$this->eat();
}
public function eatIfHungry() {
// states we need to think about:
// - isHungry
// - !isHungry
if ($this->isHungry()) {
// states we need to think about:
// - isHungry
$this->eat();
}
// states we need to think about:
// - isHungry
// - !isHungry
// sad :( conditions that were ruled-out inside the `if` have come back
// the number of states we need to consider keeps changing:
// we need more contextual awareness to code like this
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment