Skip to content

Instantly share code, notes, and snippets.

@cromwellryan
Created April 5, 2012 18: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 cromwellryan/2313236 to your computer and use it in GitHub Desktop.
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
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;
}
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);
}
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