Reverse Conditional

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 );

image/svg+xml

 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

  • Remove negative from conditional.
  • Switch clauses
  • Compile and test.

There's further discussion on this kind of refactoring on Ward's wiki.