Kotlin or Scala? Scala or Kotlin? The two contenders for the crown of the JVM Kingdoms and the title of “Better Java” each bring something unique to the fight. But which should be the next ruler of your code?
Java is old. Not that there’s anything wrong with being old. Sometimes it can be a good thing. Math is old and we still use it. As the saying goes – if it ain’t broke don’t fix it. But that doesn’t mean we shouldn’t find ways to improve upon it. Java that is.
That is exactly what both Scala and Kotlin aim to be – a better, newer Java. It’s safe to say both have been successful in their aim, but each adds and expands on the capabilities of Java in its own way.
Before we discuss the use-cases and criteria upon which to select Kotlin or Scala for your next project, let’s get acquainted with the history, and the major strengths and weaknesses of each.
Introduced to the world over 15 years ago in 2004, Scala was designed to address the flaws and common development issues of Java. Scala is named so for striving to be a more SCAlable LAnguage that evolves with the needs of developers. It dubs itself as a general-purpose programming language providing support for functional programming and a strong static type system.
As it was developed in the academia, Scala didn’t only serve as a proof-of-concept for a major static language on the JVM (which was pretty major at the time), but also offered a playground for different paradigms and ideas on functional programming.
Being around for 15 years made Scala a familiar face in the JVM landscape. Many developers either work with it or have worked with it, either on their own apps or those of others. This means more resources, more tutorials, and more (useful) answers on StackOverflow and Reddit.
Pattern Matching & Big Math
Calling itself “Java’s switch on steroids”, Scala’s match statement lets you match any type of data, including your own types. It also offers full support for macros and higher-kinded types, making Scala ideal for big data processing tasks.
Scala was designed to successfully marry functional and OOP paradigms, and it does so fairly well. Delivering on the promise of “write once, run anywhere” of JVM, and combining it with a functional style of coding, Scala lets you make the best of both worlds in your code.
Slow Compilation Speed
“Scala has the right features, but its most obvious deficiency is very slow compilation.” These words were said by no other than the lead developer of Kotlin. And he’s not the only one. When it comes to complex enterprise-grade programs, it can take Scala long minutes to compile what Java and Kotlin can handle in mere seconds.
Inferior Java Compatibility
If you’re planning to go back and forth between legacy Java code and advanced Scala code, you might come across some trouble. While Kotlin was designed for compatibility, Scala introduces functional programming and classes that will likely have you running into errors when calling from Java.
Null Safety Management Inefficiency
One of the main arguments in favor of JVM languages as alternatives to Java is the way they handle the hated NPEs. When compared to Kotlin, many find Scala’s approach inefficient and clunky. To deal with NPEs, Scala replaces null values with Option, which adds some complexity to the code as it needs to be explicitly used.
Developed by JetBrains and released to the world as open source in 2012, Kotlin quickly became one of the fastest growing languages on GitHub. Drawing heavily from Scala and aiming to solve practical problems, Kotlin brought great compilation times and perfect interoperability with Java.
Kotlin quickly attracted many tech giants who encorporated Kotlin into their stack including Google, Square, Pinterest, and Atlassian. The popularity of the young language soared when it was announced by Google as an officially adopted language for Android development.
Say what you will about the power of collaboration in academia, it can’t compare to the strength of corporate support. With Google and JetBrains (who brought you Intellij IDE) supporting the Kotlin ecosystem, there are undeniable advantages.
Superior Java Interoperability
As I’ve mentioned in the introduction, one of the main strengths of Kotlin is its perfect compatibility with Java code. You can call Kotlin code from Java and vice versa with no errors or issues.
One of the pain points of Java that Kotlin addresses is boilerplate code. In Kotlin there’s simply less of it. With less code lines, there is less room for bugs and also makes the code written easier to read.
Inferior Pattern Matching
Pattern matching is not fully supported in Kotlin, and is clearly inferior to Scala in that respect. While it is possible to achieve similar results with smart application of the when clause, Scala’s matching capabilities and ease of use are far ahead.
While it is growing quickly, the Kotlin community still has some catching up to do with Scala. Though Google’s support of Kotlin as an official language for Android significantly boosted its popularity, a newer community may mean less tutorials, tools and experts to lend a hand when needed.
Kotlin is king when it comes to Android development. Its whole ecosystem is geared toward Android apps. But what about other applications? Not as great. Outside the Android world, Kotlin is still not as useful or applicable as Scala.
What most impacts your choice, at the end of the day, is the intended use of the language in the specific project (or projects) you’ll be using it for. Given the strengths and weaknesses of each, there are specific cases in which one shines over the other.
Scala for Big Data & Mixed Coding Paradigms
If you’re looking to work with big data and big math, Scala is your obvious choice. With features neither Java nor Kotlin possess (like pattern matching, macros, and higher-kinded types), and perfect compatibility with Apache Spark, Scala is the language for data science and complex mathematical modeling.In addition, Scala lets you mix OOP and FP paradigms in your code. If your project demands this unique approach, Scala may serve you better than Kotlin or Java.
Kotlin for Android
If your project is on Android, then there’s really no question here: Kotlin is your language. Not only because it’s officially endorsed by Google, but also because it has all the tools needed for development and debugging of Android applications. In addition, Kotlin is built into Android Studio (as of version 3.0), so there isn’t even any additional installation or configuration needed before you can start coding in Kotlin.
Good old Java is still both – good and old. The new kids on the block each offer improvements and expansions upon it, dealing differently with familiar issues and pain points of Java development. As always, the trick is in finding the right tool for the job – a language that is equally comfortable for you, and compatible with the goals of the project. Which is yours?
(This post was written by yours truly and originally published on the Codota blog - Kotlin vs Scala: which is right for you?)