Skip to content

Instantly share code, notes, and snippets.

@pvdz
Created May 26, 2013 09:29
Show Gist options
  • Save pvdz/5652201 to your computer and use it in GitHub Desktop.
Save pvdz/5652201 to your computer and use it in GitHub Desktop.
// original: https://gist.github.com/qfox/5652198
// this would use dynamic variable access to store vars from different funcs
// dynamic prop access could very well kill optimizations though
// step1: change var
var $state = {};
function start(x){
$state['start:foo'] = 0;
if (x === 1) $state['start:foo'] = 1;
else if (x === 2) $state['start:foo'] = 2;
else if (x === 3) $state['start:foo'] = 3;
else if (x === 4) $state['start:foo'] = 4;
else $state['start:foo'] = 5;
return $state['start:foo'];
}
// step2: split up code branches
var $state = {};
function start(x){
$state['start:foo'] = 0;
if (x === 1) $state['start:foo'] = 1;
else $start1(x);
return $state['start:foo'];
}
function $start1(x){
if (x === 2) $state['start:foo'] = 2;
else $start2(x);
}
function $start2(x){
if (x === 3) $state['start:foo'] = 3;
else $start3(x);
}
function $start3(x){
if (x === 4) $state['start:foo'] = 4;
else $state['start:foo'] = 5;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment