Skip to content

Instantly share code, notes, and snippets.

@chakrit
Created July 11, 2012 09:37
Show Gist options
  • Save chakrit/3089304 to your computer and use it in GitHub Desktop.
Save chakrit/3089304 to your computer and use it in GitHub Desktop.
[refactor.io] entry/1 - Decomposed Conditional
if (date.before (SUMMER_START) || date.after(SUMMER_END))
charge = quantity * _winterRate + _winterServiceCharge;
else
charge = quantity * _summerRate;
if (notSummer(date))
charge = winterCharge(quantity);
else
charge = summerCharge (quantity);
// helpers
function notSummer(date) {
return date.before(SUMMER_START) || date.after(SUMMER_END);
}
function winterCharge(q) {
return q * _winterRate + _winterServiceCharge
}
function summerCharge(q) {
return q * _summerRate;
}

Original article: Refactoring: Decompose Conditional

Applicable for:

All languages and programming styles, but usually more applicable for imperative langauges.

Benefits:

Usually results in longer code but with much better readability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment