Need help on the convincing the team not to use !!...
# android
n
Need help on the convincing the team not to use !! operator. Recently switched jobs and the code base is full of !!. Any article with a strong recommendation from Kotlin itself?
👍 1
g
The worst thing about
!!
is that it’s often actually not needed and it’s just bad understanding of the language Almost in all cases it can be avoided
But sometimes, especially for cases where some kind complex lifecycle is involved, you should actually crash isntead of using
?
But problem with !! in this case is very bad error message, so I prefer do something like: someLifecycleDependantProperty ?: error(“Wrong state $currentState. blha blah”)
☝️ 1
so It will get IllegalStateException with some debug data instead of simple “KotlinNullPointerException
n
That's my take too. But difficult to stop the team without a strong guidance and proof from Kotlin itself.
g
I’m not sure what kind proof is needed
it should be blessed by someone?
n
Arr.. I would put it this way. They have been recently transitioning from Java to Kotlin. Probably the team has been comfortably using ever since. So comfortable that every single file has multiple occurrences ( thanks to convert java to kotlin tool in Android studio). Their point is if Kotlin is giving that option why not use it. They have not suffered because of it so far.
g
Maybe right way to convince is to show options which allow avoid
!!
👍 1
r
lol I actually don't think its a big deal if used correctly . After all its a language feature, like they mentioned.. However I would say, why are they using? If its everywhere perhaps they need to think about nullablitiy first
👍 2
t
What Kotlin officially tells about
!!
: https://twitter.com/kotlin/status/1309153139753390083
❤️ 1
n
I had presented them other options and showed them how using !! would backfire. I've decided to point out in every code review it creeps out and created '!!' Cleanup task in the backlog to work on module by module. Thanks for your inputs.
k
In our team we have strong policies about coding style, all code must be integrated by pull-request, and if contains
!!
should be rejected. In your case, I suggest to convence the leader to implement this policies
i
I only use !! If you know it never to be null
r
@kristianconk there are situations where it could make sense. For instance, platform type that don't have nullability annotation. Policies that restrict parts of the language seem extreme. After all its not like that syntax was added to the language by mistake