is there a better way to remove the `if(carId.is...
# coroutines
h
is there a better way to remove the
if(carId.isNullOrEmpty())
statement here as it looks duplicated code to me.
deferredCarIdResult.await()
can return an empty string like this "" so I return CarNotFound("not found") but when the coroutines timeout I also return CarNotFound("not found")
Copy code
val carId = runBlocking {
                    withTimeoutOrNull(10000L) {
                        deferredCarIdResult.await()
                    }
                } ?: return CarNotFound("not found")
                
                if(carId.isNullOrEmpty()){
                    return CarNotFound("not found")
                }
d
Remove
?: return CarNotFound("not found")
?
Or add
.takeUnless { it.isEmpty() }
right after the
await
.
h
thanks I think removing
?: return CarNotFound("not found")
is more readable so I'll do that
if i add
.takeUnless { it.isEmpty() }
can I lose the if the statement then @Dominaezzz?
d
Yes
h
would
takeUnless
be more idiomatic in this case? @Dominaezzz. it's not as readable but i think it just takes getting used to
d
Hmm, I'm not sure if it's more idiomatic per se but the more readable option is more idiomatic.
Short answer is no.
h
cool, thank you