android-studio - IntelliJ IDEA : Boolean method XX is always inverted

标签 android-studio intellij-idea

我在 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/

相关文章:

Android Firestore : Failed to gain exclusive lock to the Firestore client's offline persistence

android - 如何修复 Android Studio 中找不到 Android 插件的问题

android - Android Studio ndk-build使用非零值2完成

android - pdfbox android studio 中的 NoClassDefFoundError

IntellIJ 中的 Android 项目 - 错误 : Activity can't be created

java - IntelliJ IDEA 13 Scala Plugin & Eclipse Kepler 编译报错

intellij-idea - Intellij-for循环的类型是否具有自动完成功能?

Android Studio - 表格布局大小?

intellij-idea - -Dkotlinx.coroutines.debug 在 IntelliJ IDEA JVM 选项中不起作用

java - 如何在 Android Studio 和 Gradle 中设置 -source 1.7