【发布时间】:2019-06-06 05:12:51
【问题描述】:
我的代码如下
private fun validateInput(): Boolean {
if (etReportRow1.text.toString() == ""
|| etReportRow2.text.toString() == ""
|| etReportRow3.text.toString() == "")
return false
else
return true
}
编译器告诉我
冗余 'if' 语句 less... (Ctrl+F1) 此检查报告 if 可以简化为单个语句的语句。例如: if (foo()) { return true } else { return false } 可以 简化为返回 foo()。
建议的代码不会进入循环吗?
【问题讨论】:
-
你可以直接返回!(etReportRow1.text.toString() == "" || etReportRow2.text.toString() == "" || etReportRow3.text.toString() == " ")
-
顺便说一句,避免使用
==进行对象(字符串)比较 -
@MauricePerry 这是一个 kotlin 问题,它用
==覆盖了equals(),所以这可以正常工作 -
@MauricePerry 如果我们不犯错误,我们将无法改进 ;)
-
@Lino ... 而且我们不需要像 stackoverflow 这样的网站
标签: if-statement kotlin