Rob Pridham
May 25, 2024

--

Yes, this. Using let for values you don't want to consume is actually dangerous.

nullableValue?.let {
doSomething()
} ?: run {
doSomethingElse()
}

If doSomething() happens to return null, then this function will break in that it'll run both blocks, not the expected design. Perhaps not very likely in practice but best avoided.

--

--

Rob Pridham
Rob Pridham

Written by Rob Pridham

Principal Software Engineer, BBC News/Sport/Weather Apps

No responses yet