0Pricing
Kotlin Academy · Lesson

Choosing the Right One

When to use each.

Choosing the Right One is a free Kotlin Academy lesson on CoddyKit — lesson 4 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 Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Choosing a Scope Function

With five scope functions available, picking the right one comes down to two questions:

  • How is the object referenced: this or it?
  • What is returned: the lambda result or the object?

The Decision Table

Here is the full picture:

  • let uses it, returns lambda result.
  • run uses this, returns lambda result.
  • with uses this (arg), returns lambda result.
  • apply uses this, returns object.
  • also uses it, returns object.

Use let for Null Safety

When you have a nullable value and want to act on it only if present, reach for let with a safe call.

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

Use run for this + Result

When you want to access members with this and compute a result, use run.

fun main() {
    val text = "scope"
    val info = text.run { "$this has length $length" }
    println(info)
}

Use with for Grouping

When you have a non-null object and want to perform several operations on it, with reads naturally.

fun main() {
    val list = listOf(1, 2, 3)
    with(list) {
        println(sum())
        println(size)
    }
}

Use apply for Configuration

When you create an object and need to set several properties, apply keeps the code clean and returns the object.

data class Server(var host: String = "", var port: Int = 0)

fun main() {
    val server = Server().apply {
        host = "0.0.0.0"
        port = 80
    }
    println(server)
}

Use also for Side Effects

When you want to log, debug, or validate without changing the object, also with it is the choice.

fun main() {
    val nums = listOf(1, 2, 3)
        .also { println("Got ${it.size} items") }
    println(nums.sum())
}

Combining Scope Functions

Real code often chains them: configure with apply, log with also, transform with let.

data class User(var name: String = "", var score: Int = 0)

fun main() {
    val label = User()
        .apply { name = "Max"; score = 90 }
        .also { println("Created $it") }
        .let { "${it.name}: ${it.score}" }
    println(label)
}

Avoiding Overuse

Scope functions can hurt readability if nested or overused. If a plain variable and explicit code reads more clearly, prefer that. Use scope functions to remove genuine repetition, not to be clever.

Nesting Pitfall

Nesting scope functions makes it and this ambiguous. If you must nest, rename the parameter to keep things clear.

fun main() {
    val outer = "A"
    outer.let { o ->
        "B".let { i ->
            println("$o then $i")
        }
    }
}

A Quick Mental Model

Remember: object-returning functions (apply, also) build pipelines; result-returning functions (let, run, with) transform values. Pick it versus this based on readability.

Quick Check

Which scope function should you use to configure a freshly created object and get that object back?

Recap

You now have a decision framework: choose by reference (this vs it) and return value (object vs result). let and run transform, with groups, apply configures, and also adds side effects. Use them to reduce noise, not to add it.

Frequently asked questions

Is the “Choosing the Right One” lesson free?

Yes — the full text of “Choosing the Right One” is free to read here on the web, and the Kotlin 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 Kotlin Academy course, upgrade to CoddyKit PRO.

What will I learn in “Choosing the Right One”?

When to use each. You practise Kotlin 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 Kotlin Academy?

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

How long does the “Choosing the Right One” 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 Kotlin Academy lesson?

Yes. Every Kotlin 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. let and run
  2. apply and also
  3. with
  4. Choosing the Right One
← Back to Kotlin Academy