Decompose Conditional

last modified: October 2, 2004

http://www.refactoring.com/catalog/decomposeConditional.html

In other words, take a complex condition, like "if x and y and not z" and ExtractMethod the condition into a meaningful function -- like "if thisCustomerIsEligibleForADiscount".


[And a different but related issue based on the example at the link above...]

Also "if not ... else" ConsideredHarmful:

Instead of

if (notSummer(date))
  charge = winterCharge(quantity);
else
  charge = summerCharge(quantity);

consider

if (isSummer(date))
  charge = summerCharge(quantity);
else
  charge = winterCharge(quantity);

or

if (isWinter(date))
  charge = winterCharge(quantity);
else
  charge = summerCharge(quantity);

The first reads as "if not summer do the winter charge else do the summer charge." The second reads as "if it is summer do the summer charge, else do the winter charge."

else statements that are a double negative are difficult to understand.

see also ElseConsideredSmelly


Loading...