skip to content

What is the subjectless (argument-less) form of `when`, and when would you use it over an `if`/`else if` chain?

level: middleimportance: should knowfreq 60%

answer

  1. `when {}` with no parentheses value
  2. Each branch is a boolean expression
  3. Replaces if/else-if ladder
  4. First true branch wins, lazy top-to-bottom
  5. Use subject form when all branches test the same value

basics

~20 s

Write when {} with no value in the parentheses. Each branch is its own boolean condition, and the first true one wins. It reads more cleanly than a long if/else if chain when you have several unrelated conditions.

solid answer

~40 s

Omitting the subject — `when { cond1 -> ...; cond2 -> ...; else -> ... }` — turns each branch into an independent boolean expression evaluated top to bottom; the first `true` branch runs. This is the direct replacement for an `if`/`else if`/`else` ladder and is preferred for readability when branches test different variables or compound conditions. As an expression it still needs to be exhaustive (typically via `else`). You can mix comparisons, function calls, `&&`/`||`, and even `in`/`is` checks against any in-scope value. A common idiom is capturing a value in the subject when all branches compare it, versus going subjectless when conditions are heterogeneous. Conditions are evaluated lazily left-to-right, so later branch conditions aren't computed once an earlier one matches.

code

kotlin · 6 lines
kotlin
fun classify(n: Int): String = when {
    n < 0 -> "negative"
    n == 0 -> "zero"
    n % 2 == 0 -> "positive even"
    else -> "positive odd"
}

go deeper

for a junior

Recognizes when {} as an if/else-if alternative and writes basic boolean branches.

for a middle

Chooses subjectless vs subject form appropriately and knows conditions short-circuit top to bottom.

for a senior

Reasons about side effects/cost of branch order and jump-table optimization for the subject form.

for a principal

Weighs readability and maintainability trade-offs across a codebase and sets conventions for when each form is used.

## The subjectless form Normally `when (subject)` compares one value against each branch. If you **omit** the subject, each branch becomes a **standalone boolean expression**: ```kotlin fun grade(score: Int, attended: Boolean): String = when { !attended -> "Incomplete" score >= 90 -> "A" score in 80..89 -> "B" // any expression, including in-checks score >= 70 && score < 80 -> "C" else -> "F" } ``` Kotlin evaluates `!attended`, then `score >= 90`, and so on, **top to bottom**, and runs the **first** branch whose condition is `true`. This is exactly equivalent to: ```kotlin if (!attended) "Incomplete" else if (score >= 90) "A" // ... else "F" ``` ## When to prefer it over `if`/`else if` - **Heterogeneous conditions:** branches test different variables or compound predicates — the flat `when {}` reads better than a nested ladder. - **Expression result:** you want a single value out of several conditions; `when {}` as an expression is concise and forces you to think about exhaustiveness. - **Consistency:** keeps multi-branch logic in one visual style. Prefer the **subject form** `when (x)` when **every** branch compares the *same* value — it's shorter and lets the compiler emit a jump table for dense int/enum subjects. ## Evaluation semantics - Conditions are evaluated **lazily and in order**; once a branch matches, no later condition is evaluated. This matters if conditions have side effects or are expensive. - As an **expression**, a subjectless `when` must be exhaustive — usually you add `else`. - You can still use `in`/`!in` and `is`/`!is` inside subjectless branches; you just write the operand explicitly: `x is String`, `n in range`. ## Capturing the subject Kotlin lets you bind the subject to a variable: `when (val r = compute()) { ... }`. That's a subject form; the subjectless form is the right tool when there's no single subject to bind.

  • Are branch conditions evaluated all at once?
    No — they are evaluated top to bottom and short-circuit; once one is true the rest are skipped.
  • When is the subject form better?
    When every branch compares the same value; it's shorter and can compile to a jump table.

A subject when asks 'which box does this one item fit?'; a subjectless when is a series of independent yes/no gates, each opening for a different reason.

saying these in an interview costs you the question

  • Saying all branch conditions are evaluated even after a match
  • Claiming subjectless `when` can't return a value
  • Not knowing it's equivalent to an if/else-if chain
  • Using subjectless form when a single subject would be clearer

context