Skip to content

Instantly share code, notes, and snippets.

@billforward-alex
Created July 29, 2016 11:17
Show Gist options
  • Save billforward-alex/3fc708c8a5c69c3104b575e55fee3b9a to your computer and use it in GitHub Desktop.
Save billforward-alex/3fc708c8a5c69c3104b575e55fee3b9a to your computer and use it in GitHub Desktop.
<?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