CodeGym /Courses /Kotlin SELF /Closures and side effects

Closures and side effects

Kotlin SELF
Level 21, Lesson 4
Available

1. What is a closure: a lambda with outer context

When you first get familiar with lambdas, it seems like they are just a “short function right at the call site”. But reality is a bit trickier: a lambda can read variables that are declared outside, in the surrounding scope. And this is very much like carrying a backpack: wherever you go, the backpack goes with you (and sometimes it contains something heavy and suspicious).

Technically, that is exactly a closure: the lambda “closes over” outer variables and can use them. And it works not only for lambdas, but also for local functions (functions inside a function). In the official Kotlin documentation there is a classic example: a local function dfs uses a variable visited declared in the outer function—this is a typical context capture by a closure.

Let’s build intuition with a small example.

fun main() {
    val prefix = "[LOG]" // outer variable

    val log: (String) -> String = { msg ->
        "$prefix $msg"   // lambda uses prefix
    }

    println(log("start")) // [LOG] start
}

Here prefix lives outside, and log uses it inside. We didn’t “thread it through as a parameter”, but access is there.

2. Capturing val and var: where it is safe and where “state” begins

The most “friendly” kind of closure is when a lambda captures a val, i.e., an immutable variable. In that case it is easy to keep the mental model: “the lambda uses a configuration”. It is almost like a function with an additional parameter, except the parameter is hidden in the environment.

This is often used to create “configured” predicates or transformers. For example, we need a predicate “string longer than N”. N is a configuration value, and the check itself remains pure.

fun longerThan(minLen: Int): (String) -> Boolean {
    return { s -> s.length > minLen } // minLen is captured
}

fun main() {
    val isLong = longerThan(3)

    println(isLong("go"))      // false
    println(isLong("kotlin"))  // true
}

Notice an important psychological point: isLong looks like a regular function (String) -> Boolean, and it really is “just a check”. No hidden magic.

Mini diagram: “configuration” as an outer val

flowchart LR
    A["minLen=3 (val)"] --> B["lambda (String)->Boolean"]
    B --> C["call: isLong('kotlin')"]
    C --> D[true]

In general, you can remember a simple rule: capturing a val is most often safe, because the value does not change, which means the lambda’s behavior is stable.

Now comes the exact moment when code turns into a detective story: “why did it work yesterday, but not today”. If a lambda captures a var, it gains access to mutable state, and that means:

  • the lambda’s behavior may depend on the past,
  • the same call may produce different effects if the state has changed,
  • the worst part is that the type (T) -> Boolean does not show this at all.

Let’s look at an “innocent” predicate that suddenly starts counting how many times it was called.

fun main() {
    var checks = 0

    val isEvenWithCounter: (Int) -> Boolean = { x ->
        checks++              // side effect
        x % 2 == 0
    }

    println(isEvenWithCounter(2)) // true
    println(isEvenWithCounter(3)) // false
    println("checks=$checks")     // checks=2
}

Formally, the predicate “checks evenness”. In practice, it also mutates the outer checks. This is hidden state: the reader sees “predicate”, but gets “predicate + counter”.

Sometimes you need that (for example, logging or collecting statistics), but the problem is that it is easy to do accidentally and then be surprised by the consequences for a long time.

3. Side effects and the “purity” of higher-order functions

When we say “side effect”, we mean an action that goes beyond “returned a value and that’s it”. For example: printing to the console, changing an outer variable, writing to a file, adding to a list. A lambda can be pure (only computes and returns) or impure (does something else too).

It is important to understand: side effects are not forbidden. Kotlin will not come at night and take your keyboard away for using println. But if a side effect is hidden where you expect a “simple function”, the code becomes less predictable.

Let’s compare two predicates. Both have the type (Int) -> Boolean, but one is “quiet” and the other is “talkative”.

fun main() {
    val quiet: (Int) -> Boolean = { it > 0 }

    val noisy: (Int) -> Boolean = { x ->
        println("checking $x") // side effect
        x > 0
    }

    println(quiet(10)) // true
    println(noisy(10)) // checking 10
                      // true
}

One just returns true/false. The other prints every time. Sometimes this is convenient for debugging, but if you pass such a predicate into a higher-order function, “unexpected” prints can pollute output and make program behavior noisy.

Now let’s take a higher-order function that looks honest and simple: “count how many elements match a predicate”. At first glance, it is pure: input → computation → result.

