0Pricing
Android Academy · Lesson

let, also, and Scope Functions

Work with nullable values.

let, also, and Scope Functions is a free Android Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Are Scope Functions?

Scope functions let you run a block of code in the context of an object. They make code more concise, especially around nullable values.

This lesson focuses on let and also, the two most useful for null safety, and how they relate to the safe call operator.

The let Function

let runs a block and passes the object in as it. It returns whatever the block returns.

It is handy for transforming a value or scoping a temporary name.

fun main() {
    val length = "Kotlin".let { it.length }
    println(length)  // prints: 6
}

let for Null Safety

Combine ?. with let to execute a block only when a value is not null. The block runs once, with the value as it.

If the value is null, the block is skipped entirely.

fun main() {
    val name: String? = "Ada"
    name?.let {
        println("Hello, " + it)
    }
}

Skipped When Null

When the receiver is null, ?.let does nothing. The block never executes.

This program prints nothing because name is null.

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

Renaming it

You can give the parameter a clearer name instead of it. This improves readability in longer blocks.

Use an arrow to name it explicitly.

val name: String? = "Bo"
name?.let { value ->
    println(value.uppercase())
}

The also Function

also also passes the object as it, but it returns the original object instead of the block's result.

It is great for side effects like logging while keeping the value unchanged in a chain.

fun main() {
    val n = 5.also { println("got " + it) }
    println(n)  // prints: 5
}

let vs also Return Value

The key difference: let returns the block result, while also returns the original object.

Choose let to transform, and also to peek without changing the value.

val a = "hi".let { it.length }   // a = 2 (Int)
val b = "hi".also { it.length }  // b = "hi" (String)

also for Logging

Because also returns the object, it slots into a chain to inspect a value mid-flow.

Here we log a nullable value only if it is present, then keep using it.

fun main() {
    val token: String? = "abc"
    val result = token?.also { println("token: " + it) }
    println(result)
}

let with Elvis

Pair ?.let with the Elvis operator to provide an alternative when the value is null.

The block produces a result when present; the Elvis side handles the null case.

fun main() {
    val raw: String? = null
    val out = raw?.let { it.trim() } ?: "empty"
    println(out)  // prints: empty
}

Choosing a Scope Function

For null safety, reach for let when you want to transform or use a non-null value in a block. Reach for also when you want a side effect but must keep the original object.

Both pair naturally with the safe call operator to avoid null crashes.

user?.let { save(it) }      // transform/use
user?.also { log(it) }      // side effect, keep user

A Combined Example

This program uses ?.let to build a message and Elvis to fall back. It shows the everyday rhythm of null-safe Kotlin.

No explicit null checks appear, yet it is fully safe.

fun main() {
    val city: String? = "Goreme"
    val label = city?.let { "City: " + it } ?: "Unknown"
    println(label)
}

Quick Check

Test your understanding of let and also.

Recap

Scope functions run a block in an object's context. let passes the object as it and returns the block result; also passes it and returns the original object.

With ?.let you act on a value only when it is not null. Next you will pull these ideas together to avoid NullPointerExceptions entirely.

Frequently asked questions

Is the “let, also, and Scope Functions” lesson free?

Yes — the full text of “let, also, and Scope Functions” is free to read here on the web, and the Android Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.

What will I learn in “let, also, and Scope Functions”?

Work with nullable values. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Android Academy?

No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “let, also, and Scope Functions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Android Academy lesson?

Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Nullable vs Non-Null Types
  2. Safe Calls and Elvis
  3. let, also, and Scope Functions
  4. Avoiding NullPointerExceptions
← Back to Android Academy