IntelliJ IDEA:布尔方法XX总是被反转

mobileDev:

我在IntelliJ中运行lint时警告“布尔方法总是反转”。我的代码库中有几个类似的警告。我想念哪种基本编码风格?

public static boolean isBlueToothEnabled(){
    final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if(bluetoothAdapter != null)
        return bluetoothAdapter.isEnabled();
    return  false;
}
罗宾·文森特:

尝试返回false是否bluetoothAdapter为null,否则返回的输出isEnabled()

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

阅读更多:

罗伯特·马丁(Robert Martin)在《清洁规范》中写道:“负面因素比积极因素难理解。因此,在可能的情况下,条件应该表示为肯定。” (马丁,[G29])。IntelliJ IDEA进行了三项检查,以帮助您保持乐观。

https://blog.jetbrains.com/idea/2014/09/the-inspection-connection-issue-2/(条目4:避免出现负面条件)

https://www.jetbrains.com/help/idea/2016.1/invert-boolean.html

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章