fun countMatching(items: List<Int>, predicate: (Int) -> Boolean): Int {
    var count = 0
    for (x in items) {
        if (predicate(x)) count++
    }
    return count
}

And now let’s pass a predicate that captures an outer var and mutates it.

fun main() {
    val xs = listOf(-1, 2, 3)

    var hits = 0
    val p: (Int) -> Boolean = { x ->
        val ok = x > 0
        if (ok) hits++     // side effect: mutate outer var
        ok
    }

    val result = countMatching(xs, p)

    println("result=$result hits=$hits") // result=2 hits=2
}

The problem is not that this “doesn’t work”. It works. The problem is that countMatching can no longer be perceived as “just counting”. It “just counts” only for pure predicates. With impure predicates it becomes part of more complex behavior.

And this is the moment when it is important to learn to ask yourself while reading code: what did this lambda capture from the outside?

4. How to quickly spot closures while reading code

When a lambda appears in code, it is useful not only to understand “what it does”, but literally “what it touches”. It is like checking your pockets before doing laundry: if you forget your keys, it will be fun later, but not for you.

There is a simple mental algorithm.

First, look at the lambda parameters: { x -> ... }. Then look at the body: which names are used. If you see variables inside that are not parameters and are not declared inside the lambda (via val/var), then they are taken from the outer scope—meaning the lambda is a closure.

Local functions behave similarly: if an inner function uses a variable from the outer one, it also closes over it. In the Kotlin documentation example, a local function dfs(current: Person) uses visited declared outside—this is exactly the same mechanism of capturing outer state.

5. Practice: expense reports and “slippery” state

We have been building a small console application for a few days with a list of entities and commands like add/list/remove (you built things like that with collections and loops). Today we won’t reinvent the world: we’ll add simple reports to our app, and they are a convenient place to see where closures help and where they get in the way.

Let’s keep an “expense” without OOP for now (classes will come much later), as a Triple:

  • first — id
  • second — category
  • third — amount
fun main() {
    val expenses = mutableListOf<Triple<Int, String, Int>>()

    expenses.add(Triple(1, "food", 120))
    expenses.add(Triple(2, "transport", 60))
    expenses.add(Triple(3, "food", 300))

    println(expenses.size) // 3
}

A useful closure: a minimum-amount filter

Let’s make a function that prints expenses that match a condition. We intentionally use a for loop to stay aligned with the material of the current day and not drift into “magic” operations from the standard library.

fun printExpensesIf(
    expenses: List<Triple<Int, String, Int>>,
    predicate: (Triple<Int, String, Int>) -> Boolean
) {
    for (e in expenses) {
        if (predicate(e)) {
            println(e)
        }
    }
}

Now we’ll create a predicate via a closure, where minAmount is an outer configuration parameter captured as a val.

fun minAmountPredicate(minAmount: Int): (Triple<Int, String, Int>) -> Boolean {
    return { e -> e.third >= minAmount }
}

fun main() {
    val expenses = listOf(
        Triple(1, "food", 120),
        Triple(2, "transport", 60),
        Triple(3, "food", 300),
    )

    val bigOnly = minAmountPredicate(100)
    printExpensesIf(expenses, bigOnly)
    // (1, food, 120)
    // (3, food, 300)
}

This is a good example of a “proper” closure: minAmount is captured but does not change, and the predicate remains pure.

A dangerous closure: line numbering via a captured var

Now imagine you want to number lines nicely in a report. The temptation appears: “let’s create a var i outside and increment it inside the lambda”. It works… but if you reuse the lambda, you may get unexpected numbering.

fun main() {
    val expenses = listOf(
        Triple(1, "food", 120),
        Triple(2, "transport", 60),
    )

    var i = 1
    val printLine: (Triple<Int, String, Int>) -> Unit = { e ->
        println("${i}. id=${e.first} ${e.second} ${e.third}")
        i++
    }

    for (e in expenses) printLine(e)
    // 1. id=1 food 120
    // 2. id=2 transport 60

    for (e in expenses) printLine(e)
    // 3. id=1 food 120
    // 4. id=2 transport 60
}

If you expected the second pass to start from 1 again, surprise: i has already been modified. That is, printLine now depends on the past, even though by type it is just (Expense) -> Unit.

Sometimes this is acceptable (for example, you do exactly one pass and the lambda is not used anywhere else). But if such a handler is passed around, stored in a variable, called from different functions—you get “state that lives longer than you think”.

