Kotlin is a new programming language from JetBrains, the maker of the world’s best IDEs. In this article let us see why Kotlin is good. Then we will discuss some of the problems and hiccups you may encounter if you start using it today.


Also See: SET THE FUTURE WITH MANUFACTURING TRANSFORMATION

Why Kotlin is good

Kotlin compiles to JVM bytecode or JavaScript. It is not a language you will write a kernel in. Kotlin comes from industry, not academia. It solves problems faced by working programmers today. As an example, the type system helps you avoid null pointer exceptions. Research languages tend to just not have null at all, but this is of no use to people working with large codebases and APIs which do.

Kotlin costs nothing to adopt! It’s open source, but that’s not what I mean here. What it mean is there’s a high quality, one-click Java to Kotlin converter tool, and a strong focus on Java binary compatibility. You can convert an existing Java project one file at a time and everything will still compile, even for complex programs that run to millions of lines of code

As an obvious implication of the above, Kotlin programs can use all existing Java frameworks and libraries, even advanced frameworks that rely on annotation processing. It is approachable and it can be learned in a few hours by simply reading the language reference. The syntax is lean and intuitive. The language balances terseness and readability well.

Kotlin allows you to keep using your productivity enhancing tools. If you use IntelliJ, IDE interop is entirely seamless: code can be refactored, searched, navigated and auto completed as if the Kotlin code was Java and vice-versa. Kotlin is highly suitable for enterprise Java shops. If you spend all day working on big Java codebases at even bigger companies, you should investigate Kotlin because:

  • Adopting Kotlin is low risk
  • It can be trialled in a small part of your code base by one or two enthusiastic team members without disrupting the rest of your project
  • Kotlin classes export a Java API that looks identical to that of regular Java code.
  • Focuses on readable syntax, code reviews are not a problem
  • They can still be done by team members who aren’t familiar with the language.

Features

Kotlin stands out in a sea of new programming languages because of its focus on the ecosystem: Despite that, Kotlin has many useful features that make writing code in it pleasant:

  • Null safety (optionality), that lets the compiler systematically flag potential null pointer dereferences. Unlike some languages, this does not involve an option type and is therefore zero-overhead
  • Lean syntax: type inference works everywhere, one liner functions take one line, simple structs/JavaBeans can also be declared in one line.
  • Distinguishes: Functional programming support with zero-overhead lambdas and ability to do mapping, folding etc over standard Java collections. The Kotlin type system distinguishes between mutable and immutable views over collections.
  • Extension functions: Let you add methods to classes without modifying their source code. This looks at first like a superficial bit of syntax sugar to avoid FooUtils style classes. Then you realise that doing it this way enables you to easily discover the new methods via auto-completion.
  • Operator overloading: Operators map to special method names, so can override the behaviour of the existing operators but you cannot define entirely new ones. This strikes a balance between power and readability.
  • Markdown instead of HTML for your API docs: This makes writing JavaDocs much more pleasant. The “Dokka” tool, which is the equivalent of JavaDoc, can read both Kotlin and Java source code and generate combined doc websites, both with its own style and also in the standard JavaDoc HTML style.
  • Better generics: If you never fully got to grips with what exactly super and extends mean when put inside a type variable, don’t worry: it’s not you. Java’s generics really are just confusing. Kotlin fixes it. Delegation (forwarding methods) can be done automatically.
  • The == operator does what you actually expect. Would you like fast and convenient async programming? Of course you would.
  • String interpolation “works like ${this.example}!”
  • Function arguments can be named, optional and variadic.

Challenges whilst using the language

The data class feature is a very useful way to auto-generate JavaBean boilerplate, but it imposes limitations as well.

  • There are no type aliases yet. So, function types have to be written out redundantly each and every time.
  • By default, classes are final. You have to mark them as “open” if you want the standard Java behaviour. Some Java frameworks that rely on bytecode synthesis assume classes won’t be final and can fail or silently do the wrong thing when encountering Kotlin code.
  • Kotlin targets Java 6 bytecode. It does not use some of the improvements in Java 8.
  • The community is small. Whilst the excellent Java interop means you don’t really need Kotlin libraries, they’re still nice to have and currently there aren’t many.
  • There is no standard style guide, and at points Kotlin gives you several alternative syntaxes to choose from. Kotlin written by different people may look different. This is in contrast to Go, which enforces style rigidly.
  • Kotlin is pickier about some things than Java. It will not auto convert integers to longs and such, you are required to specify you want the conversion explicitly.
  • Because Kotlin targets Java 6 it is restricted to the features that runtime has. Whilst it can match or exceed C# in many areas, it lacks features like value types which aren’t yet a part of the Java platform.

Try it now!

Like many modern languages, Kotlin has a way to try it out via your web browser. Unlike those other languages, Kotlin’s tryout site is practically a full blown IDE that features fast autocompletion, real time background compilation and even online static analysis!