This article covers the technical reasons behind why our first project using the Kotlin language, an Android app for the YMCA of Cambridge, Kitchener, and Waterloo, won’t be our last. It was written with input from Kanoa Haley and Ethan Xue.
The YMCA of Cambridge, Kitchener, and Waterloo runs children’s overnight camps throughout the summer. The parents and guardians of the campers want to have visibility into what their children are doing, while maintaining enough distance to let the children have fun and grow independently. Zeitspace was hired to design and develop an iOS app, Overnight Camp, that provided on-demand access to updates such as photos of their children, bus schedules changes, weather reports, and blogs from camp staff.
Overnight Camp App
Shortly after completing the iOS app, we were asked to develop an Android version of Overnight Camper. There are many options engineers have to choose from in developing a mobile app these days (web app, native, hybrid, etc…) with trade-offs for each.
We wanted to give our users an extremely responsive, fast, and comfortable experience that embraced the Android platform, so we determined we need to develop a native application. If you’re familiar with the Android ecosystem, you’ll know that this was just the first of many choices facing developers. There are countless libraries, languages, and design patterns that we also needed to pick.
We chose a relatively new language, Kotlin, developed by JetBrains, makers of IntelliJ and contributors to Android Studio. In May, at I/O 2017, Google made Kotlin a fully supported Android language. Kotlin first made an appearance on the Android platform back in 2011 after JetBrains, decided they wanted to create a superior language and developer experience to Java. The design goals were to mix-in with Java and add modern language features while being more concise and understandable than Java without sacrificing readability, performance or security. No small feat.
The result is that JetBrains created a new language that is a pleasure to work with. It isn’t as specialized as other languages that also run on the JVM, but it provides an easy and intuitive alternative to Java and makes your Android development a little more fun.
During the time developing the YMCA Android app from the ground up, our developers found that they quickly got up to speed on the language and grew to truly appreciate Kotlin. Kanoa is an Java developer while Ethan is a Swift/iOS developer but who was new to Android. The following are the features of Kotlin that really stood out for them.
Kotlin can seamlessly be mixed into a Java project and vice-versa. Libraries, code, classes, and everything else is entirely available to Kotlin and like-wise from Kotlin to Java. This really de-risked our Kotlin choice because we could always fallback to Java. For example, we were able to create our JSON model POJOs in Kotlin and apply Jackson Java Annotations to the Kotlin classes to facilitate perfect JSON generation and parsing. It was incredible how simply and seamlessly our classes all worked together regardless of language. Obviously this is currently Kotlin’s strongest feature and the reason we chose Kotlin so easily. This feature also allows you to take a huge, even monolithic, Java application and slowly convert it to Kotlin at your leisure, one class at a time.
Data Class and Accessor Methods
In Kotlin, you can have a regular class just like Java, but you can also have a “Data Class”. A Data Class allows you to declare your class fields in your constructor and Kotlin will automatically generate fields, getters, setters, .equals, .copy, .hashCode and other common methods. This allowed us to skip over a huge amount of boilerplate and create our models in a single line of code. This freed us up to only write methods that focussed on the app logic. This is a simple feature but highlights how Kotlin strives to fix and improve upon Java.
With the rise of functional features and functional programming becoming more wide-spread, JetBrains decided to enable developers to take advantage of functional programming in Kotlin. Most of these are simple features and not all are entirely implemented just yet (even immutable fields can still be mutated if you really try to get around it), but they provide some simple ways to deal with previously annoying problems. Kotlin Arrays all support the typical map, reduce, flatten, filter, etc… which is wonderful for making our code more concise and less error-prone.
Nullability (AKA Optionals)
Another wonderfully simple but great feature of Kotlin is its way to get rid of the pesky NullPointerException . Null is no longer a common type to assign to variables since a regular type can’t in Kotlin can’t be null. You have to deliberately create a String? instead of a String if you want to use null. This minimizes the potential for the dreaded billion-dollar mistake.
Extension Methods & Properties
Extensions are fairly common these days, but none-the-less still great to have and very much welcomed when developing on Android. You can add new methods to any already created class that can be used in the exact same manner as the original methods. This allowed us to extend upon the Android APIs whenever we needed to without the need to first inherit from the class.
Another key feature we liked is functions as first-class objects. Kotlin supports functions the same way as any other object as well as lambdas (with wonderful scoping tricks). This allowed use to make use of many little features such as promises and passing listeners directly. This isn’t new or novel in a language, but since it is still uncommon to see in Java, it was a breath of fresh air to be able to treat functions as anything else in Kotlin.
Kotlin is a modern, simple, efficient and easy to learn language that is 100% interoperable with Java. It gives you more concise and less error-prone code plus it’s a joy to use. And readable, maintainable code is important to us here at Zeitspace and even more so for our clients. As a result, we’re big fans and plan to Kotlin on future Android work. We definitely think it’s worth taking a close look at Kotlin for your projects.