A summary of Kotlin tips from #31DaysofKotlin

Murat Can Bur
GDGIstanbul
Published in
6 min readApr 27, 2018

elvis operator

When you have a nullable type, use the default value if it is not null use the value.

Docs: https://kotlinlang.org/docs/reference/null-safety.html#elvis-operator

string templates

A string template expression starts with a dollar sign $. Evaluate expressions using ${expression}.

Docs: https://kotlinlang.org/docs/reference/basic-types.html#string-templates

Destructuring declarations

Android KTX uses destructuring to assign the component values of a color.

Docs: https://kotlinlang.org/docs/reference/multi-declarations.html

when

when replaces the switch operator. Kotlin’s when expression can match on just about anything.

Docs: https://kotlinlang.org/docs/reference/control-flow.html#when-expression

ranges

For loops, range expressions

Docs : https://kotlinlang.org/docs/reference/ranges.html

properties

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

Docs : https://kotlinlang.org/docs/reference/properties.html

data classes : one role: to hold data.

The default implementation of equals() is generated (so are hashCode(), toString(), and copy()) and checks for structural equality.

Docs : https://kotlinlang.org/docs/reference/data-classes.html

access modifiers

Kotlin has a rich set of visibility modifiers you can use as well: private, protected, internal.

Docs : https://kotlinlang.org/docs/reference/visibility-modifiers.html

default parameters

Is the number of method overloads getting out of hand? Specify default parameter values in functions. Make the code even more readable with named parameters.

Docs : https://kotlinlang.org/docs/reference/functions.html#default-arguments

sealed classes

Kotlin sealed classes let you easily handle error data.

They’re a perfect fit for ViewHolders.

RecyclerView and detail clicks, shares, delete actions and etc.

Docs : https://kotlinlang.org/docs/reference/sealed-classes.html

lazy

Defer the cost of expensive property initialization until they’re actually needed

Docs : https://kotlinlang.org/docs/reference/delegated-properties.html#lazy

lateinit

In Kotlin non-null vals must be initialized. What to do? It’s a promise: initialize me later!

Docs : https://kotlinlang.org/docs/reference/properties.html#late-initialized-properties-and-variables

require

Are your function arguments valid? Check before using them, with require. If they’re not valid an IllegalArgumentException is thrown.

Docs : https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/require.html

inline

Can’t wait to use lambdas to make new APIs? Get in line. Kotlin lets you specify a function as inline — which means calls will be replaced with the function body. Breathe and make lambda-based APIs with zero overhead.

Docs : https://kotlinlang.org/docs/reference/inline-functions.html

operator overloading

Write Kotlin (time * 2) faster with operator overloading. Objects like Path, Range or SpannableStrings naturally allow for operations like addition or subtraction.

Docs : https://kotlinlang.org/docs/reference/operator-overloading.html#operator-overloading

basic syntax

Utility methods for a class? Add them to the top level of the source file. In Java, they are compiled as static methods of that class

Docs : https://kotlinlang.org/docs/reference/basic-syntax.html

Iterating types without an iterator

Iterators in interesting places? Android KTX adds iterators to ViewGroup and SparseArray. To define iterator extensions use the operator keyword. Foreach loops will use the extensions!

Docs :https://github.com/android/android-ktx/blob/master/src/main/java/androidx/core/view/ViewGroup.kt#L66

easy content values

Combine the power of ContentValues with the brevity of Kotlin. Use the Android KTX ContentValues creator and just pass a Pair<StringKey, Value>. Android KTX implementation.

Docs : https://github.com/android/android-ktx/blob/master/src/main/java/androidx/core/content/ContentValues.kt#L21

bundles

Bundle creator in Android KTX. No more calls to putString, putInt, or any of their 20 friends. One call will make you a new Bundle

Docs : https://github.com/android/android-ktx/blob/master/src/main/java/androidx/core/os/Bundle.kt#L27

cleaning up postDelayed

Lambdas are sweet. With last parameter call syntax, you can cleanup callbacks, Callable, and Runnable. Example from Android KTX.

Docs :https://github.com/android/android-ktx/blob/master/src/main/java/androidx/core/os/Handler.kt#L38

@file:JvmName

Using Kotlin and Java in the same project? Do you have classes with top-level functions or properties? By default, the compiler generates the class name as YourFileKt. Change it by annotating the file with @file:JvmName.

Docs :https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#package-level-functions

One plus : Statics, @JvmStatic

by

Delegate your work to another class with by.

Docs :https://kotlinlang.org/docs/reference/delegation.html#implementation-by-delegation

extension functions

No more Util classes! Extend the functionality of a class by using extension functions. Put the name of the class you’re extending before the name of the method you’re adding.

KTX Graphics

Android KTX has a great set of functions to make your code more concise when working with classes from the graphics package.

spans

Android KTX adds extension functions for some of the most common spans and makes the API easier to use.

Docs : https://github.com/android/android-ktx/blob/master/src/main/java/androidx/core/text/SpannableStringBuilder.kt

parcelize

Love the speed of Parcelable, but don’t like writing all that code? Say hello to @Parcelize.

KTX view padding

Android KTX lets you set the padding on one side of a view using default parameters.

Docs : https://github.com/android/android-ktx/blob/master/src/main/java/androidx/core/view/View.kt#L117

Disclaimer :

All the content of these example codes and some descriptions are taken from Android Developers twitter account.

--

--

Murat Can Bur
GDGIstanbul

Blogger , Public Speaker, Mobile Engineering Team Lead @Trendyol