My journey from Java to Scala through Kotlin

Marharyta Nedzelska
8 min readMar 7, 2018

--

In this article I’ll tell you my own story about how I got acquainted with each of these languages and what did I learn from it. I don’t want to promote any of them but just to share my own impression and experience.

Who am I?

Today I am a Software Engineer @ Wix, I use Scala most of the time in my work. Before this, I worked as a Java Developer for several years. I also participate in our local community life and became a leader of Kyiv Kotlin user group. Thus I have some expertise in all of these languages and can share my ideas with you.

I started learning Java when I was studying in the university because I wanted to join a students’ startup and they decided to use Java. I knew C++ and C# so it wasn’t a big problem for me to learn Java syntax but my code was ugly. Then I learned more about conventions, I learned frameworks, got a serious job, then another one… And at some moment I caught myself that I am not so excited about Java as it was before. I was really annoyed by all these getters, setters, and all boilerplate code… You can tell me “use Lombok”. Ok! So what about stupid casting when you’re inside “instanceOf” checks? What about using variables directly in the string? I want default parameters! What about all cool features that all modern languages have? I want Java with blackjack and harlots!

To feel happier I wanted to learn something new and switch to a different programming language. My first impression about Scala was “What the hell is it???” I understood nothing and decided to stay with my beloved Java.

A year after I found out that Kotlin language appeared. It was advertised everywhere. All conferences, tech blogs, tech speeches, podcasts etc. All of them advertised Kotlin a lot. I went through it and it looked like Scala for me with big ambitions. They will substitute Java! Ha-ha! Really? I thought it doesn’t worth my attention and continued generating getters and setters…

My article could have ended at this point or even never appeared at all if I had fewer ambitions and not such good friend. I was helping my friend, JUG UA leader in his Java community affairs and we argued that I can learn Kotlin pretty well in some months. To win I had to prepare a talk about Kotlin with live coding for our local community. It was a big challenge for me. But I like challenges! So my preparation started.

I read the documentation. Thank God, It was not very big! Then I passed all levels of Kotlin Koans and made tweets about it (It’s important :) ) I wrote some test projects, implement a simple algorithm, wrote small web apps with Spring Boot. Of course, I used TDD approach and tested everything. And I was extremely pleased by using val/var instead of writing getters and setters, by having data class just with keyword “data”, accessing variables directly in string literals, smart casts, nullability, extension functions, inline lambdas etc…

But it still was not enough for the talk. I knew that too many people will ask me different even strange questions during meetup and I started dig into Kotlin, found some puzzlers, read a book “Kotlin in Action” by Dmitry Jemerov and Svetlana Isakova, analyzed decompiled code… I learned how nullability feature works, how generics are implemented, lambdas, extension functions, coroutines… And once I found myself fall in love with Kotlin! This is an amazing language, so simple, so clear! Finally, I prepared my talk. After that people started finding me and asking for help in Kotlin in social networks, messengers etc. At some point, I understood that it is extremely difficult to be productive at work and spend lots of time helping others. The solution was obvious! My friend and I organized Kyiv Kotlin User group and the community started helping each other. And I just manage a bit this process.

Scala! How did I get there?

Becoming a bit popular in Kyiv Java world I obtained many work proposals every day. But I wanted to find a dream job, so I skipped them a lot. But finally, I found it. I liked that company and what they do. I thought I can be there as free as I always desired. The only problem was that they write in Scala. So I need to learn it. But I remember my first impression and was afraid of the language. On the other hand, I knew that I would never forgive myself if I lose this opportunity. So I decided to try…

One of the tasks during my interviews was writing a program. Before starting I asked the instructor if I can write it in any language I want and he answered yes but he should be able to understand it. So Kotlin was OK for both of us and I started working on it. I really like the code I wrote I used many features of the language and felt how simple and readable it is. So if you are trying to get your dream job which language will you choose for test task: the one you have been working for years or the one you’ve just learned? So being as crazy as I am you’ll probably choose the second one!

Finally, I got this job and started learning Scala. I was really pleased how simple it is after Kotlin because they are very similar. Apart from my deep love to Kotlin I really like functional approach in Scala. The cutest thing in this language was how close it is to math and how smart it is. And I feel no regrets that today I am writing in Scala.

Now let’s have a look at some features of these languages I like/dislike:

