Nik*_*ntz 4 java refactoring boolean-logic demorgans-law
我可以像这样重构,这些是等价的,因此更简单直接的代码版本是首选吗?
在重构之前:
if (!matcher.matches() && !matcher2.matches() && !matcher3.matches()
&& !matcher4.matches() && !matcher5.matches()
&& !matcher6.matches() && !matcher7.matches()
&& !matcher8.matches()) {
return true;
} else
return false;
Run Code Online (Sandbox Code Playgroud)
重构后:
return (matcher.matches() || matcher2.matches() || matcher3.matches()
|| matcher4.matches() || matcher5.matches()
|| matcher6.matches() || matcher7.matches()
|| matcher8.matches())
Run Code Online (Sandbox Code Playgroud)
实际上,没有.第一个是true仅当所有匹配器不匹配时.如果所有匹配器在第二个语句中不匹配,则返回false
return !(matcher.matches() || matcher2.matches() || matcher3.matches()
|| matcher4.matches() || matcher5.matches()
|| matcher6.matches() || matcher7.matches()
|| matcher8.matches())
Run Code Online (Sandbox Code Playgroud)
这是对的