Skip to content

Instantly share code, notes, and snippets.

@pvdz
Last active December 17, 2015 18:19
Show Gist options
  • Select an option

  • Save pvdz/5652207 to your computer and use it in GitHub Desktop.

Select an option

Save pvdz/5652207 to your computer and use it in GitHub Desktop.
// original: https://gist.github.com/qfox/5652198
// this rewrite tries to use globals for any local vars
// you'll have the burden of extra scopes, but it beats dynamic vars (https://gist.github.com/qfox/5652201)
// step1: change var
var $start_foo;
function start(x){
$start_foo = 0;
if (x === 1) $start_foo = 1;
else if (x === 2) $start_foo = 2;
else if (x === 3) $start_foo = 3;
else if (x === 4) $start_foo = 4;
else $start_foo = 5;
return $start_foo;
}
// step2: split up code branches
var $start_foo = {};
function start(x){
$start_foo = 0;
if (x === 1) $start_foo = 1;
else $start1(x);
return $start_foo;
}
function $start1(x){
if (x === 2) $start_foo = 2;
else $start2(x);
}
function $start2(x){
if (x === 3) $start_foo = 3;
else $start3(x);
}
function $start3(x){
if (x === 4) $start_foo = 4;
else $start_foo = 5;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment