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.

33 reposts140 likes

Note that even when R8 is on, the first function will be more expensive in interpreted mode. It's only after compilation to aarch64 by ART that it becomes fully optimized. If you don't have R8 on, well… ART still does wonders on the final code mind you (no branches, and a single call).

Agree but with a caveat. Kotlin needs a ternary operation. It's the only syntax I envy from other langs. "if-else" seems verbose to me given how succinct the rest of the language is. This was discussed ages ago and turned down (discuss.kotlinlang.org/t/ternary-op...) but I still want it damn it! 😂

discuss.kotlinlang.orgTernary operatorReasons to not add the ternary operator: It doesn’t add any new functionality If-expressions work okay as they are now It can lead to unclear code when chaining ternaries It can lead to unclear code ...

Once you spend enough time looking at Java code in the IJ platform you realise that ternaries are an irresistible lure for unreadable code. I now fear ternaries. I've seen them nested three levels deep, with complex conditions and all the things they should never be paired with.

The problem is you can't enforce "sane" usage of these ternaries and so folks will inevitably abuse them, I fear. Sure, code reviews can catch this... Unless people get in a "I saved two characters here" frenzy and yeah, you end up with undecipherable nonsense

I would love a ternary operator, just like I would love better bitwise operators. Both would make this nicer: ((((data[index + 0] and 0xf) shl 4) or if (index + 1 < data.size) (data[index + 1] and 0xf) else 0xf) and 0xff).toByte() (not production code :)

The algo here just checks if the matching brackets or (nya,nyan) in this case are valid. I add an element to stack whenever an opening bracket is found and remove it when the closing bracket is found. This way the program will only throw error if the user program has mismatched brackets.

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.

I always wonder why it's always `let` and not `also`, which doesn't have that "returns null" problem. I try to use these scoping functions only if I do not need an else branch and I'm too lazy to declare a local variable to get smart casting work on immutable classes declared in other modules.

I feel some folks think that basic null checks are uncouth in idiomatic Kotlin. As a personal taste thing, I don't like 1 line if-else, but I'll put brackets in like your example all day long compared to adding an Elvis.

OMG I hate this. And people are like "this is idiomatic" kotlin. I used to link them to @jakewhartons tweet about it. But you should write a blog post about this so I can point people to it in code review when people tell me to use let. I already link them to your tweet regarding let, apply, etc