https://kotlinlang.org logo
Title
e

Ellen Spertus

05/14/2022, 9:04 PM
Is there a more elegant way to do something like this:
fun getSomething(): SomeType {
   val result = computeSomething()
   doSomethingElse()
   return result
}
:yes: 2
s

Sam

05/14/2022, 9:06 PM
return computeSomething().also { doSomethingElse() }
🥇 2
s

Szymon Jeziorski

05/15/2022, 10:10 AM
or even convert it to function assignment altogether (you could also skip return type if it's clear):
fun getSomething() = computeSomething().also { doSomethingElse() }
🥇 1
j

Jordan Petersen

05/16/2022, 4:07 PM
In my opinion, these are all valid. However,
doSomethingElse()
trigged in a function called
getSomething()
could be a confusing side effect. I would say that should be called separately so that the only purpose of
getSomething()
is to get something 😒imple_smile:.
☝️ 1
e

Ellen Spertus

05/20/2022, 8:59 PM
@Jordan Petersen Good point.
👍 1