Is there a more elegant way to do something like t...
# codingconventions
e
Is there a more elegant way to do something like this:
Copy code
fun getSomething(): SomeType {
   val result = computeSomething()
   doSomethingElse()
   return result
}
👌 2
s
Copy code
return computeSomething().also { doSomethingElse() }
🥇 2
s
or even convert it to function assignment altogether (you could also skip return type if it's clear):
Copy code
fun getSomething() = computeSomething().also { doSomethingElse() }
🥇 1
j
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 simple smile.
☝️ 1
e
@Jordan Petersen Good point.
👍 1