butternut
butternut copied to clipboard
Optimise certain if-else statements
// input
if ( x ) {
foo = 1;
} else {
foo = 2
}
// output
x?(foo=1):(foo=2)
// better output
foo=x?1:2
// input
if ( x ) {
return 1;
} else {
return 2;
}
// output
if(x){return 1}else{return 2}
// better output
return x?1:2
There may be some more general rule that captures these specific examples, but having a special case for them wouldn't be the worst thing in the world.