Why you should adapt Kotlin now

“Code on a computer” by Markus Spiske on Unsplash

I have been working on Java for at least 7 years now. It is still my first language of preference. It is a very old and stable language with a large ecosystem around it. The stability of ecosystem and mature libraries allows us to write production ready application with confidence. However, it lacks many features that the modern programming languages like Ruby, C#, Python and others offer.

Even though I prefer verbosity in my code, I have always been annoyed by the boilerplate code that Java projects require. Since past year and half I have been working with C# at work and I have absolutely fallen in love with the syntax it offers (specially LINQ).

I recently got introduced to Kotlin while watching a YouTube Video from Google I/O ’17 and got immediately sold on it. Following are some of the main reasons why I will be adapting Kotlin.

Kotlin is more concise

// Java Person
public final class Person {
private String name;

public Person(String name) {
this.name = name
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() { /* Implement this */ }

@Override
public boolean equals(Object o) { /* Implement this */ }

@Override
public int hashCode() { /* Implement this */ }
}

// Kotlin Person
data class Person(var name: String)

Larger the number of lines of boilerplate code, greater the chance of introducing bugs and maintenance overhead. Compilers are good at finding common errors than humans. We should let it do the job while we focus on creative tasks and better architecture of the software.

NULL safety

Person person = null; 
person.getName(); // throws NullPointer Exception

In Kotlin, we can avoid such errors as the following code will throw compilation error.

val person : Person; 
person = null; // compilation error

All objects are not nullable by default. We can override this by changing its type to be nullable by using ? operator.

val person : Person? = null; // this is legal

We should never make an object nullable unless we have a very good reason to do so. We can prevent null pointer exception on nullable objects by performing good old null checks.

val person : Person? = null;

if (null != person) {
val name = person.getName();
}

Java interoperability

// Person.java
class Person {
private String name;

public String getName() {
return name;
}
}

// Person.kt
fun doSomething() {
// using Person from Java code
val person = Person();

// we can call Java getter as attributes
val name = person.name;
}

Easy to adapt

Full stack development

This is not a brand new idea and NodeJS has been successfully selling this idea of same code base in server and back-end. It uses JavaScript or TypeScript to write code that runs on server or browser. You can use tools like NativeScript to then build iPhone or Android applications from the same code base. This is great but I never got sold to this idea. I have been working with JavaScript for over 10 years, I think its a great language. But its lack of type safety will create lots of headache in maintaining large code base. It is easier to introduce bugs in the JavaScript code.

Using Kotlin you can achieve the benefits of one language for full stack development. Kotlin strongly typed and its modern feature allows us to focus more on writing great applications while minimizing bugs in the code.

Officially supported by Google

Kotlin bleands seamlessly with Jetbranins set of tools and IDEs and makes it very easy to adapt. Jetbrains have heavily invested on Kotlin and they will continue to do so. Kotlin is here to stay!!

Conclusion

Originally published at subash.com.au on February 17, 2018.

Lead Software Engineer at A Cloud Guru