Is it better to write code as in methodA, methodB,...
# coroutines
l
Is it better to write code as in methodA, methodB, or methodC, why and what is the difference if any?
d
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
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
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
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