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

Luis Munoz

05/06/2020, 2:53 AM
Is it better to write code as in methodA, methodB, or methodC, why and what is the difference if any?
d

Dominaezzz

05/06/2020, 2:56 AM
Considering what the JVM does with strings. I'd say
methodC
.
I just noticed the contents of the string. Yeah
methodB
. (
methodA
is just
mathodC
with overhead)
l

Luis Munoz

05/06/2020, 3:07 AM
What does the JVM do with Strings? I was thinking probably methodB or methodC, not sure they have a difference (maybe when an error occrus in xResult something different happens). methodA seems wrong because it runs code then returns a flow while the others don't run code until the flow is collected. I think it is more rare to run code then return a flow with the result, that is a case where you want to collect with same value multiple times.
s

svenjacobs

05/06/2020, 6:37 AM
I personally prefer solution B because then
xResult
is only evaluated lazily when the Flow is collected. In both solutions A and C
xResult
is computed when the function is called, but the Flow might never be collected? 🤷🏼‍♂️
☝🏻 1
👍 2
z

Zach Klippenstein (he/him) [MOD]

05/06/2020, 3:21 PM
Because in A and C recollecting the flow will not perform the network request again, it's impossible to implement retry logic via flow operators. It also means your methods will also have to be suspending if your service call is, which is a smell in my opinion.
👍 3
3 Views