Probably the worst inspection IDEA has to offer :s...
# random
m
Probably the worst inspection IDEA has to offer 😅 Esp. bad for people who are new to the language.
😟 2
w
Why?
m
Because it completely changes the meaning from exception to simply omitting some code from being executed. In most cases you want to properly handle unexpected
null
and not simply skip some code. It does make sense in some cases, but the “weak warning” here can suggest new developers that using return is always the better option.
Actually at KotlinConf this year there was one talk that discouraged exactly that approach - ignoring
null
to avoid the
!!
😄
a
Heh, I thought inspections usually didn't change the functionality of code
m
Inspections don’t. But developers relying on them do.
w
Ah, I missed that the inspection changes semantics 😕
p
My most unloved inspection is to replace nested null checks with
?
calls
n
Was expecting the IntelliJ inspection to have the option to replace !! with ?: defaultValue, eg
val a = number ?: 0
.
m
Returning or using a default value both makes sense in some scenarios. Note that while it’s easy to come up with default values for primitives, it won’t work for most class types. But there usually is a reason that someone uses
!!
in the first place, because the value is expected to be non-null. Treating it as a warning with the suggestion to change it either to
?: return
or
?: default
will completely change the intention from “expected to be non-null” to “legit to be null so do something else in that case”. Hence the inspection is almost always misleading.
@Paul Woitaschek I’m not sure what you mean,. Could you give an example? 🙂
c
Yeah it's bad. `!!`will crash immediately while the suggestion will make the code appear to work (and probably crash later).
n
Using
!!
is good in Unit testing since if there are
null
issues you want to pick them up quickly, before they create a larger issue in a program/library.
b
That's the erlang way, crash early, be happy
😁 1
c
Or you could use a statically typed language and not crash in the first place
🤣 4
b
Having the compiler bark at you is just a "crash early" 😉
🙄 2
☝️ 1
🤔 1
➕ 1
Talking about that, there was a great talk at kotlinconf on how to do typing properly (someone from soundcloud IIRC)
t
Type are awesome but they don't save you from everything.