Notes: What can Kotlin do for me?

 

Below are a few of, what I thought were, the interesting tidbits from Hadi Hariri’s GDD Europe ’17 talk entitled “What can Kotlin do for me?”

it’s kind of like javascript … in a good way

Misc

use val by default instead of var

“work towards having read only variables”

easily create a list of numbers within a rang
val numbers = 1..100

asSequence() to create a Sequence from an Iterable  See Doc

 

Functions

what we are trying to do is keep functions as simple as possible…

Characteristics of Kotlin functions

  • functions don’t have to exist within a class
  • they can exist outside of a class as “top-level” functions
  • these top-level functions can then be called from wherever you would like

Function Syntax

fun sum(x: Int, y: Int) = x + y

  • skip declaration of return type
  • omit curly braces

fun sum(x: Int, y: Int) : Int { return x + y }

  • can also be declared like this

 

Overloaded methods vs Kotlin functions

Java requires overloaded methods to provide methods with the same functionality but different arguments

This can lead to a great deal of duplicated, boilerplate code

Functions in Kotlin reduce this by supporting the following features

  • default arguments
  • named arguments

Because of this, a single function can be called multiple ways, with different numbers of arguments.  For example:

// define a log function with default arg values
fun log(tag:String = "tag", msg:String = "")

// can then call the function in multiple ways
log()
log("a tag", "a message")
log(msg="a message", tag="a tag")

 

Data classes

Data classes provides getters/setters, copy, equals/hashCode functions for you

  • reduces boilerplate
  • no maintenance required as class changes

Why does this matter?  Some might argue that this is all generated by the IDE in java anyways.  Hadi makes a great point here.

“… the IDE doesn’t maintain it for you

Sure, the IDE might generate the equals() method for you, but it won’t update it every time the class’s data changes.  It won’t ensure that you didn’t manually adjust the generated implementation and introduce an error.

With Kotlin data classes, this code is generated behind the scenes for you so there isn’t a question about whether it’s up to date or correct (assuming you trust the tooling, which I do at this point)

When using the copy function, you can choose to override the copied value for certain properties

val modelCopy = model.copy(value = "a new value")

 

Destructuring of variables

Data classes support destructuring declarations, when can help make your code more understandable

For example, you can write a function that returns a `Pair` or `Triple`then use destructuring to make the returned values more comprehensible

val returnedPair = returnsPair()
println(returnedPair.second)

// destructuring allows us to supply meaningful names to the values
val (_, email) = returnsPair()
println(email)

Typealiases

Typealiases allow a developer to provide a new name for an existing type

  • typealias CustomerName = String
  • can improve readability
  • can enable you to use an alias throughout your code, then change the alias definition to refactor the code in a single place

 

Extension functions

Allow us to extend a class’s functionality, without inheriting from that class

  • can extend any type in Kotlin or Java
  • adds expressivity

The Kotlin standard library is largely build from extension functions on generic types.  For example:

public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this }

In an Android “context”, we could imagine creating extension functions that perform tasks like getting resources in a backwards compatible way with a cleaner api than using ContextCompat.  For example:

ContextCompat.getColor(context, R.color.primary)

// an extension function version of the same code
context.getColorCompat(R.color.primary)

 

What does all of this buy you?

  • expressivity
  • concise code
  • less boilerplate
  • modern features

You can do everything in Java, and when I say Java I mean you can do everything in assembly

Kotlin doesn’t necessarily come with any single, amazing feature that requires necessitates developers learning it right away.  Rather, the experience of using Kotlin on a daily basis is greater than the sum of its parts.  All of the small niceties add up to make the language feel easy to use, expressive, convenient, and safe.

Hadi makes the comparison that Assembly provides the same functionality as modern languages, but not many people would choose Assembly for their daily development language.  Newer languages emerged that made developers’ jobs easier and more enjoyable.

That’s certainly how I feel about Kotlin.  It’s not that Kotlin suddenly enables me to write well architected code, or think up brilliant design patterns, but instead it gets out of my way and lets me express the patterns and functionality that I want to, in a much easier way.

Leave a Reply