Remove Double Negative (by Ashley Frieze and Martin Fowler) <IMG SRC = "http://www.refactoring.com/catalog/new.gif" border=0>
2003. 8. 2. 14:42
Remove Double Negative
Refactoring contributed by Ashley Frieze and Martin Fowler
Make it a single positive conditional
if ( !item.isNotFound() )
![](http://www.refactoring.com/catalog/arrow.gif)
if ( item.isFound() )
Motivation
Double negatives are often frowned on by mavens of natural language. Often this frowning is inappropriate - certainly in English the double negative has its uses.
But that is not true in programming. There double negatives are just plain confusing. So kill them on sight.
Mechanics
- If you don't have a method with the opposite sense, create one. The body can just call the original (you can fix that later).
- Compile
- Replace each double negative with a call to the new function
- Compile and test after each replace
- If the negative form of the method isn't used elsewhere, use Inline Method to inline it into the positive form
- If you do need both forms, consider moving the body of the negative form over to the positive and reversing its sense, and then have the negative method call the positive one.
- Consider using Substitute Algorithm if the positive form is difficult to understand.
***** 아름다운프로님에 의해서 게시물 복사 + 카테고리변경되었습니다 (2003-12-18 17:27)