- 13 Apr 2022
- 5 min read
Best Kotlin features I value most as Java Developer
Features that makes me happier in my day-to-day coding challenges
I was working as Java Backend Developer for almost 5 years of my career and I am still a BackEnd Developer but last year I moved to Kotlin as a primary language for all my new projects.
Recently I was tasked to upgrade a legacy Java project and I found myself irritated that some Kotlin features are not there so here I am writing my top list of Kotlin features I like the most.
No more semicolons
I don't need to put semicolons at the end of the line anymore. Sounds like a very tiny thin but I get used to this very quickly. Even more, I see that non of the modern programming languages has it. Is there any reason why Java did not remove that with the latest JDK update?
Easier collection operations
With Kotlin I have nice wrappers to work with arrays. There are lots of list shortcuts functions that I use a lot.
Any place is good for my function
Java's best practices were always about good object design since it is an almost pure object-oriented language, except primitives. It means you always should think hard about where to put your functions, your "behavior" and try to not mix it with your "state". That almost always leads to classes named "Utils" with all the stuff devs don't know where to put to.
Likely for me Kotlin has an extension function so I can keep my functions in any place now and keep my code more readable. I always think that more readable code is better than more sophisticated. What is the point of smart code if only a few people can read it?
Here we declare ObjectFromOtherLibrary.prepare()
extension function outside of not controlled class. Latter we use the "with" statement to declare a scope instead of variable and call functions that are in the ObjectFromOtherLibrary
object.
Easy to keep everything immutable
One of the biggest challenges in every programming language is multithreading. Things become much more complex with scale and not linear code. Many multithreading issues in Java were coming from mutable objects and “race conditions” as a result of this.
Kotlin decided to overcome this with immutable design. It's really simple now to keep everything immutable in Kotlin and that is my strong advice to follow this best practice.
If statement can return value
Again, this is not a life changer but really I was trying to invent this myself in Java and I am really glad it is here right now.
Do more at the same time
I am talking about scope functions and here is my example of using this feature:
Here I am declaring the object conditionally with “takeIf” and then saving it at the same time when returning with the “also” keyword
In conclusion
Even though I have considerable Java experience and started programming with Kotlin only recently, I already like the concept of using Koltin to write more fun Java as developers should be happy about what they do. Right?