[英]IntelliJ IDEA: Boolean method XX is always inverted
I am getting warning " Boolean method is always inverted " on running lint in IntelliJ.我在 IntelliJ 中运行 lint 时收到警告“布尔方法总是被反转”。 I have several similar warnings in my codebase.我的代码库中有几个类似的警告。 Which basic coding style, am I missing?我错过了哪种基本编码风格?
public static boolean isBlueToothEnabled(){
final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter != null)
return bluetoothAdapter.isEnabled();
return false;
}
try to return false
if bluetoothAdapter
is null otherwise return the output of isEnabled()
如果bluetoothAdapter
为 null 则尝试返回false
否则返回isEnabled()
的输出
public static boolean isBlueToothEnabled(){
final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter == null){
return false;
}
return bluetoothAdapter.isEnabled();
}
Read more:阅读更多:
In Clean Code, Robert Martin writes, “Negatives are just a bit harder to understand than positives.在 Clean Code 中,Robert Martin 写道:“消极比积极更难理解。 So, when possible, conditionals should be expressed as positives.”因此,在可能的情况下,条件应表示为正数。” (Martin, [G29]). (马丁,[G29])。 IntelliJ IDEA has three inspections to help you stay positive. IntelliJ IDEA 有三项检查可帮助您保持积极态度。
https://blog.jetbrains.com/idea/2014/09/the-inspection-connection-issue-2/ (Entry #4 Avoiding negative Conditionals) https://blog.jetbrains.com/idea/2014/09/the-inspection-connection-issue-2/ (条目#4 避免否定条件)
https://www.jetbrains.com/help/idea/2016.1/invert-boolean.html https://www.jetbrains.com/help/idea/2016.1/invert-boolean.html
Summary: Method always called like !method().总结:方法总是像 !method() 一样被调用。 It's more efficient to have less number of computation.计算次数越少效率越高。 Refactor the method and return oppositely and call the method without (!)重构方法并反向返回并调用不带 (!)
Why it happens为什么会发生
If we have a method foo()
如果我们有一个方法foo()
boolean foo()
{
if(condition == true)
return true;
else
return false;
}
And is only called like !foo()
,并且只被称为!foo()
,
class A
{
if(!foo())
do something;
...
if(!foo())
do something else;
}
Because we only call !foo()
and did not call foo()
.因为我们只调用!foo()
而没有调用foo()
。
The warning asks us to use foo()
in a positive way.警告要求我们以积极的方式使用foo()
。
Remove the warning删除警告
By inverting the return value of the method foo(),通过反转方法 foo() 的返回值,
boolean foo()
{
if(condition == true)
return **false**;
else
return **true**;
}
Now call the method现在调用方法
class A
{
if(foo())
do the same thing;
}
I must say that there are cases where I think this warning isn't even fixable in a reasonable way.我必须说,在某些情况下,我认为此警告甚至无法以合理的方式修复。 Take for example code looking like this:以如下代码为例:
boolean getInfo(Info info) {
// Retrieve some info that can fail so the function returns true or false
}
void func() {
Info info;
if (!getInfo(info)) {
// Failed; handle error, early out
return;
}
// Do stuff
if (!getInfo(info)) {
// Failed; handle error, early out
return;
}
// Do stuff
if (!getInfo(info)) {
// Failed; handle error, early out
return;
}
// Do final calculations
}
What to do in a case like this?遇到这种情况怎么办? Rename the function to notGetInfo()
?将函数重命名为notGetInfo()
? getDesinformation()
? getDesinformation()
? Don't early out of the function on failure resulting in a growing tree of brackets?不要在失败时提前退出函数导致括号树不断增长?
For simplify, See the code below:为简化起见,请参阅以下代码:
public boolean isBoy(int sex){ return sex == boy};
if(!isBoy(sex)){ // todo}
If do so, it called inverted. How to solve? see the other code below:
public boolean isGirl(int sex){ return sex == girl};
if(isGirl(sex)){ // todo}
Because you need to judge the condition whether is a girl, So that you should avoid to judge whether is a boy and put a '!'.因为要判断条件是否是女孩,所以要避免判断是否是男孩,并加上'!'。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.