Kotlin Language Features to Improve Your Code

Ashish Yadav
3 min readMay 25, 2023

Learn how to use Kotlin’s features to write more concise and readable code, such as the let function, the apply function, and the with function, and how to use them to improve your code’s readability.

Use the Elvis operator (?:)

The Elvis operator is a great way to handle null values in Kotlin. It takes two operands, and the result is the first operand if it is not null, and the second operand otherwise. For example, the following code will print “Hello, world!” even if the name variable is null:

val name: String? = null
println("Hello, ${name ?: "world"}")

This code is equivalent to the following code:

val name: String? = null
if (name != null) {
println("Hello, $name")
} else {
println("Hello, world")
}

The Elvis operator is a more concise way to write this code and is also more readable.

let function

The let function is a great way to execute a code block on a value and then return the value. It is often used to check for null values and perform some action if the value is not null. For example, the following code will print “Hello, world!” if the name variable is not null:

val name: String? = null
println(name?.let { "Hello, $it" })

This code is equivalent to the following code:


val name: String? = null
if (name != null) {
println("Hello, $name")
} else {
return
}

The let function is a more concise way to write this code and is also more readable.

apply function

The apply function is a great way to set the properties of an object and then return the object. It is often used to initialize an object and then perform some action on it. For example, the following code will create a new Person object with the name “John Doe” and then print the person’s name:

val person = Person().apply {
name = "John Doe"
}
println(person.name)

This code is equivalent to the following code:

val person = Person()
person.name = "John Doe"
println(person.name)

The apply function is a more concise way to write this code and is also more readable.

with function

The with function is a great way to access the properties of an object and then perform some action on it. It is often used to avoid having to repeat the name of the object multiple times. For example, the following code is equivalent to the previous example:

with(Person()) {
name = "John Doe"
println(name)
}

This code is more concise than the previous code, and it is also more readable.

The range operator (..)

The range operator is a great way to iterate over a range of values. For example, the following code will print the numbers from 1 to 10:

for (i in 1..10) {
println(i)
}

This code is equivalent to the following code:

var i = 1
while (i <= 10) {
println(i)
i++
}

The range operator is a more concise way to write this code and is also more readable.

when expression

The when expression is a great way to handle multiple cases in a single statement. For example, the following code will print “Hello, world!” if the name variable is not null, and “Goodbye, world!” if it is null:

val name: String? = null
when (name) {
null -> println("Goodbye, world!")
else -> println("Hello, $name!")
}

This code is equivalent to the following code:

val name: String? = null
if (name == null) {
println("Goodbye, world!")
} else {
println("Hello, $name!")
}

The when expression is a more concise way to write this code and is also more readable.

Kotlin is a powerful language that can be used to write concise, readable, and efficient code. By following the tips in this article, you can improve your Kotlin skills and write better code.

Here are some additional tips that you may find helpful:

  • Use Kotlin’s documentation to learn more about the language’s features.
  • Read Kotlin code from open-source projects to see how other developers use the language.
  • Join the Kotlin community and ask questions on forums and Slack channels.

With a little practice, you’ll be writing great Kotlin code in no time!

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Ashish Yadav
Ashish Yadav

Responses (1)

Write a response

Why you think that code you show is more readable?

--