I guess many of us love Elvis, both the artist and the operator in Kotlin. But it can lead to some hidden pitfalls if you are not aware of how it works.
I only realized recently when Vladimir Zdravkovic put some code on twitter and ask us to guess what it’s printing:
Writing your classes with a good API is hard but important. As the writer is trying to make it easy for the user, we sometimes tend to repeat ourselves by adding convenient methods.
Think about the
List interface in Java. To check if there are no elements in the list we could check
list.getLength() == 0 or we simply ask for
The 2nd one reads much better. But it also adds a duplication and implicit connection between the two: If the list is empty, it can’t contain any elements! This must be respected by every implementer of the interface!
We can easily think of many other methods with implicit dependencies. …
How tools made us forget how to write sane constructors
In an ideal world, developers get smarter every day. The code we write this year should be better than the code we wrote 10 years ago, which in turn should be better than the code 20 years ago. Today we have better tools, more modern languages, and better practices.
But as often in life, we realize we are not living in that ideal world. …