Discovering Kotlin

Iuliana Cosmina
Jul 25, 2018 · 4 min read

Some time ago, I realized that basically all my career is centered around Java, Java-based tools and Java frameworks. Sure, as a software engineer I’ve always believed that the particular programming language is not important; the logic and your problem solving skills matter more. But believing wasn't making it true for me, since all my projects, at work and at home, were in Java.

Enter Kotlin.

My interest in Kotlin was sparked the day it was announced by JetBrains. I've been using IntelliJ IDEA since 2006, and I’ve used PHP Storm to edit my blog from time to time. I've been fond of this company for many years because of the wonderful development tools they produce. So, clearly, Kotlin had to be another awesome product. But only after seeing Josh Long's "Bootiful Kotlin" presentation at the Spring IO conference in Barcelona, did I decide that this was a programming language I needed to learn.

So when my first training day at NCR came, I knew exactly what to do.

What I needed:

  • a good tutorial.
  • a computer — I have a Mac, this will do ;).
  • a little time — roughly 8 hours.
  • an idea — NCR's representative animal is the bear, so let's save bear instances to a database.

The Kickoff

I installed the Kotlin compiler. I didn’t really need it, since I was using IntelliJ IDEA, but I wanted to explore the command line as well. I created my new Kotlin project and wrote my first few lines of code:

And I compared it to the same code written in Java:

And for completeness, I compared it to the same code in Scala:

And concluded that the Kotlin version was definitely the most compact and readable.

After that, I spent about two hours reading about the Kotlin syntax, and was able to compile a list of things that impressed me (I am not so easy to impress, btw):

  • Default immutability.
  • Everything is by default not null, so no more NPEs.
  • Less "{}" and no more ";".
  • Simple syntax, no more getters, setters, no constructors needed.
  • No "new" operator.
  • Ranges.
  • Placeholders.
  • Inline functions.
  • Awesome type casting and type inference.
  • The when expression.

I am just scratching the surface, but I am already in love.

The code

After writing more code to do trivial things like traversing arrays and manipulating strings, I had enough confidence to write my application.

I wanted to start fast, so I used Spring Initializr to generate my project.

This gave me a functioning Gradle project with all the plugins I needed to get started.

The entry-point Application.kt file looked like this:

Next, I created an entity class in a file named Bear.kt:

No setters, getters, toString() or constructor needed. Pretty neat, huh?

And since I wanted to limit the species of bears to just a few, I created an enum. Behold the Species.kt file:

Next came some Spring Data JPA:

Boom! A repository. Look ma', no curly braces!

Then I could modify the Application class to create a few Bear instances and save them to the database before extracting them and printing them just to prove it all worked.

We run the application and our bears are created:

INFO 25701 — — [ main] com.td.ApplicationKt : Starting ApplicationKt on WGBIG185029–21L with PID 25701 (…)

INFO 25701 — — [ main] com.td.Application : Bear(name=Silverhair, species=BROWN, id=1)
INFO 25701 — — [ main] com.td.Application : Bear(name=Silverlocks, species=ASIATIC, id=2)
INFO 25701 — — [ main] com.td.Application : Bear(name=Goldenlocks, species=POLAR, id=3)
INFO 25701 — — [ main] com.td.Application : Bear(name=Goldilocks, species=POLAR, id=4)
INFO 25701 — — [ main] com.td.Application : All done.

Conclusion

I think Kotlin is the future for a lot of Java projects. Its readable and concise syntax will greatly reduce development time.

Also, since it can be easily integrated with Java code, the migration of a Java project can be done gradually while the project is still being shipped to the clients with little risk of failures.

NCR Edinburgh

Our thoughts on Software Engineering, Technology & Design…

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store