idea提示那些事

1 篇文章 0 订阅
1 篇文章 0 订阅

在用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的无参函数都不必加括号


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值