It is kind of ironic that in the same thread where we discuss increasing guidance for coders, we also lament IntelliJâs intervention. If you like more guidance, you should love IntelliJ!
IntelliJ guides coders all the time, flagging suspicious code and suggesting improvements.
For example, given the following code:
** def sillyToString(x: AnyRef): String = {
if(x.isInstanceOf[String]) {
x.asInstanceOf[String]
} else {
x.toString
}
}**
IntelliJ suggests to replace this by a pattern match, and if I accept the suggestion, turns it into:
** def sillyToString(x: AnyRef): String = {
x match {
case str: String =>
str
case _ =>
x.toString
}
}**
Note that not only the isInstanceOf, but also the asInstanceOf is gone.
If IntelliJ does not flag every instance of null or asInstanceOf, then that is probably because (1) the code probably does exactly what the coder intended it to do and (2) there is no obvious alternative. And in that case, a compiler warning probably wouldnât do much good either, because the coder either does not know the alternative, or decided the alternative was not good enough.
Too many compiler warning are not good, because it increases the chance that they will be disabled or ignored, especially if coders start to feel that the warnings are probably not bugs, but just perceived lack of idiomaticity.
Compiler warnings should be reserved for cases that are probably bugs, because only then people will feel compelled to take them seriously.
Guidance for code improvements should go to tools. Perhaps the compiler may emit, next to errors and warnings, also âadvisesâ.