Hey everyone :wave: Quick question, as my attempt...
# compose
g
Hey everyone 👋 Quick question, as my attempts at searching returned nothing In compose, when dealing with nullable types, which is more idiomatic of the two?
Copy code
@Composable
fun MyComposable(input: Type?) {
  if (input != null) {
     XComposable(input)
  } else {
     YComposable()
  }
}
vs
Copy code
@Composable
fun MyComposable(input: Type?) {
  input?.let {
    XComposable(input)
  } ?: YComposable()
}
My intuition is telling me that the first one is more idiomatic + it seems easier to understand at a glance but I can’t seem to find any references for that (so if you know of any I’d appreciate linking them)
1️⃣ 5
s
Same as any kotlin code. 1st is clear about it's intentions 2nd is quirky and hides a footgun that both branches will in fact run if the last thing you call inside the body of let happens to return
null
🙏 2
a
I have also made it a personal rule to only use
?.let {}
without any elvis operator and use
if-else
if I need the null-case to be handled. More often than not, to avoid unnecessary nesting, I would do
Copy code
if (input == null) {
    return
}
especially if there are several nullable parameters.
💯 2
Fail early and fast.