Android studio IntelliJ IDEA:布尔方法XX总是反向的

Android studio IntelliJ IDEA:布尔方法XX总是反向的,android-studio,intellij-idea,Android Studio,Intellij Idea,在IntelliJ中运行lint时,我收到警告“布尔方法总是反向的”。我的代码库中有几个类似的警告。我缺少哪种基本的编码风格 public static boolean isBlueToothEnabled(){ final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if(bluetoothAdapter != null) return bluetoothAd

在IntelliJ中运行lint时,我收到警告“布尔方法总是反向的”。我的代码库中有几个类似的警告。我缺少哪种基本的编码风格

public static boolean isBlueToothEnabled(){
    final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if(bluetoothAdapter != null)
        return bluetoothAdapter.isEnabled();
    return  false;
}

如果
bluetoothAdapter
为空,请尝试返回
false
,否则返回
isEnabled()的输出

阅读更多:

罗伯特·马丁在《干净的代码》中写道:“消极的东西只会稍微难一点 因此,在可能的情况下,条件句应该 表示为肯定。”(马丁[G29])。IntelliJ IDEA有三个 检查以帮助您保持积极

(条目#4避免使用否定条件)


总结:方法总是像这样调用!方法()。计算量越少,效率越高。重构方法并反向返回,调用不带(!)的方法。


为什么会这样

如果我们有一个方法
foo()

并且只被称为像
!foo()

因为我们只调用
!foo()
并且没有调用
foo()

警告要求我们以积极的方式使用
foo()

删除警告

通过反转方法foo()的返回值

现在调用该方法

    class A
    {
        if(foo())
            do the same thing;
    }

有关简化,请参见下面的代码:

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}

因为你需要判断条件是否是女孩,所以你应该避免判断是否是男孩而放一个“!”

我必须说,在某些情况下,我认为这一警告甚至无法以合理的方式得到纠正。以下面的代码为例:

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
}

在这种情况下该怎么办?将函数重命名为
notGetInfo()
<代码>获取设计信息()
?如果出现故障导致括号树不断增长,不要过早退出函数?

但是切换返回值的想法/目的是什么(我没有从您发布的链接中了解到)。这立即引出了一个问题:如何禁用该检查。这个案例看起来有效。但是,我有一个这种形式的单行方法:
validationMessages.stream().anyMatch(m->m.level()==level.ERROR)和我得到相同的警告。这对我来说没有意义。@我并没有花太多时间在这上面,但是你能用noneMatch而不是anyMatch来避免吗?Imho应该解决该警告,因为如果没有错误,您的方法将返回true,而如果有错误,则返回true。让我知道你的想法。
@SuppressWarnings(“BooleanMethodIsAlwaysInverted”)
这很好,但这只是为了删除警告吗?有什么实际好处吗?像性能或其他方面一样,可能会产生忽略不计但不必要的开销来抵消结果。如果您有更频繁地否定结果的方法调用,那么您就有相同的否定开销“拥有…”更有效”这个性能问题不应该在编译器级别处理吗?同意。为了可读性,此模式要好得多。。。至少对于
boolean
返回。当我查看类的方法时,看到名为
getInfo
的方法比
failedToGetInfo
getInfoFailure
方法更有意义。这会使我对该方法的主要目的感到困惑;它是在做什么,还是在检查失败?另一方面,在这种情况下,如果使用
void getInfo()
,并在失败时抛出异常,并在堆栈跟踪中找到解释,而不是返回一个模糊的布尔值,则可以说它更优雅(并且仍然非常可读)。这是一个很好的解决方案,虽然如果期望 GETFIGNE>代码>经常失败,但是必须考虑java中异常的高成本。
    boolean foo()
    {
        if(condition == true)
            return **false**;
        else
            return **true**;
    }
    class A
    {
        if(foo())
            do the same thing;
    }
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}
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
}