Created
April 5, 2012 18:57
-
-
Save cromwellryan/2313236 to your computer and use it in GitHub Desktop.
Moving from high churn to low churn, single responsibility, common closure-y code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function calculatebonus( employee, currentsalary ) { | |
var bonus = 0; | |
switch( employee.ReviewResult ) { | |
case "Good": | |
return 0.025 * currentsalary; | |
case "Great": | |
return 0.04 * currentsalary | |
case "InvestementBanker": | |
return 2 * currentsalary; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getBonusCalculator( employee ) { | |
switch( employee.ReviewResult) { | |
case "Good": | |
return new StandardBonusCalculator(); | |
case "Great": | |
return new ThrowEmSomeDoughBonusCalculator(); | |
case "InvestementBanker": | |
return new OpenTheVaultsBonusCalculator(); | |
} | |
return new NoBonusCalculator(); | |
} | |
function calculatebonus( employee, currentsalary ) { | |
var calc = getBonusCalculator( employee ); | |
return calc.getbonus(currentsalary); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function GoodReviewAndThreeYearsTenureBonusCalculator() { | |
function applies(employee) { | |
return employee.ReviewStatus === "Good" && employee.Tenure > 3 | |
} | |
function getbonus(currentsalary) { | |
return 0.025 * employee.Tenure * currentsalary; | |
} | |
return { applies: applies, getbonus: getbonus}; | |
} | |
registerCalculator(new GoodReviewAndThreeYearsTenureBonusCalculator()); | |
function StandardTenureBonusCalculator() { | |
function applies(employee) { | |
return employee.ReviewStatus === "Good" && employee.Tenure > 3 | |
} | |
function getbonus(currentsalary) { | |
return 0.025 * employee.Tenure * currentsalary; | |
} | |
return { applies: applies, getbonus: getbonus}; | |
} | |
registerCalculator(new StandardTenureBonusCalculator()); | |
// ... others | |
function getBonusCalculator( employee) { | |
for( i = 0; i < allcalculators.length; i++ ) { | |
var calc = allcalculators[i]; | |
if( calc.applies(employee) ) { | |
return calc; | |
} | |
} | |
} | |
function calculatebonus( employee, currentsalary ) { | |
var calc = getBonusCalculator( employee ); | |
return calc.getbonus(currentsalary); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment