天天看点

Remove Double Negative(去除双重否定)WHATWHENHOW

WHAT

双重否定的条件语句,可以使用一个肯定的条件语句代替

记住:双重否定 = 肯定

本次重构涉及到的基本重构手法:

  1. Extract Method(提炼方法)
  2. Inline Method(内联方法)
  3. Substitute Algorithm(替换算法)

WHEN

双重否定,虽然机器能够很好理解并正确执行,但是不符合人的自然语言习惯,且令人迷惑,降低代码可读性

HOW

重构前的代码

package remove.doubles.negative;

public class SomeClass {
	
	private boolean found = false;

	public boolean isNotFound() {
		return !found;
	}

	public void someMethod() {
		if (!isNotFound()) { // double negative
			// do something
		}
	}
	
	public void someMethod2() {
		if (isNotFound()) { // negative
			// do something else
		}
	}
	
}
           

重构步骤

  • 提炼肯定条件方法:选中否定条件方法中的肯定部分(如果现在没有,替换算法,杜撰一个),使用快捷键(Alt + Shitf + M)或者上下文菜单(Refactor -> Extract Method ... ),弹出” Extract Method“对话框 
    Remove Double Negative(去除双重否定)WHATWHENHOW
    Remove Double Negative(去除双重否定)WHATWHENHOW
  • 方法名取名为:isFound,并选择Access Modifier为public,提取函数之后的代码
package remove.doubles.negative;

public class SomeClass {
	
	private boolean found = false;

	public boolean isNotFound() {
		return !isFound();
	}

	public boolean isFound() {// extracted positive conditional method
		return found;
	}

	public void someMethod() {
		if (!isNotFound()) { // double negative
			// do something
		}
	}
	
	public void someMethod2() {
		if (isNotFound()) { // negative
			// do something else
		}
	}
	
}
           
  • 针对每一个双重否定条件,将双重否定改为肯定
    • 先内联否定条件方法:在调用处,选择否定条件,使用快捷键(Alt + Shit + I)或者上下文菜单(Refactor -> Inline...),弹出“Inline Method”对话框,选择“Only the selected invocation”
      Remove Double Negative(去除双重否定)WHATWHENHOW
      Remove Double Negative(去除双重否定)WHATWHENHOW
    • 重构后的代码 
package remove.doubles.negative;

public class SomeClass {
	
	private boolean found = false;

	public boolean isNotFound() {
		return !isFound();
	}

	public boolean isFound() {// extracted positive conditional method
		return found;
	}

	public void someMethod() {
		if (!!isFound()) { // double negative, after inline method 
			// do something
		}
	}
	
	public void someMethod2() {
		if (isNotFound()) { // negative
			// do something else
		}
	}
	
} 
           
    • 替换算法,把双重否定改为肯定
package remove.doubles.negative;

public class SomeClass {
	
	private boolean found = false;

	public boolean isNotFound() {
		return !isFound();
	}

	public boolean isFound() {// extracted positive conditional method
		return found;
	}

	public void someMethod() {
		if (isFound()) { // changed double negative to positive
			// do something
		}
	}
	
	public void someMethod2() {
		if (isNotFound()) { // negative
			// do something else
		}
	}
	
}
           
  • 最后,如果否定条件方法isNotFound(),没有调用,使用隐藏方法(Hidden Method),或者只有少数几个调用,使用内联方法:Inline method 
    • 选中 isNotFound方法, 使用快捷键(Alt + Shit + I)或者上下文菜单(Refactor -> Inline...),弹出“Inline Method”对话框,选择“All invocations”,并选择“Delete method declaration”
      Remove Double Negative(去除双重否定)WHATWHENHOW
      Remove Double Negative(去除双重否定)WHATWHENHOW

重构后的代码

package remove.doubles.negative;

public class SomeClass {
	
	private boolean found = false;

	public boolean isFound() {// extracted positive conditional method
		return found;
	}

	public void someMethod() {
		if (isFound()) { // changed double negative to positive
			// do something
		}
	}
	
	public void someMethod2() {
		if (!isFound()) { // changed negative to use not positive
			// do something else
		}
	}
	
}
           

继续阅读