我在 IntelliJ 中运行 lint 时收到警告“ bool 方法总是被反转”。我的代码库中有几个类似的警告。我错过了哪种基本编码风格?
public static boolean isBlueToothEnabled(){
final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter != null)
return bluetoothAdapter.isEnabled();
return false;
}
最佳答案
尝试返回 false
如果 bluetoothAdapter
为空,否则返回 isEnabled()
的输出
public static boolean isBlueToothEnabled(){
final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter == null){
return false;
}
return bluetoothAdapter.isEnabled();
}
阅读更多:
In Clean Code, Robert Martin writes, “Negatives are just a bit harder to understand than positives. So, when possible, conditionals should be expressed as positives.” (Martin, [G29]). IntelliJ IDEA has three inspections to help you stay positive.
https://blog.jetbrains.com/idea/2014/09/the-inspection-connection-issue-2/
(条目 #4 避免否定条件)
https://www.jetbrains.com/help/idea/2016.1/invert-boolean.html
关于android-studio - IntelliJ IDEA : Boolean method XX is always inverted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38047367/