```// 1 something?.let { return it }``` Or ```// ...
# codingconventions
l
Copy code
// 1
something?.let { return it }
Or
Copy code
// 2
if(something != null) { return something }
2️⃣ 11
1️⃣ 5
m
If
something
is mutable and accessible from other threads,
2
might still return null
a
won't smart-cast not be available in that case?
m
Smart cast to 'Int' is impossible, because 'something' is a mutable property that could have been changed by this time
(assuming
something
is an Int?) (so the compiler actually stops you from making this mistake)
e
IMO I like the option 2 for being simpler for people that aren't kotlin developers. 99.999997% (made up stats) programming languages have a if statement.
c
My commentary would be to refrain from multiple/short-circuiting returns. For me this would be prime example to use if as an expression: return
if (something == null) calculateAlternative() else something
Or even
return something ?: calculateAlternative()
e
Lovely usage of the elvis operator.
g
also for short expressions you can remove curly praces and option 2 becomes even shorter and more clean:
Copy code
if (something != null) return something