Illustration by Virginia Poltrack

#31DaysOfKotlin — Week 1 Recap

Florina Muntenescu
Android Developers
4 min readApr 3, 2018

--

The more Kotlin code we write, the more we love it! Kotlin’s modern language features together with Android KTX made our Android code more concise, clear and pleasant. We (@FMuntenescu and @objcode) started the #31DaysOfKotlin series as a way of sharing some of our favorite Kotlin and Android KTX features and hopefully get more of you to like it as much as we do.

Check out the other recaps:

In the first 7 days of Kotlin we focused on the basics.

Day 1: Elvis operator

Handling nulls in style? Check out the elvis operator ?:, to cut your “null-erplate”. It’s just a small bit of syntax sugar to replace nulls with a default value or even return! Docs: Elvis operator.

val name: String = person.name ?: “unknown”
val age = person.age ?: return

Day 2: String templates

Formatting Strings? Refer to variables and expressions in string literals by putting $ in front of the variable name. Evaluate expressions using ${expression}. Docs: string templates.

val language = “Kotlin”// “Kotlin has 6 characters”
val text = “$language has ${language.length} characters”

Day 3: Destructuring declarations

Now with prisms? Android KTX uses destructuring to assign the component values of a color. You can use destructuring in your classes, or extend existing classes to add destructuring. Docs: destructuring declarations.

// now with prisms
val (red, green, blue) = color
// destructuring for squares
val (left, top, right, bottom) = rect
// or more pointedly
val (x, y) = point

Day 4: When expressions

A switch statement with superpowers? Kotlin’s when expression can match on just about anything. Literal values, enums, ranges of numbers. You can even call arbitrary functions! Docs: when

class Train(val cargo: Number?) {
override fun toString(): String {
return when (cargo) {
null, 0 -> "empty"
1 -> "tiny"
in 2..10 -> "small"
is Int -> "big inty"
else -> "$cargo"
}
}
}

Day 5: For loops, range expressions and destructuring

For loops get superpowers when used with two other Kotlin features: range expressions and destructuring. Docs: ranges, destructuring.

// iterating in the range 1 to 100
for(i in 1..100) {…}
// iterating backwards, in the range 100 to 1
for(i in 100 downTo 1){…}
// iterating over an array, getting every other element
val array = arrayOf(“a”, “b”, “x”)
for(i in 1 until array.size step 2 ){…}
// iterating over an array with the item index and destructuring
for((index, element) in array.withIndex()) {…}
// iterating over a map
val map = mapOf(1 to “one”, 2 to “two”)
for( (key, value) in map){…}

Day 6: Properties

In Kotlin, classes can have mutable and read-only properties, with getters and setters generated by default. You can also implement custom ones if required. Docs: properties.

class User {
// properties
val id: String = “” // immutable. just getter
var name: String = “” // default getter and setter var surname: String = “” // custom getter, default setter
get() = surname.toUpperCase() // custom getter declaration
var email: String = “” // default getter, custom setter
set(value) { // custom setter declaration
// “value” = name of the setter parameter
// “field” = property’s backing field; generated
if(isEmailValid(value)) field = value
}
}

Day 7: Data classes and equality

Creating classes with one role: to hold data? Mark them as “data” classes. The default implementation of equals() is generated (so are hashCode(), toString(), and copy()) and checks for structural equality. Docs: data classes, equality

data class User(
val name: String,
val email: String,
val address: Address,

)
public class UserListDiffCallback: DiffUtil.Callback() { override fun areContentsTheSame(
oldItemPosition: Int,
newItemPosition: Int
): Boolean {
// use the generated equals method
return newUserList[newItemPosition] ==
oldUserList[oldItemPosition])
}

This week focused on the basics: removing null errors, simplifying loops and conditions, improving getters and setters, and removing boilerplate. Next week we’ll dive into more Kotlin features!

Did you already start using Kotlin? We’d love to hear what other features you found great and how you used them in your Android app.

Extra thanks to our reviewers: Jake, Romain, Nick, James, Don and our designer: Virginia.

--

--