Colton Idle
09/25/2020, 4:13 AMAdam Powell
09/25/2020, 4:23 AM.let {}
, etc.Adam Powell
09/25/2020, 4:31 AM!!
gildor
09/25/2020, 4:34 AMgildor
09/25/2020, 4:35 AMAdam Powell
09/25/2020, 4:35 AMgildor
09/25/2020, 4:36 AMAdam Powell
09/25/2020, 4:36 AMgildor
09/25/2020, 4:36 AMAdam Powell
09/25/2020, 4:37 AMAdam Powell
09/25/2020, 4:37 AMColton Idle
09/25/2020, 4:52 AMAdam Powell
09/25/2020, 4:55 AM!!
, but it's not the end of the worldAdam Powell
09/25/2020, 4:55 AMColton Idle
09/25/2020, 4:58 AMMark Murphy
09/25/2020, 11:34 AMby fragment B I know that it should not be null (as per some business requirement or something). That would be a valid use case no?FWIW, I would argue "no". The problem is that "I know that it should not be null" is a short-lived state. Code changes. Platforms change. Devices change. Developers change. Business requirements change. And one day,
null
might show up as a result, and then your app crashes hard. !!
is convenient but unforgiving.Colton Idle
09/25/2020, 6:28 PM?
you're just potentially hiding issues. I would rather take the crash over unexpected behavior in the app? Unless you're still saying that I should assert the invariant, but I should instead use some other method.Mark Murphy
09/25/2020, 6:55 PMI feel like if you just use aYou handle the possibility ofyou're just potentially hiding issues?
null
. Exactly what your response is to null
is would depend on your business logic. If nothing else, log a message with whatever sort of system you're using for that sort of thing (e.g., log a non-fatal with Crashlytics) and deal with the situation gracefully for your user.
I would rather take the crash over unexpected behavior in the appYour users might not like that approach. If you feel that you have a sufficiently-robust test suite that 100% of
!!
crashes would be caught in testing, so you feel comfortable with !!
, you're welcome to go that route. I don't know of many places that have testing strong enough that would make that approach viable, but that's just me.Colton Idle
09/25/2020, 6:56 PMAdam Powell
09/25/2020, 7:02 PM!!
Adam Powell
09/25/2020, 7:03 PMColton Idle
09/25/2020, 7:04 PMAdam Powell
09/25/2020, 7:05 PMAdam Powell
09/25/2020, 7:05 PMAdam Powell
09/25/2020, 7:07 PMvar result: Result? = null
doFirstThing()
result = doSecondThing()
vs.
doFirstThing()
val result = doSecondThing()
Adam Powell
09/25/2020, 7:08 PM