Skip to main content
St Louis

Back to all posts

How to Use the "Let" And "Run" Functions In Kotlin?

Published on
6 min read
How to Use the "Let" And "Run" Functions In Kotlin? image

Best Kotlin Programming Books to Buy in September 2025

1 Kotlin in Action, Second Edition

Kotlin in Action, Second Edition

BUY & SAVE
$45.98 $59.99
Save 23%
Kotlin in Action, Second Edition
2 Head First Kotlin: A Brain-Friendly Guide

Head First Kotlin: A Brain-Friendly Guide

BUY & SAVE
$50.36 $79.99
Save 37%
Head First Kotlin: A Brain-Friendly Guide
3 Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

BUY & SAVE
$33.00 $38.99
Save 15%
Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language
4 Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

BUY & SAVE
$59.30 $89.99
Save 34%
Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin
5 Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

BUY & SAVE
$48.00 $65.99
Save 27%
Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines
6 Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

BUY & SAVE
$36.20 $59.99
Save 40%
Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer
7 Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

BUY & SAVE
$29.95 $32.95
Save 9%
Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)
8 Kotlin: An Illustrated Guide

Kotlin: An Illustrated Guide

BUY & SAVE
$49.53
Kotlin: An Illustrated Guide
9 Functional Programming in Kotlin

Functional Programming in Kotlin

BUY & SAVE
$46.16 $49.99
Save 8%
Functional Programming in Kotlin
+
ONE MORE?

In Kotlin, the let and run functions are useful tools for simplifying and enhancing your code. While both functions have some similarities, they differ in the way they are used and their intended purposes.

The let function is used to execute a block of code on a non-null object and allows you to perform operations on that object within the block. It is particularly handy when you want to avoid null checks and ensure safe operations by executing code only if the object is not null. The let function takes the object reference as a parameter and gives you a temporary variable (often referred to as it) to work with inside the block. After the block execution, the let function returns the result of the last expression inside the block.

Here's an example usage of the let function:

val length = nullableString?.let { // Code inside this block will be executed only if nullableString is not null it.length } ?: 0

In this example, the let function is used to perform a length operation on nullableString. If nullableString is null, the let block won't be executed, and the length will default to zero (0).

On the other hand, the run function is similar to let but is typically used when you want to execute a series of operations on an object. Unlike let, the run function doesn't provide a temporary variable but instead works directly with the calling object using the this reference. It also returns the result of the last expression inside the block.

Here's an example usage of the run function:

val person = Person().apply { name = "John" age = 30 }

val description = person.run { // Code inside this block is executed on the "person" object "Name: $name, Age: $age" }

In this example, the run function is used to build a string description of a person object. By using run, we can directly access the properties and methods of the object without needing a temporary variable.

Both the let and run functions are powerful tools for concise and readable Kotlin code. Choosing between them depends on whether you want to perform operations on a single object using let or execute a block of code on an object directly using run.

How to use the "also" function in Kotlin?

In Kotlin, the also function is an extension function defined on any object. It allows you to perform some additional actions on an object within a block and then return the original object. Here's how to use it:

  1. Declare an object or use an existing one on which you want to apply the also function.

val value = "Hello"

  1. Use the also function by invoking it on the object and providing a lambda expression as a parameter. The lambda will be executed on the object, and within the lambda, you can perform additional actions.

value.also { println("Before: $it") // Access the object using the implicit `it` parameter // Perform additional actions on `it` println("After: $it") }

  1. The returned value of the also function call will be the original object itself. You can use it for further operations or assignments.

val modifiedValue = value.also { // Perform additional actions on `it` // ... }.toUpperCase() // Chain additional operations after the `also` function call

The also function is useful when you want to perform some additional actions on an object without changing its state, such as logging, debugging, or asserting intermediate values during a function composition.

What are the advantages of using the "let" function in Kotlin?

The "let" function in Kotlin offers several advantages:

  1. Nullable Safe: It is a scoping function that provides a safe way to access nullable objects and avoid null pointer exceptions. Inside the "let" scope, the object is non-null, eliminating the need for null checks.
  2. Simplified Code: It simplifies code by eliminating the need for temporary variables or extra checks. It allows you to perform operations on an object in a concise manner, enhancing readability and reducing boilerplate code.
  3. Immutable Data: It ensures that the object is effectively immutable within the "let" scope. It prevents accidental modifications and maintains data integrity.
  4. Chaining Operations: "let" can be easily chained with other scoping functions like "apply", "run", "also", and "with". This allows for more complex operations and reduces the need for multiple variable assignments.
  5. Function Composition: The functional nature of "let" makes it useful for composing functions together. It allows you to pass the result of one function as an argument to another function directly, facilitating functional programming patterns.
  6. Improved Debugging: When debugging, the "let" function can be utilized to isolate and examine specific parts of the code. It provides a clean scope to analyze and test specific operations.

Overall, the "let" function offers a safe and simplified approach to working with nullable objects, improving readability, reducing boilerplate code, and allowing for functional composition.

What are some practical use cases of the "run" function in Kotlin?

The "run" function in Kotlin is a higher-order function that allows you to execute a block of code on an object. It returns the result of the executed block. The "run" function can have various practical use cases, some of which include:

  1. Simplifying object initialization: You can use "run" to initialize an object and set its properties without repeating the object name. It allows you to chain multiple calls within the block to set the properties of the object. Example: val person = Person().run { name = "John Doe" age = 30 address = "123 Main St" this // returns the Person object }
  2. Object scoping: "run" is useful for scoping a block of code to a specific object. It allows you to access the object's properties and methods directly without explicitly referencing the object name. Example: val result = myObject.run { // perform some operations using the object's properties calculateResult() }
  3. Chaining function calls: "run" can be used to chain function calls on an object. It allows you to perform multiple operations on the object without repeating the object name. Example: val result = myObject.run { performOperation1() performOperation2() performOperation3() getResult() }
  4. Creating DSLs (Domain Specific Languages): "run" is often used in the creation of DSLs in Kotlin. It allows you to define a block of custom code that resembles a domain-specific language for a specific use case. Example: val person = createPerson { name = "John Doe" age = 30 address = "123 Main St" } Here, "createPerson" is a higher-order function that takes a lambda block as a parameter. It can use "run" to execute the block and create a person object with the specified properties.

These are just a few practical use cases of the "run" function in Kotlin. It is a versatile function that can help simplify code and improve readability in various scenarios.