6. Patterns: how to keep effects visible and local

When problems with closures begin, you might want to “ban everything and live in a monastery of pure functions”. In practice, it is better to master a few calm habits that make code predictable, even when side effects are needed.

If you have a counter, let it live where the counting happens

If your goal is to count something, it is better to return it as a value rather than mutate an outer var. This is especially relevant for predicates: a predicate should answer the question true/false, not “and also do a little bookkeeping on the side”.

Let’s compare two approaches. First, a “hidden counter”:

fun main() {
    val xs = listOf(1, 2, 3, 4)

    var evens = 0
    val p: (Int) -> Boolean = { x ->
        val ok = x % 2 == 0
        if (ok) evens++
        ok
    }

    val count = countMatching(xs, p)
    println("count=$count evens=$evens") // count=2 evens=2
}

And now a more honest option: count inside and return.

fun countEvens(xs: List<Int>): Int {
    var evens = 0
    for (x in xs) if (x % 2 == 0) evens++
    return evens
}

fun main() {
    println(countEvens(listOf(1, 2, 3, 4))) // 2
}

The second variant is more banal, but it is impossible to “not realize” what is happening.

If you need an effect, separate action from predicate

If you need both to select elements and to print—don’t cram printing into the predicate. In the previous lecture we made a function with predicate and action. This is an excellent “architecture micro-habit”: the effect lives where it belongs.

fun forEachIf(
    items: List<Int>,
    predicate: (Int) -> Boolean,
    action: (Int) -> Unit
) {
    for (x in items) {
        if (predicate(x)) action(x)
    }
}

fun main() {
    val xs = listOf(1, 2, 3, 4)

    val isEven: (Int) -> Boolean = { it % 2 == 0 }
    val printEven: (Int) -> Unit = { println("even=$it") }

    forEachIf(xs, isEven, printEven)
    // even=2
    // even=4
}

Notice how much easier it is to read: the predicate is responsible for “which ones”, the action is responsible for “what to do”.

Keep the lifetime of a closure short

One of the main practical dangers is not the capture of a var itself, but that a lambda with captured state starts living “too long”. For example, you store it in a file-level variable, or pass it far away.

Try to keep such lambdas closer to the place where they are used. If you need a counter-lambda only for one report—create it inside the report function and don’t return it outside.

This resembles the principle “don’t leave the faucet open”: if water is needed only to fill a glass, don’t leave the tap running overnight.

7. Typical mistakes

Mistake #1: a predicate with side effects is disguised as a “simple check”.
Beginners often put println inside a predicate, increment an outer counter, or change an outer flag, and then wonder why a higher-order function became “noisy” or why results depend on the number of calls. If a function is conceptually a “condition”, try to keep it pure, and move effects into a separate action.

Mistake #2: capturing a var is used as a “quick way to pass state”, and it bites you on reuse.
From the outside it looks like: “so what, var i, I’ll number the lines now”. But as soon as you run the same code a second time, the counter does not reset because it lives in the closure. If the state must start over on each run—keep it inside the function/loop that represents the “run”.

Mistake #3: a “combine-all” lambda: it checks, prints, and accumulates statistics.
Such code is hard to test and hard to read: the signature says one thing, but it does a whole platoon of tasks. It is better to separate roles: the predicate returns Boolean, the action performs outward effects, and statistics are computed by a separate function or a separate step.

Mistake #4: while reading code, people miss the capture of outer variables.
A very typical situation: someone reads { x -> x > limit } but does not notice that limit is an outer variable that may be changed somewhere else. A useful habit: when you see a lambda, quickly scan the names inside and ask yourself: “is this a parameter, a local val, or a captured variable?”. The same mechanism works for local functions, as in examples where an inner function uses an outer visited.

1
Task
Kotlin SELF, level 21, lesson 4
Locked
Log formatter
Log formatter
1
Task
Kotlin SELF, level 21, lesson 4
Locked
Length filter
Length filter
1
Task
Kotlin SELF, level 21, lesson 4
Locked
Matching counter
Matching counter
1
Task
Kotlin SELF, level 21, lesson 4
Locked
Problem statement and action
Problem statement and action
1
Survey/quiz
Lambdas and callable references, level 21, lesson 4
Unavailable
Lambdas and callable references
Lambdas and callable references
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION