Skip to content

Instantly share code, notes, and snippets.

@xiaoyunyang
Last active February 3, 2018 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiaoyunyang/dfc02f5625d83eaf8a72bc80a121cc7c to your computer and use it in GitHub Desktop.
Save xiaoyunyang/dfc02f5625d83eaf8a72bc80a121cc7c to your computer and use it in GitHub Desktop.
More Testable Code
// BETTER - more testable
const incrementedVal = (val) => val + 1
function substituteVal(val, low, high) {
if(val >= high) return high
else if(val <= low) return low
else return val
}
function shouldIncrement(val, disabled) {
return val >= 0 && val < 10 && !disabled
}
function valIncrementer(val, disabled) {
if( shouldIncrement(val, disabled) ) {
return incrementedVal(val)
}
return substituteVal(val, 0, 10)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment