Best Kotlin features that I value most as Java Developer

Vlad Mykol
4 min readMar 9, 2022

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

Photo made by the author

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 17 update?

Java code with semicolons

I have nice wrappers to work with arrays

There are lots of list shortcuts functions that I use a lot

Kotlin list wrappers

Put your functions in any place

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?

Kotlin snippet

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

It’s about Scope functions https://kotlinlang.org/docs/scope-functions.html

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

Did I miss anything? Want some more?

You may clap a couple of times (👏++) to encourage my future writings directly and I will be super thankful for that. I wish you stressless codding irrespective of what language or technology you use!

--

--