idea提示那些事

来源:互联网 发布:windows pyqt5 教程 编辑:程序博客网 时间:2024/06/05 17:21

在用idea编程过程中会产生许多worning提示,细细查看这些提示,会得到许多好处:
- 优化代码,解除潜在bug
- 学习相关语言语法
现在把遇到的warning作总结如下。

求Array大小时候建议用length

Replace .size with .length on arrays and strings less
This inspection reports array.size and string.size calls. While such calls are legitimate, they require an additional implicit conversion to SeqLike to be made. A common use case would be calling length on arrays and strings may provide significant advantages.
Before:
Array(1, 2, 3, 4).size
"this is a string".size
After:
Array(1, 2, 3, 4).length
"this is a string".length

在求Array数量的时候建议用length函数提升效率


s字符串中包含变量时不加括号

val ss = "not find"
println(s"The error is ${ss}.")变为
println(s"The error is $ss.")


函数推导式不用加大括号

case _:Exception => {
log.error(s"wrong instruction : $s")
return Map()
}

应该为
case _:Exception =>
log.error(s"wrong instruction : $s")
return Map()


Map取值

Replace with .(key) less... (⌘F1)
Removes unnecessary gets when getting a value from map given a key.
Before:
map.get(k).get
After:
map(k)

直接用key从map中取值


调用无参函数

Java accessor method called as empty-paren less... (⌘F1)
Methods that follow JavaBean naming contract for accessors are expected to have no side effects. The recommended convention is to use a parameterless method whenever there are no parameters and the method have no side effect. This convention supports the uniform access principle, which says that client code should not be affected by a decision to implement an attribute as a field or method. The problem is that Java does not implement the uniform access principle. To bridge that gap, Scala allows you to leave off the empty parentheses on an invocation of function that takes no arguments.
* Refer to Programming in Scala, 10.3 Defining parameterless methods

在scala中调用无参函数或者调用java的无参函数都不必加括号


1 0
原创粉丝点击