Post

I love Kotlin but please don't abuse its features. I regularly see code like in the first function when you can instead write a simple if statement: it's shorter, easier to read, and doesn't allocate an object! Thankfully both functions compile to the same assembly when R8 is turned on.

My own pet peeve is when folks use `?.let {} ?:` instead of if/else when checking null It's less readable, and more error prone (because if "doThing()" accidentally returns null, then you end up running the ?: branch, which often is not what you actually want.

But the regular if else in Kotlin is shorter than takeIf/let

0 reposts6 likes

I actually think this is a unique example of philosophical incongruence in the language - typically Kotlin feels like a "we give you all the options/operators, pick wisely" stance but in this case they say just use the simple if/else construct

To be clear : I personally strongly think the if/else option is better in all scenarios (purely because of readability) but this always stood out to me as a rare exception