Reverse Conditional (by Bill Murphy and Martin Fowler) <IMG SRC = "http://www.refactoring.com/catalog/new.gif" border=0>
2003. 8. 3. 12:43
Reverse Conditional
Refactoring contributed by Bill Murphy and Martin Fowler
Reverse the sense of the conditional and reorder the conditional's clauses.
if ( !isSummer( date ) )
charge = winterCharge( quantity );
else
charge = summerCharge( quantity );
![](http://www.refactoring.com/catalog/arrow.gif)
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 the wiki.
***** 아름다운프로님에 의해서 게시물 복사 + 카테고리변경되었습니다 (2003-12-18 17:27)