mark cerqueira well-rounded nerd

Kotlin Kills the Arrow Anti-Pattern

I read a great article on the Arrow Anti-Pattern the other day. Shortly thereafter on the same day I also thought about Kotlin and boom! I realized something I’ve known all along: Kotlin kills the Arrow Anti-Pattern!

Consider this Java method:

The anti-pattern here is the series of if checks (for null) that make this code look like an arrow. With Kotlin’s safe call operator (?) and Elvis operator (?:) we can write that method in a single line!

Boom! The arrow is dead! While checking nullness is not the only case arrows can emerge, null checking is a pervasive painpoint, and common source of the arrow anti-pattern, in Java so these operators make our lives much better.