3 ms·
I did not want to claim this. But there are times where you can be sure that the nullable object is not null. You got the double-exclamation-point (!!) for thi
by tommyage 7y ago
I did not want to claim this.
But there are times where you can be sure that the nullable object is not null.
You got the double-exclamation-point (!!) for this. But some calls to Android-facilities doesnt accept this, so the IDE notifies you about an var, which could have changed at this point in time.
Resulting in multiple !! in the same scope of function.
- distances 7y agoFor me !! is a bad code smell. It is appropriate sometimes, but very rarely. Usually you can write it better with ?.let/apply/also/etc scope functions. Assigning it to temporary val can be done as a workaround for doing !! twice, but as said, probably needs a scope function instead. Same with vars, they are still needed every now and then but most of a clean codebase is hopefully vals and immutable data classes.
- tommyage 7y agoThe thing with scope functions is, you are doing nothing if it is null, returning default (even magic..) value or throwing an unchecked exception, even though you want to force your colleague to handle his failure accordingly
- distances 7y agoYou can always do something else for the null case, but this totally depends on the context. Hard to say anything generic. Throwing an error is always an option too if the value is expected.
- int_19h 7y agoThe whole point is to force you to think about your assumptions every time you write that "!!". It might be annoying, but it's less annoying than debugging an obscure user-reported NPE later on.