1. In Kotlin we have data classes in Scala — case classes. They look similar. The difference is when you omit var/val before field in Scala you get val by default. In Kotlin if you forget var/val you just get a constructor’s argument instead of a property. In Java, we just can have Lombok and I guess in this battle it loses.

case class Person (name: String, age: Int) != data class Person (name: String, age: Int)

2. In Kotlin you can access variables directly in String. In Scala, you can do it too but with some overhead like writing ‘s’ at the beginning:

val greeting = “Hello, $name!” VS val greeting = s“Hello, $name!”

3. I like nullability feature in Kotlin. It works well when you are forced to check nullability of types during compilation. On the other hand. Smart casts are not always good enough to understand that type of a variable is not nullable so you need to write some additional things. Good news! Language becomes better all the time and smart casts are also becoming smarter.

val person: Person? = null // OK

val person: Person = null // Compile error

For me, it looks better than Optional in Java and Scala

4. The thing I really enjoy in Scala is pattern matching which is not so powerful in Kotlin with the keyword “when”. In Scala, you can use any object with an unapply method in a match. In Kotlin when functionality is reduced. So you can’t write this:

response match {

case AllSuccessful() => Success()

case NotFound() => Failure(new NotFoundException(response, request))

case Undeliverable() => Failure(new UndeliverableException(response, request))

case _ => Failure(new SendException(response, request))

}

where AllSuccessful, NotFound, Undeliverable — objects with the unapply method and a custom logic in that method.

5. Both languages are very powerful in working with lambdas and it’s just the question of taste what would you prefer:

val sumOfSquares = list.map{it * it}.fold(0) {x, y -> x + y} or

val sumOfSquares = list.map{i => i * i}.fold(0) {_+_}

There are so many cool features like operators overloading, implicits, monads etc in Scala and like extension functions, destructuring, inline/noinline/crossinline, reified generics and so on. I listed just a couple of things.

You may think that these small things are the only differences between these languages. Actually, It’s not true. Being so close to each other in syntax they have extremely different ideologies. Let’s have a look:

  1. The first thing I would like to tell you is that Kotlin seems to be just a “better Java” while Scala is something much more powerful. On the one hand, you have plenty of opportunities, in Scala, you can overload all operators you want (even create your own), you can describe your own String preprocessor and do whatever you want. Sounds cool! On the other hand, more power means more responsibility! It’s so easy to write in Scala really unmaintainable code! Sometimes you may desire other devs just doesn’t know about some features. Kotlin doesn’t have these features. In my opinion, it is not so easy to write ugly code in Kotlin because you do not have too many instruments.
  2. As a result of the previous paragraph, Scala is a really difficult language to start with. Thus there are not so many Scala developers (comparing to Java). While Kotlin is pretty easy. You can start writing code in Kotlin in very short terms. I know a lot of people who started writing in Kotlin after С#, Go, Python, Swift even without knowing Java.
  3. One more thing I would like to tell you about is tooling. I know that both Scala and Kotlin tooling are not as good as for Java. But we should take into consideration that Kotlin is developed by JetBrains, the company, which also makes IDEs, plugins and other tools for developers. So it seems that Kotlin tooling has a great potential to become even better than one for Java. At the same time, Scala tooling is supported mostly by the community that leads to its slow development.
  4. As you know, apart from being compiled to bytecode, Kotlin could be compiled to javascript or to native code. I heard about same opportunities in Scala but I don’t know if someone really uses it. For Kotlin I would like to add that Spring supports configuring in Kotlin and you can write Gradle build script using Kotlin Dsl. Google also supports Kotlin being one of the langs for Android. And this really increases its popularity.
  5. By the way, I think there are not so many cool libs or framework written in Kotlin. But we have powerful Play framework and Apache Spark written in Scala with awesome Scala API.

To sum up I would like to say that I like all these there languages all of them are good for some purpose. I just want you to know that every language has its pros and cons and you should decide what is more important to you when you start your project. To my mind, if you just want to write code using “better Java” without boilerplate and verbosity Kotlin is for you. Easy and pragmatic! And if you want to try something more powerful, with a real functional approach, with freedom of making whatever you want — use Scala!

So if you are fed up with Java and it’s boilerplate you have many alternatives inside JVM world as well as outside of it.

Hope this article was useful to you!

--

--