skip to content

What does the Kotlin standard-library function TODO("...") do, and what happens at runtime if execution reaches it?

level: juniorimportance: must knowfreq 60%

answer

  1. Returns Nothing -> fits any type
  2. Throws NotImplementedError (an Error)
  3. Two overloads: TODO() and TODO(reason)
  4. Message: 'An operation is not implemented: <reason>'
  5. inline, package kotlin

basics

~10 s

TODO marks code you haven't written yet. If the program runs that line, it crashes by throwing an error, reminding you to finish it.

solid answer

~40 s

TODO() is an inline stdlib function with return type Nothing. You call it where you must return a value or fill in a body but haven't implemented it yet, e.g. `override fun calc(): Int = TODO("add tax logic")`. Because it returns Nothing, the compiler accepts it as any expected type and treats following code as unreachable, so unrelated 'must return a value' errors disappear while stubbing. At runtime it throws NotImplementedError (a subclass of Error). There are two overloads: TODO() and TODO(reason: String); the reason becomes the message "An operation is not implemented: <reason>". It lets a half-built file still compile and run until the stubbed path is exercised.

code

kotlin · 5 lines
kotlin
fun computeDiscount(order: Order): Int = TODO("apply seasonal rules")

// Compiles fine (Nothing satisfies the Int return type).
// At runtime, calling it throws:
//   NotImplementedError: An operation is not implemented: apply seasonal rules

go deeper

for a junior

Knows TODO() stubs unfinished code and throws if reached.

for a middle

Explains the Nothing return type and why it satisfies any expected type and silences 'must return a value' errors.

for a senior

Distinguishes NotImplementedError (Error) from Exception, notes both overloads, the inline nature, and appropriate use in TDD/stubbing.

for a principal

Discusses team conventions: TODO() as a deliberate compile-but-crash marker vs lingering // TODO comments, CI checks to prevent shipping live TODO() calls, and Nothing's role in exhaustiveness.

## What TODO is `TODO` is a function in the Kotlin standard library (package `kotlin`). Its job is to mark code that is not implemented yet, while still letting the file **compile**. There are two overloads: ```kotlin public inline fun TODO(): Nothing = throw NotImplementedError() public inline fun TODO(reason: String): Nothing = throw NotImplementedError("An operation is not implemented: $reason") ``` ## Key term: the `Nothing` return type `Nothing` is a special type in Kotlin that has **no instances** — an expression of type `Nothing` never returns normally (it either throws or loops forever). Two consequences: - `Nothing` is a **subtype of every other type**, so `TODO()` type-checks anywhere a value is required: `val name: String = TODO()`, `fun f(): Int = TODO()`, etc. - The compiler treats code **after** a `Nothing`-returning call as **unreachable**, so it won't complain that a function "must return a value". That is why you can stub a method and the project still builds. ## Runtime behaviour If execution actually reaches `TODO()`, it **throws** `NotImplementedError`. `NotImplementedError` extends `Error` (not `Exception`), signalling "this is a programming gap, not a recoverable condition". With a reason the message is `"An operation is not implemented: <reason>"`; without one it's `"An operation is not implemented."`. ```kotlin fun price(item: String): Int = TODO("look up price for $item") fun main() { println("start") price("book") // throws NotImplementedError: An operation is not implemented: look up price for book } ``` ## When to use it - Stubbing functions while sketching an API or doing TDD. - Marking `when`/`if` branches you'll fill in later. - IntelliJ surfaces `TODO()` calls so they're easy to find. Because it's `inline`, there's no extra call overhead — the throw is inlined at the call site.

  • Why does adding TODO() let a non-implemented function with a non-Unit return type still compile?
    Because TODO() returns Nothing, which is a subtype of every type, so it satisfies any expected return type, and code after it is treated as unreachable.
  • Is NotImplementedError an Exception?
    No. It subclasses Error, not Exception, so it isn't meant to be caught and recovered from.

Like a sticky note saying 'fill this in' that sets off an alarm if anyone actually tries to use the unfinished part.

saying these in an interview costs you the question

  • Saying TODO() returns Unit or null
  • Thinking TODO() is a no-op that does nothing at runtime
  • Claiming it throws a checked/catchable Exception meant for recovery
  • Believing TODO() is just a comment recognized by the IDE
  • Saying it logs a warning instead of throwing

context