Kotlin: Statically typed programming language for the JVM, Android and browser

Hello! I’m writing these series of articles as I learn the language – My plan is to cover some aspects like: what, why, pros, cons, highlights on the language reference, functions / lambdas and some practical examples using spring boot.

So lets start from the basic in this first post (sorry, no code at this time).

What is it?

Kotlin is a statically-typed language that runs on the JVM and also can be compiled to JavaScript (haven’t tried. have no glue). Developed by JetBrains in Saint Petersburg, Russia (name from the Kotlin Island) was nominated as the language of the month in January 2012 by Dr. Dobb’s Journal. It  is designed to interoperate with Java and is reliant on the existing Java Class Library, such as Collections (Kotlin does not have its own collections package).

My opinion so far is that the syntax is more expressive and concise than Java (aka less boilerplate) and reminds me Groovy good parts however typed. As a JVM based language, the benefit is that you can have Java and Kotlin coexist in the same codebase while still getting some improvements by writing Kotlin. You also get the benefits of better functional programming without the complexities of Scala.

Why Kotlin?

Being closely associated with some of the design goals that JetBrains chose I’d start with:

  • Interoperable with Java ->
    • compiled to JVM bytecode can easily be used where Java is running today. Perhaps it takes this to the next level, like: you can call Kotlin code from Java since all the heavy lifting is done by Kotlin;
    • calling Java libraries and frameworks in Kotlin – invoke methods, extend and implement, apply annotations, etc. Actually I would say here to use Java frameworks rather than Kotlin frameworks. Why? maturity of Java frameworks;
    • There is virtually no run-time overhead (it consists mostly of focused extensions to the Java standard lib) and even the functional constructs are optimized at compile-time with in-lining;
    • It lowers the barrier of adoption. It’s bound to attract people working with Java as a more expressive and less verbose language;
  • Pragmatic ->
    It is a pragmatic language adopting ideas and traits that have proven to be successful over the years in existing languages. It does not enforce any particular programming paradigm like predominantly object-oriented or functional, which coupled with its lean and readable syntax making the learning curve better;
  • Safe ->
    It aims to achieve a higher level of safety being a statically typed language on the JVM. Exploring further, there are a couple of interesting ideas that Kotlin has introduced: Null Safety and the ‘is’ operator

    • Null Safety: provided by the language’s type system is a great step eliminating NullPointerException. Variables and properties cannot be null in Kotlin unless explicitly allowed;
    • Class casts: lead to a ClassCastException,Kotlin makes it easier by combining the check and the cast in a single operation with the is operator;
  • Concise and Expressive ->
    JetBrains defines concise as: “one that can clearly express the intent of the code with its syntax and does not obscure it with boilerplate required to accomplish the intent”. I agree.

Kotlin is great, but…

Some cons I’ve seen so far…

  • No namespaces -> allows you to define functions and properties at the top level of your files. Cool! but it can cause some confusion when combined with the fact that all top level declarations are referenced unqualified;

  • No static modifiers -> it has unusual handling of static function and property declarations when are called from Java code. It’s not bad, but sounds weird to me;
  • Method count -> will reduce the number of lines of code in your project. But it will also probably increase, specially concern if you are using Android, the method count of the compiled code (“Every public val causes Kotlin to generate both a backing field and a getter function that can be called from Java. Every public var will cause both a getter and setter to be generated“);

  • All classes and functions in Kotlin are closed by default (C++ style / can be good as well… functional styled);
  • Its new, there is no a big use cases yet – mainly Android Apps;
  • If you are functional purist, Kotlin is not for you.

Scala X Kotlin

There are a long discussion about why not Scala? In my opinion both languages are great choices. Basically to me is a new language to learn and so far I fell it is much easier to learn/use than Scala and it’s Java interoperability is indeed a very good point. There is a trade off (as usual) and I’ll give up (now) some of power of Scala for the simplicity of Kotlin.

Also as a Pivotal Lab’s consultant I am testing some JVM languages on Spring’s land which can help me to deliver business value quickly – I’ve been using some Groovy + Spring Boot and a lot of test with Spock. Now its time to try Kotlin + Spring (Scala is not supported yet).

Next step

Next post I will highlight some interesting points from Kotlin’s reference.

Some additional resources

Conclusion

Kotlin is overall a great language. It is much less verbose than Java and has an excellent standard library that drastically reduce the amount of boilerplate code you need to write. Have you tried it?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s