Reverse Conditional

Refactoring contributed by Bill Murphy and Martin Fowler

You have a conditional that would be easier to understand if you reversed its sense.

Reverse the sense of the conditional and reorder the conditional's clauses.

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

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

Motivation

Often conditionals can be phrased in way that makes them hard to read. This is particularly the case when they have a not in front of them. If the conditional only has a "then" clause and no "else" clause, this is reasonable. However if the conditional has both clauses, then you might as well reverse the conditional.

Mechanics

| Refactoring Home | | Alphabetical List |