https://kotlinlang.org logo
#coroutines
Title
# coroutines
h

Hexa

03/16/2019, 9:07 AM
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

Dominaezzz

03/16/2019, 9:13 AM
Remove
?: return CarNotFound("not found")
?
Or add
.takeUnless { it.isEmpty() }
right after the
await
.
h

Hexa

03/16/2019, 9:30 AM
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

Dominaezzz

03/16/2019, 9:33 AM
Yes
h

Hexa

03/16/2019, 9:40 AM
would
takeUnless
be more idiomatic in this case? @Dominaezzz. it's not as readable but i think it just takes getting used to
d

Dominaezzz

03/16/2019, 9:43 AM
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

Hexa

03/16/2019, 9:44 AM
cool, thank you
3 Views