Why you should love Kotlin

December 4, 2017

Computer screen with text
Sharing is caring:

For a long time, I thought of Kotlin as the new kid at school that everyone wanted to be friends with – except me. Until an unrelated set of tweets and blog posts changed my mind. First, there was a random tweet, “Kotlin/Native now supports accessing Objective-C APIs on iOS.”  Then I read Paul Krill’s post “Beta JetBrains IDE moves Kotlin apps out of the JVM.” This blew my mind, I could create iOS apps without having to learn Swift. Lastly, I learned that a recent survey conducted by Realm indicated that Kotlin is positioned to overtake Android/Java by December 2018. I wanted- needed to know more about it. After a bit of research, here are my findings what is it, what makes it superior to Java, and where to learn more.

 

There are three standout reasons that Kotlin is taking the developer community by storm. To begin with it uses Java, the second most powerful language and popular language. By building off Java, it takes advantage of the class libraries and the Java Virtual Machine. As a sweetener, it is a simplified and expressive language that interoperates with Java, which means developers can switch over by simply reducing code not starting from scratch. In addition, Kotlin address Java’s weak error handling while improving “safety features for nullability and immutability.” Next question where did this new simplified and expressive language originate?

 

Reminiscent of Python and Ruby’s creation developers were looking for an improved language. Developers at JetBrains, a Russian company and the makers of IntelliJ IDEA, were frustrated with Java’s limitations. They considered Scala, but the compilation time was a problem. So, they began working on their own language, Project Kotlin, in 2010. It’s debut occurred at the JVM Language Summit in July of 2011. Two years later in February 2012 JetBrains open sourced it under an Apache 2 license. Four years later, they released v1.0. Then in May of 2017 at Google I/O it was announced that it was an official language of Android. As such, Kotlin has the full backing of Google resources.  But how is it superior to Android?

 

Kotlin improves the developer experience from nullpointer exception to cross platform usage. While researching, I came across several posts that detail the differences between it and Android/Java. My favorite post was written by Magnus Vinther over at Medium which provides 17 detail differences between Kotlin and Java.

Nullpointer

var neverNull: String = "This can't be null"            // 1

var nullable: String? = "You can keep a null here"      // 2

nullable = null                                         // 3

var inferredNonNull = "The compiler assumes non-null"   // 4

example code originates: Kotlin By Example

Shorter code

package org.kotlin.byexample        // 1

fun main(args: Array<String>) {     // 2
    println("Hello, World!")        // 3
}

example code originates: Kotlin by Example

Improvement to FindViewByID

fun <T : View> Activity.bind(@IdRes idRes: Int): Lazy<T> {
    @Suppress("UNCHECKED_CAST")
    return unsafeLazy { findViewById(idRes) as T }
}

fun <T : View> View.bind(@IdRes idRes: Int): Lazy<T> {
    @Suppress("UNCHECKED_CAST")
    return unsafeLazy { findViewById(idRes) as T }
}

example code originates: Improving FindViewById with Kotlin

As a junior developer nullpointer handling, shorter code, and an improved FindViewByID resonate with me. However, I understand that three examples might not be enough to convince you to try Kotlin, so checkout out Vinther’s “Why You Should Totally Switch to Kotlin!”.

Now that we have covered the why, how, and provided some examples you are probably curious to learn about Kotlin and how to use it.  The following list of resources for it is not exhaustive instead I suggest it as a starting point. Every week additional resources become available from blogs, repos, books, videos, to online courses.

Kotlin Info

https://github.com/JetBrains/kotlin

https://developer.android.com/kotlin/index.html

https://kotlinlang.org/docs/reference/idioms.html

https://www.infoworld.com/search?query=kotlin&contentType=article%2Cresource

https://github.com/JetBrains/kotlin

Kotlin Courses

https://kotlinlang.org/docs/tutorials/

https://www.udemy.com/kotlin-course/

https://teamtreehouse.com/library/kotlin-for-java-developers

https://www.lynda.com/Kotlin-tutorials/650172-0.html

https://www.udacity.com/course/kotlin-for-android-developers–ud888

https://try.kotlinlang.org/#/Kotlin%20Koans/Introduction/Hello,%20world!/Task.kt

 

Works Cited

Google. (2017, May 01). Kotlin and Android. Retrieved from Developer: https://developer.android.com/kotlin/index.html

Krill, P. (2011, July 22). JetBrains readies JVM-based language. Retrieved from InfoWorld: https://www.infoworld.com/article/2622405/java/jetbrains-readies-jvm-based-language.html

Krill, P. (2011, July 22). JetBrains readies JVM-based language. Retrieved from InfoWorld: https://www.infoworld.com/article/2622405/java/jetbrains-readies-jvm-based-language.html

Krill, P. (2017, November 6). Beta JetBrains IDE moves Kotlin apps out of the JVM. Retrieved from InfoWorld: https://www.infoworld.com/article/3236199/java/beta-jetbrains-ide-moves-kotlin-apps-out-of-the-jvm.html

Krill, P. (2017, October 10). InfoWorld. Retrieved from Kotlin could overtake Java on Android next year : https://www.infoworld.com/article/3230301/mobile-development/kotlin-could-overtake-java-on-android-next-year.html

Wikipedia. (2017, November 29). Kotlin. Retrieved from Wikipedia: https://en.wikipedia.org/wiki/Kotlin_(programming_language)

 

 

 

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.