如何消除IntelliJ中Boolean方法'findError'的总是反转警告?
解决IntelliJ警告:"Boolean method 'findError' is always inverted"
问题描述
我编写了Kotlin函数findError,功能符合预期,但IntelliJ IDE提示警告:"Boolean method 'findError' is always inverted"。我大致理解其含义,也知道可以抑制该警告,但作为初学者,不清楚如何在不改变功能的前提下改写代码消除警告,纠结是直接抑制还是采用更规范的写法。尝试去掉!符号改写后仍收到警告,怀疑Bas Leijdekkers关于检查机制的评论是正确的。
原函数代码
fun findError(puzzle: Array<IntArray>): Boolean { for (z in 0..8) { val blockNums = mutableListOf<Int>() val xNums = mutableListOf<Int>() val yNums = mutableListOf<Int>() for (index in 0..8) { xNums.add(puzzle[z][index]) yNums.add(puzzle[index][z]) blockNums.add(puzzle[blocks.xy[z + 1][index]][blocks.xy[z][index]]) if (blockNums.count() != blockNums.toSet().count() || yNums.count() != yNums.toSet().count() || xNums.count() != xNums.toSet().count()) return false } } return true }
Blocks数据类代码
public final data class Blocks public constructor(blockNums: kotlin.collections.MutableList<kotlin.Int>, blockNumsFinal: kotlin.collections.MutableSet<kotlin.Int>, xy: kotlin.collections.List<kotlin.collections.List<kotlin.Int>>) { public final val blockNums: kotlin.collections.MutableList<kotlin.Int> /* compiled code */ public final val blockNumsFinal: kotlin.collections.MutableSet<kotlin.Int> /* compiled code */ public final var xy: kotlin.collections.List<kotlin.collections.List<kotlin.Int>> /* compiled code */ public final operator fun component1(): kotlin.collections.MutableList<kotlin.Int> { /* compiled code */ } public final operator fun component2(): kotlin.collections.MutableSet<kotlin.Int> { /* compiled code */ } public final operator fun component3(): kotlin.collections.List<kotlin.collections.List<kotlin.Int>> { /* compiled code */ } }
改写后的函数代码
fun findError(puzzle: Array<IntArray>): Boolean { val blockNums = mutableListOf<Int>() val xNums = mutableListOf<Int>() val yNums = mutableListOf<Int>() var counts = 0 for (z in 0..8) { blockNums.clear() xNums.clear() yNums.clear() for (index in 0..8) { xNums.add(puzzle[z][index]) yNums.add(puzzle[index][z]) blockNums.add(puzzle[blocks.xy[z + 1][index]][blocks.xy[z][index]]) if (blockNums.count() == blockNums.toSet().count() && yNums.count() == yNums.toSet().count() && xNums.count() == xNums.toSet().count()) { counts++ } } } return counts == 81 }
解决方案
核心原因
警告的本质是函数名语义与返回值逻辑不匹配:findError字面意思是“查找错误”,正常逻辑下应该返回true表示“找到错误”,但你的函数返回true时代表“未发现错误”,返回false时才是“找到错误”,这种颠倒的语义让IDE判断调用方大概率会用!取反,因此抛出警告。
规范改写(推荐)
直接修改函数名,让名称与返回值逻辑完全匹配,这是最符合代码可读性原则的做法,也能彻底消除警告:
// 函数名改为isValid,返回true代表数独有效(无错误) fun isValid(puzzle: Array<IntArray>): Boolean { for (z in 0..8) { val blockNums = mutableListOf<Int>() val xNums = mutableListOf<Int>() val yNums = mutableListOf<Int>() for (index in 0..8) { xNums.add(puzzle[z][index]) yNums.add(puzzle[index][z]) blockNums.add(puzzle[blocks.xy[z + 1][index]][blocks.xy[z][index]]) if (blockNums.count() != blockNums.toSet().count() || yNums.count() != yNums.toSet().count() || xNums.count() != xNums.toSet().count()) return false } } return true }
你也可以用hasNoErrors这类名称,只要语义和返回值逻辑一致即可。
替代方案:抑制警告(不推荐)
如果必须保留原函数名,可以通过注解抑制警告,但这会降低代码可读性,不建议长期使用:
@Suppress("BooleanMethodIsAlwaysInverted") fun findError(puzzle: Array<IntArray>): Boolean { // 原函数代码 }
关于你改写的版本
你改写的版本通过计数判断逻辑是可行的,但效率低于原版本(原版本发现错误会提前返回,改写版本需要遍历全部81个元素),且核心问题(函数名与返回值语义颠倒)没有解决,因此仍会触发警告。
内容的提问来源于stack exchange,提问作者JD74
相关产品推荐
相关产品推荐

