How do I understand the coroutine lifecycle in Kotlin?

Kotlin coroutine lifecycle: the mental model

A Kotlin coroutine is a lightweight concurrent task that runs inside a CoroutineScope and is controlled by a Job.

The lifecycle is mostly about the state of that Job.

At a high level:

New → Active → Completing → Completed
              ↘ Cancelling → Cancelled

Most of the time, you mainly think about:

Active → Completed
Active → Cancelled

1. Coroutine starts in a scope

You usually launch a coroutine with launch or async:

import kotlinx.coroutines.*

fun main() = runBlocking {
    val job = launch {
        println("Coroutine started")
        delay(1000)
        println("Coroutine finished")
    }

    println("Job is active: ${job.isActive}")
    job.join()
    println("Job is completed: ${job.isCompleted}")
}

Here:

  • runBlocking creates a coroutine scope.
  • launch starts a child coroutine.
  • job represents that coroutine’s lifecycle.
  • join() waits until the coroutine finishes.

2. The main lifecycle states

New

A coroutine can be created but not started yet if you use CoroutineStart.LAZY.

val job = launch(start = CoroutineStart.LAZY) {
    println("Started later")
}

At this point, the coroutine exists but has not begun running.

You start it with:

job.start()

or:

job.join()

Active

Once started, the coroutine becomes active.

val job = launch {
    delay(1000)
}

While active, it can:

  • run code
  • suspend
  • resume later
  • launch child coroutines
  • be cancelled

Suspension does not mean the coroutine is inactive. A coroutine waiting in delay, for example, is still active.


Completing

When the coroutine body finishes, the coroutine enters a completing phase.

This matters especially if it has child coroutines:

val job = launch {
    launch {
        delay(1000)
        println("Child done")
    }

    println("Parent body done")
}

The parent coroutine’s body may finish quickly, but the parent Job is not fully completed until its children complete.

This is part of Kotlin’s structured concurrency model.


Completed

A coroutine is completed when:

  • its body finishes normally
  • all of its child coroutines are completed

Example:

val job = launch {
    delay(500)
    println("Done")
}

job.join()
println(job.isCompleted)

Cancelling

When cancellation is requested, the coroutine enters a cancelling state.

val job = launch {
    repeat(10) {
        delay(500)
        println("Working $it")
    }
}

delay(1200)
job.cancel()

Cancellation is cooperative. The coroutine needs to reach a cancellable suspension point or check cancellation manually.

Common cancellable suspension points include:

  • delay
  • withContext
  • yield
  • many kotlinx.coroutines suspending functions

Cancelled

Once cancellation cleanup finishes, the coroutine becomes cancelled.

val job = launch {
    try {
        delay(5000)
    } finally {
        println("Cleanup")
    }
}

delay(100)
job.cancelAndJoin()
println(job.isCancelled)

cancelAndJoin() cancels the coroutine and waits for it to fully finish.


3. Cancellation is cooperative

This coroutine cancels easily:

val job = launch {
    while (isActive) {
        println("Working")
        delay(100)
    }
}

delay(500)
job.cancelAndJoin()

This one may not cancel quickly:

val job = launch {
    while (true) {
        // CPU-heavy work, no suspension, no cancellation check
    }
}

For CPU-bound work, check cancellation manually:

val job = launch {
    while (isActive) {
        // Do a chunk of work
    }
}

Or call:

ensureActive()

Example:

val job = launch {
    repeat(1_000_000) {
        ensureActive()
        // Do work
    }
}

4. Parent-child relationship

Coroutines launched inside another coroutine become its children:

fun main() = runBlocking {
    val parent = launch {
        launch {
            delay(1000)
            println("Child finished")
        }

        println("Parent body finished")
    }

    parent.join()
    println("Parent fully completed")
}

Output is conceptually:

Parent body finished
Child finished
Parent fully completed

The parent waits for the child before becoming completed.


5. Cancelling a parent cancels its children

val parent = launch {
    launch {
        repeat(10) {
            delay(300)
            println("Child working")
        }
    }
}

delay(700)
parent.cancelAndJoin()

When the parent is cancelled, its child coroutine is cancelled too.

This is one of the most important lifecycle rules.


6. Child failure usually cancels the parent

By default, if a child coroutine fails with an exception, the parent is cancelled:

val parent = launch {
    launch {
        throw RuntimeException("Child failed")
    }

    launch {
        delay(1000)
        println("This may be cancelled")
    }
}

This behavior keeps concurrent work grouped together: if one part fails, the whole operation usually fails.

If you want children to fail independently, use supervisorScope or SupervisorJob.

supervisorScope {
    launch {
        throw RuntimeException("Child failed")
    }

    launch {
        delay(1000)
        println("Still runs")
    }
}

7. launch vs async

Both create coroutines, but they expose results differently.

launch

Use launch for fire-and-forget work that does not return a value.

val job: Job = launch {
    println("Doing work")
}

It returns a Job.


async

Use async for work that returns a value.

val deferred: Deferred<Int> = async {
    delay(500)
    42
}

val result = deferred.await()
println(result)

It returns a Deferred<T>, which is also a Job.

await() waits for completion and returns the result, or throws the coroutine’s exception.


8. Cleanup with finally

A coroutine can clean up when it completes or is cancelled:

val job = launch {
    try {
        repeat(10) {
            delay(300)
            println("Working $it")
        }
    } finally {
        println("Cleaning up")
    }
}

delay(800)
job.cancelAndJoin()

If you need to call a suspending function during cleanup, use NonCancellable:

val job = launch {
    try {
        delay(5000)
    } finally {
        withContext(NonCancellable) {
            delay(300)
            println("Cleanup finished")
        }
    }
}

Use this sparingly, because it delays cancellation.


9. Common lifecycle operations

Operation Meaning
job.start() Starts a lazy coroutine
job.join() Waits for coroutine completion
job.cancel() Requests cancellation
job.cancelAndJoin() Cancels and waits until finished
job.isActive true while active
job.isCompleted true after completion or cancellation
job.isCancelled true if completed due to cancellation

10. Practical lifecycle example

import kotlinx.coroutines.*

fun main() = runBlocking {
    val job = launch {
        try {
            println("Started")

            repeat(5) { index ->
                delay(500)
                println("Working $index")
            }

            println("Finished normally")
        } catch (e: CancellationException) {
            println("Cancelled")
            throw e
        } finally {
            println("Cleanup")
        }
    }

    delay(1200)

    println("Requesting cancellation")
    job.cancelAndJoin()

    println("Job completed: ${job.isCompleted}")
    println("Job cancelled: ${job.isCancelled}")
}

Possible output:

Started
Working 0
Working 1
Requesting cancellation
Cancelled
Cleanup
Job completed: true
Job cancelled: true

Simple way to remember it

Think of a coroutine like this:

Created in a scope
→ runs as a Job
→ may suspend and resume many times
→ finishes successfully, fails, or gets cancelled
→ parent and children affect each other

The key concepts are:

  1. Scope owns the coroutine
  2. Job represents its lifecycle
  3. Suspension is not blocking
  4. Cancellation is cooperative
  5. Parents wait for children
  6. Parent cancellation cancels children
  7. Child failure usually cancels the parent

How do I cancel a coroutine in Kotlin safely?

In Kotlin, you cancel a coroutine safely by calling cancel() on its Job or scope, and making sure the coroutine code is cooperative: it should suspend regularly or check for cancellation.

Basic example

import kotlinx.coroutines.*

fun main() = runBlocking {
    val job = launch {
        try {
            repeat(1_000) { i ->
                println("Working $i")
                delay(500) // cancellable suspension point
            }
        } finally {
            println("Cleaning up")
        }
    }

    delay(1_500)
    job.cancel()
    job.join()

    println("Cancelled safely")
}

Or more commonly:

job.cancelAndJoin()
job.cancelAndJoin()

This cancels the coroutine and waits until it finishes cleanup.

Use cancellable suspending functions

Most kotlinx.coroutines suspending functions are cancellable, for example:

delay(...)
withContext(...)
receive(...)
send(...)

So this is usually safe:

val job = scope.launch {
    while (true) {
        delay(1000)
        doWork()
    }
}

job.cancel()

For CPU-heavy loops, check cancellation manually

If your coroutine does not suspend often, cancellation will not be noticed immediately.

Use isActive:

val job = scope.launch {
    while (isActive) {
        doCpuWorkChunk()
    }
}

Or call ensureActive():

val job = scope.launch {
    while (true) {
        ensureActive()
        doCpuWorkChunk()
    }
}

Cleanup with finally

Use try/finally for cleanup:

val job = scope.launch {
    try {
        doWork()
    } finally {
        closeResources()
    }
}

If cleanup needs to call suspending functions, use NonCancellable:

val job = scope.launch {
    try {
        doWork()
    } finally {
        withContext(NonCancellable) {
            saveState()
            closeRemoteConnection()
        }
    }
}

Do not swallow CancellationException

Cancellation is represented by CancellationException. Avoid catching it accidentally and ignoring it.

Bad:

try {
    doWork()
} catch (e: Exception) {
    // This also catches CancellationException
    logError(e)
}

Better:

try {
    doWork()
} catch (e: CancellationException) {
    throw e
} catch (e: Exception) {
    logError(e)
}

Cancel a scope

If you created a scope, cancel it when the owner is destroyed:

class Repository {
    private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)

    fun start() {
        scope.launch {
            doWork()
        }
    }

    fun close() {
        scope.cancel()
    }
}

Summary

Use:

job.cancelAndJoin()

Make your coroutine cooperative by using:

delay(...)
isActive
ensureActive()

Clean up with:

try {
    // work
} finally {
    // cleanup
}

And avoid swallowing CancellationException.

How do I run coroutines in parallel using async and await in Kotlin?

In Kotlin, use async to start concurrent work inside a coroutine scope, then call await() to get each result.

Basic example:

import kotlinx.coroutines.*

suspend fun fetchUser(): String {
    delay(1000)
    return "User"
}

suspend fun fetchPosts(): List<String> {
    delay(1000)
    return listOf("Post 1", "Post 2")
}

fun main() = runBlocking {
    val userDeferred = async {
        fetchUser()
    }

    val postsDeferred = async {
        fetchPosts()
    }

    val user = userDeferred.await()
    val posts = postsDeferred.await()

    println(user)
    println(posts)
}

Here, fetchUser() and fetchPosts() run in parallel. Since both delay for 1 second, the total time is about 1 second instead of 2.

You can also await multiple results with awaitAll:

import kotlinx.coroutines.*

fun main() = runBlocking {
    val deferredResults = listOf(
        async {
            delay(1000)
            "Result 1"
        },
        async {
            delay(1000)
            "Result 2"
        },
        async {
            delay(1000)
            "Result 3"
        }
    )

    val results = deferredResults.awaitAll()

    println(results)
}

Output:

[Result 1, Result 2, Result 3]

A common pattern is:

coroutineScope {
    val a = async { doWorkA() }
    val b = async { doWorkB() }

    val resultA = a.await()
    val resultB = b.await()

    combine(resultA, resultB)
}

For example:

import kotlinx.coroutines.*

suspend fun loadName(): String {
    delay(500)
    return "Alice"
}

suspend fun loadAge(): Int {
    delay(500)
    return 30
}

suspend fun loadProfile(): String = coroutineScope {
    val name = async { loadName() }
    val age = async { loadAge() }

    "${name.await()} is ${age.await()} years old"
}

fun main() = runBlocking {
    println(loadProfile())
}

Important notes:

  • async returns a Deferred<T>.
  • await() suspends until the result is ready.
  • async should usually be called inside coroutineScope, supervisorScope, runBlocking, or another coroutine.
  • If one async child fails inside coroutineScope, the whole scope is cancelled.
  • Use async for concurrent computations that return values.
  • Use launch for concurrent work that does not return a value.

If you are doing blocking I/O, use an appropriate dispatcher:

val data = async(Dispatchers.IO) {
    blockingNetworkCall()
}

For CPU-heavy work:

val result = async(Dispatchers.Default) {
    heavyCalculation()
}

How do I write a suspend function in Kotlin?

In Kotlin, a suspend function is declared with the suspend modifier.

suspend fun fetchUser(): User {
    // Can call other suspend functions here
    return api.getUser()
}

A suspend function can pause without blocking the thread and later resume. It is commonly used with coroutines for asynchronous work.

Example:

import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking

suspend fun greetAfterDelay() {
    delay(1000)
    println("Hello after 1 second")
}

fun main() = runBlocking {
    greetAfterDelay()
}

Key points:

  • Use suspend fun to define one.
  • A suspend function can call other suspend functions.
  • It must be called from:
    • another suspend function, or
    • a coroutine scope such as runBlocking, launch, or async.

Example with return value:

suspend fun loadMessage(): String {
    delay(500)
    return "Loaded message"
}

fun main() = runBlocking {
    val message = loadMessage()
    println(message)
}

So the basic syntax is:

suspend fun functionName() {
    // suspending work
}

How do I use delay instead of Thread.sleep in Kotlin coroutines?

Use kotlinx.coroutines.delay(...) inside a coroutine instead of Thread.sleep(...).

Thread.sleep blocks the current thread. delay suspends the coroutine without blocking the thread, so other coroutines can keep running.

import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    println("Before delay")

    delay(1_000) // suspends for 1 second, does not block the thread

    println("After delay")
}

If you currently have:

Thread.sleep(1000)

replace it with:

delay(1000)

But note: delay is a suspend function, so it can only be called from another suspend function or from inside a coroutine builder such as launch, async, or runBlocking.

Example with launch:

import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    launch {
        delay(1000)
        println("Coroutine finished after 1 second")
    }

    println("This prints immediately")
}

Example in a suspend function:

import kotlinx.coroutines.delay

suspend fun doWork() {
    delay(500)
    println("Work done")
}

Avoid doing this in coroutines:

Thread.sleep(1000) // blocks the underlying thread

Prefer:

delay(1000) // suspends only the coroutine

How do I launch a coroutine in Kotlin using launch and runBlocking?

In Kotlin, you can use runBlocking to start a coroutine scope that blocks the current thread until its coroutines finish, and launch to start a new coroutine inside that scope.

import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    launch {
        println("Coroutine is running")
    }

    println("Main coroutine continues")
}

Output may look like:

Main coroutine continues
Coroutine is running

runBlocking creates a coroutine and blocks the current thread until all child coroutines complete.

launch starts a new coroutine that runs concurrently with the rest of the code inside the runBlocking scope.

A slightly clearer example:

import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    launch {
        delay(1000)
        println("World!")
    }

    println("Hello")
}

Output:

Hello
World!

Here:

  • runBlocking { ... } starts a blocking coroutine scope.
  • launch { ... } starts a child coroutine.
  • delay(1000) suspends the coroutine for 1 second without blocking the thread.
  • runBlocking waits until the launched coroutine finishes before main exits.

How do I optimize performance and readability when using null safety in deeply nested data?

When working with deeply nested nullable data in Kotlin, the goal is to keep code both safe and understandable without building long, fragile chains or overusing !!.

1. Avoid very long safe-call chains when they hide meaning

This is safe:

val city = user?.profile?.address?.city ?: "Unknown"

For simple reads, that is perfectly fine.

But if the chain becomes long or has business meaning, split it into named intermediate values:

val profile = user?.profile
val address = profile?.address
val city = address?.city ?: "Unknown"

This is often easier to debug and read, especially when each level has meaning.

2. Prefer early returns for required nested values

If several nested values are required, avoid deeply nested let blocks:

fun sendEmail(user: User?) {
    val email = user?.profile?.contact?.email ?: return
    val name = user.profile.name ?: "there"

    emailService.send(to = email, subject = "Hello $name")
}

This is usually clearer than:

user?.profile?.contact?.email?.let { email ->
    user.profile.name?.let { name ->
        emailService.send(to = email, subject = "Hello $name")
    }
}

Use return, continue, break, or throw with Elvis when absence should stop processing:

val id = request?.user?.id ?: return
val token = request.auth?.token ?: throw IllegalArgumentException("Missing token")

3. Use let sparingly and name the value

let is useful when you want to run code only if a value is non-null. For nested data, avoid stacking anonymous its.

Prefer this:

user?.profile?.contact?.email?.let { email ->
    sendVerificationEmail(email)
}

Avoid this:

user?.let {
    it.profile?.let {
        it.contact?.let {
            it.email?.let {
                sendVerificationEmail(it)
            }
        }
    }
}

Nested it quickly becomes unreadable. Use explicit names:

user?.let { user ->
    user.profile?.let { profile ->
        profile.contact?.let { contact ->
            contact.email?.let { email ->
                sendVerificationEmail(email)
            }
        }
    }
}

Even then, if nesting grows, early returns are usually better.

4. Use defaults at the boundary

If your app can safely treat missing nested data as a default, normalize it early.

val displayName = user?.profile?.displayName ?: "Guest"
val avatarUrl = user?.profile?.avatarUrl ?: DEFAULT_AVATAR_URL
val roles = user?.permissions?.roles.orEmpty()

For collections, orEmpty() is especially readable:

for (order in user?.orders.orEmpty()) {
    process(order)
}

This avoids repeated null checks.

5. Convert messy external data into clean internal models

Deep nullability often comes from APIs, databases, JSON, or maps. Instead of spreading null handling throughout your code, convert once near the boundary.

data class ApiUser(
    val profile: ApiProfile?
)

data class ApiProfile(
    val displayName: String?,
    val email: String?
)

data class User(
    val displayName: String,
    val email: String?
)

fun ApiUser.toDomain(): User {
    return User(
        displayName = profile?.displayName ?: "Guest",
        email = profile?.email
    )
}

Then the rest of your code works with a cleaner model:

fun render(user: User) {
    println(user.displayName)
}

This improves both performance and readability because null checks are centralized.

6. Avoid !! in nested data

This is fragile:

val city = user!!.profile!!.address!!.city!!

It may be short, but it is not safe. If any level is null, it crashes with little context.

If the value is truly required, fail with a meaningful message:

val city = user?.profile?.address?.city
    ?: error("User city is required")

or:

val city = requireNotNull(user?.profile?.address?.city) {
    "User city is required"
}

Use this when null means a programmer error or invalid state.

7. Prefer mapNotNull and filterNotNull for nested collections

For nested nullable values in collections, avoid manual loops with multiple checks.

val emails = users
    .mapNotNull { user -> user.profile?.contact?.email }

For nullable lists:

val emails = users
    .orEmpty()
    .mapNotNull { user -> user.profile?.contact?.email }

For nullable elements:

val names = users
    .filterNotNull()
    .mapNotNull { user -> user.profile?.displayName }

This is concise and usually efficient enough for normal application code.

8. Be careful with repeated expensive calls

Safe-call chains are cheap when they access properties. But avoid repeating function calls that may be expensive or have side effects:

val city = repository.getUser()?.profile?.address?.city
val country = repository.getUser()?.profile?.address?.country

Better:

val address = repository.getUser()?.profile?.address
val city = address?.city
val country = address?.country

This improves performance and avoids inconsistent results if the function returns different data each time.

9. Use local variables to benefit from smart casts

Kotlin smart casts work best with stable local values.

val profile = user.profile

if (profile != null) {
    println(profile.displayName)
    println(profile.email)
}

This is often clearer than repeating:

println(user.profile?.displayName)
println(user.profile?.email)

Especially when you need several fields from the same nullable object.

10. For maps, distinguish missing keys from null values when needed

Nested maps can become confusing because a map lookup returns nullable values.

val city = users["ana"]?.get("address")?.get("city") ?: "Unknown"

This is fine if missing and null mean the same thing.

If they do not, check explicitly:

val userData = users["ana"]

if (userData != null && userData.containsKey("city")) {
    val city = userData["city"]
    println("City key exists with value: $city")
}

Practical rule of thumb

Use this progression:

  1. Simple optional read → safe-call chain
    val value = a?.b?.c ?: default
    
  2. Required value → Elvis with return, throw, or error
    val value = a?.b?.c ?: return
    
  3. Several fields from the same nullable object → local variable + null check
    val profile = user?.profile ?: return
    println(profile.name)
    println(profile.email)
    
  4. CollectionsorEmpty(), mapNotNull, filterNotNull
    val ids = users.orEmpty().mapNotNull { it.id }
    
  5. Deeply nullable external data → normalize into a cleaner model early
    val domainUser = apiUser.toDomain()
    

In short: safe-call chains are fine for simple reads, early returns are best for required nested data, named variables improve readability, and boundary mapping keeps null complexity from spreading through your code.

How do I use scope functions in a functional reactive context with Kotlin Flows?

In Kotlin Flow code, scope functions are useful, but they should usually play a supporting role. The main structure of your reactive pipeline should come from Flow operators such as map, filter, flatMapLatest, combine, onEach, catch, and stateIn.

A good rule of thumb:

Flow operators describe the stream.
Scope functions describe what you do with each value.

1. Use map for stream transformation, let for local value transformation

If you are transforming each emitted value, the outer operation should usually be map.

val userNames: Flow<String> =
    usersFlow.map { user ->
        user.let {
            "${it.firstName} ${it.lastName}"
        }
    }

In simple cases, let may be unnecessary:

val userNames: Flow<String> =
    usersFlow.map { user ->
        "${user.firstName} ${user.lastName}"
    }

Use let inside map when it clarifies a local transformation, especially for nullable values or multistep conversion.

val profileNames: Flow<String> =
    usersFlow.map { user ->
        user.profile?.let { profile ->
            profile.displayName
        } ?: "Anonymous"
    }

2. Use onEach for stream side effects, not also as the main Flow operator

For logging, analytics, caching, or debugging, prefer onEach.

val users: Flow<List<User>> =
    userRepository.users()
        .onEach { users ->
            logger.info("Loaded ${users.size} users")
        }

Inside a transformation, also can be fine when you want to return the same value after a local side effect:

val users: Flow<List<User>> =
    userRepository.users()
        .map { users ->
            users.filter { it.isActive }
                .also { activeUsers ->
                    logger.debug("Active users: ${activeUsers.size}")
                }
        }

But avoid using also where onEach expresses the intent better:

val users: Flow<List<User>> =
    userRepository.users()
        .onEach { logger.debug("Received users: $it") }
        .map { users -> users.filter { it.isActive } }

3. Use run when computing one result from an emitted object

run is useful when each emitted value needs a multistep computation.

val summaries: Flow<UserSummary> =
    usersFlow.map { user ->
        user.run {
            val fullName = "$firstName $lastName"
            val status = if (isActive) "active" else "inactive"

            UserSummary(
                id = id,
                name = fullName,
                status = status
            )
        }
    }

This works well when you want receiver-style access with this.

4. Use apply when constructing objects inside a Flow

apply is useful for configuring a mutable object before emitting or returning it.

val requests: Flow<Request> =
    userIds.map { userId ->
        Request().apply {
            method = "GET"
            path = "/users/$userId"
            headers["Accept"] = "application/json"
        }
    }

That said, in reactive code, immutable data classes are often clearer:

val requests: Flow<Request> =
    userIds.map { userId ->
        Request(
            method = "GET",
            path = "/users/$userId",
            headers = mapOf("Accept" to "application/json")
        )
    }

Use apply mainly when an API requires mutable configuration.

5. Use with sparingly inside Flow chains

with can be useful when working with an existing object, but nested receivers can become confusing inside Flow pipelines.

val messages: Flow<String> =
    events.map { event ->
        with(event.metadata) {
            "source=$source, timestamp=$timestamp"
        }
    }

This is fine if the receiver is obvious. But if you already have multiple nested lambdas, explicit names may be clearer:

val messages: Flow<String> =
    events.map { event ->
        val metadata = event.metadata
        "source=${metadata.source}, timestamp=${metadata.timestamp}"
    }

6. Be careful with nested it

Flow pipelines often contain nested lambdas. Scope functions can make that worse if every lambda uses implicit it.

Harder to read:

val result: Flow<List<String>> =
    usersFlow.map {
        it.filter {
            it.isActive
        }.map {
            it.name
        }
    }

Clearer:

val result: Flow<List<String>> =
    usersFlow.map { users ->
        users.filter { user ->
            user.isActive
        }.map { user ->
            user.name
        }
    }

This matters even more with scope functions:

val result: Flow<UserDto> =
    usersFlow.map { user ->
        user.profile?.let { profile ->
            UserDto(
                id = user.id,
                displayName = profile.displayName
            )
        } ?: UserDto(
            id = user.id,
            displayName = "Anonymous"
        )
    }

Prefer named lambda parameters when combining Flow operators and scope functions.

7. Use takeIf / takeUnless with care

Although not scope functions in the same group, takeIf and takeUnless often appear with let.

For simple filtering, prefer Flow’s filter:

val activeUsers: Flow<User> =
    usersFlow.filter { user ->
        user.isActive
    }

Instead of:

val activeUsers: Flow<User> =
    usersFlow.mapNotNull { user ->
        user.takeIf { it.isActive }
    }

But takeIf can be useful when a transformation may produce null:

val validEmails: Flow<String> =
    usersFlow.mapNotNull { user ->
        user.email
            ?.takeIf { email -> email.contains("@") }
            ?.lowercase()
    }

8. Use mapNotNull with let for nullable values

This is a widespread Flow pattern.

val avatars: Flow<Avatar> =
    usersFlow.mapNotNull { user ->
        user.avatarUrl?.let { url ->
            Avatar(url)
        }
    }

Or:

val displayNames: Flow<String> =
    usersFlow.mapNotNull { user ->
        user.profile?.displayName
    }

Use let when constructing a result from a nullable value is more involved.

9. Use flatMapLatest when the scope contains another Flow

If the transformation returns another Flow, do not use only let or map unless you intentionally want a nested Flow<Flow<T>>.

Usually:

val userDetails: Flow<UserDetails> =
    selectedUserId
        .filterNotNull()
        .flatMapLatest { userId ->
            userRepository.observeUserDetails(userId)
        }

If the ID is nullable, and you need fallback behavior:

val userDetails: Flow<UserDetails?> =
    selectedUserId.flatMapLatest { userId ->
        userId?.let {
            userRepository.observeUserDetails(it)
        } ?: flowOf(null)
    }

Here, let is handling the nullable value, while flatMapLatest handles the reactive flattening.

10. Prefer Flow operators for lifecycle and errors

Use catch, onStart, onCompletion, and retry rather than trying to encode those behaviors with scope functions.

val uiState: Flow<UiState> =
    userRepository.users()
        .map { users ->
            UiState.Success(users)
        }
        .onStart {
            emit(UiState.Loading)
        }
        .catch { throwable ->
            emit(UiState.Error(throwable.message ?: "Unknown error"))
        }

Scope functions can still help locally:

val uiState: Flow<UiState> =
    userRepository.users()
        .map { users ->
            users
                .filter { user -> user.isActive }
                .let { activeUsers -> UiState.Success(activeUsers) }
        }
        .onStart {
            emit(UiState.Loading)
        }
        .catch { throwable ->
            emit(UiState.Error(throwable.message ?: "Unknown error"))
        }

Practical mapping

Intent in Flow code Prefer Scope function role
Transform each emission map Use let/run inside if helpful
Remove nulls filterNotNull, mapNotNull Use let for nullable conversion
Side effect per emission onEach Use also only locally
Build/configure object map + constructor or apply apply for mutable setup
Switch to another Flow flatMapLatest, flatMapConcat, flatMapMerge Use let for nullable branch
Combine streams combine, zip Scope functions only inside result builder
Handle errors catch, retry Scope functions rarely needed
Emit loading state onStart Scope functions rarely needed

Example: realistic UI state pipeline

val uiState: StateFlow<UserUiState> =
    selectedUserId
        .filterNotNull()
        .flatMapLatest { userId ->
            userRepository.observeUser(userId)
        }
        .map { user ->
            user.run {
                UserUiState.Content(
                    id = id,
                    title = "$firstName $lastName",
                    subtitle = email ?: "No email"
                )
            }
        }
        .onEach { state ->
            analytics.logScreenState(state)
        }
        .catch { throwable ->
            emit(UserUiState.Error(throwable.message ?: "Unable to load user"))
        }
        .stateIn(
            scope = viewModelScope,
            started = SharingStarted.WhileSubscribed(5_000),
            initialValue = UserUiState.Loading
        )

Here:

  • filterNotNull handles nullable IDs.
  • flatMapLatest switches to the latest selected user stream.
  • run computes a UI model from a User.
  • onEach performs a side effect.
  • catch handles errors.
  • stateIn turns the cold flow into a StateFlow.

Main guideline

Use scope functions in Flow pipelines when they improve the readability of local value handling.

Avoid using them to replace Flow operators.

Good:
Flow operators for stream behavior.
Scope functions for per-value clarity.

Risky:
Long chains of map/let/also/run with nested it everywhere.

If the chain starts becoming hard to read, introduce named lambda parameters or local variables.

How do I avoid nullable types in complex data models using sealed classes and Result wrappers?

You can avoid “nullable everywhere” in complex data models by making missing/invalid/loading/error states explicit in the type system instead of representing them with null.

In Kotlin, the usual tools are:

  1. Sealed classes/interfaces for domain states and variants.
  2. Result<T>-style wrappers for success/failure.
  3. Non-nullable data classes for valid, fully constructed domain objects.
  4. Mapping layers from nullable external DTOs into safe domain models.

1. Avoid nullable domain fields

Instead of this:

data class User(
    val id: String?,
    val name: String?,
    val email: String?,
    val subscription: Subscription?
)

Prefer making the valid domain model non-nullable:

data class User(
    val id: UserId,
    val name: UserName,
    val email: Email,
    val subscription: SubscriptionState
)

@JvmInline
value class UserId(val value: String)

@JvmInline
value class UserName(val value: String)

@JvmInline
value class Email(val value: String)

Now User represents a valid user, not a partially valid object.


2. Use sealed classes for optional-like domain states

If a subscription can be absent, do not use:

val subscription: Subscription?

Use an explicit state:

sealed interface SubscriptionState {
    data object None : SubscriptionState

    data class Active(
        val plan: Plan,
        val renewalDate: RenewalDate
    ) : SubscriptionState

    data class Cancelled(
        val cancelledAt: CancelledAt
    ) : SubscriptionState
}

Then your model becomes:

data class User(
    val id: UserId,
    val name: UserName,
    val email: Email,
    val subscription: SubscriptionState
)

This avoids ambiguity:

subscription == null

could mean:

  • not loaded
  • user has no subscription
  • API forgot to send it
  • parsing failed
  • permission denied

A sealed class makes each state explicit.


3. Use sealed classes for loading/error states

Avoid UI or repository models like this:

data class UserScreenState(
    val user: User?,
    val isLoading: Boolean,
    val error: Throwable?
)

This allows invalid combinations:

user != null && isLoading == true && error != null

Instead:

sealed interface UserScreenState {
    data object Loading : UserScreenState

    data class Loaded(
        val user: User
    ) : UserScreenState

    data class Failed(
        val error: UserError
    ) : UserScreenState
}

Now impossible states are unrepresentable.

Usage:

fun render(state: UserScreenState) {
    when (state) {
        UserScreenState.Loading -> showLoading()

        is UserScreenState.Loaded -> showUser(state.user)

        is UserScreenState.Failed -> showError(state.error)
    }
}

No nullable checks needed.


4. Use Result wrappers for operations

For repository/service calls, avoid:

suspend fun getUser(id: String): User?

because null does not explain what happened.

Prefer:

suspend fun getUser(id: UserId): Result<User>

Usage:

val result = repository.getUser(userId)

result
    .onSuccess { user ->
        showUser(user)
    }
    .onFailure { throwable ->
        showError(throwable)
    }

However, Kotlin’s built-in Result<T> uses Throwable for failure. For richer domain errors, a custom result type is often better.


5. Prefer a custom domain Result for complex models

For complex systems, define your own result wrapper:

sealed interface AppResult<out T, out E> {
    data class Success<T>(
        val value: T
    ) : AppResult<T, Nothing>

    data class Failure<E>(
        val error: E
    ) : AppResult<Nothing, E>
}

Example domain errors:

sealed interface UserError {
    data object NotFound : UserError
    data object Unauthorized : UserError

    data class InvalidResponse(
        val reason: String
    ) : UserError

    data class NetworkFailure(
        val cause: Throwable
    ) : UserError
}

Repository:

interface UserRepository {
    suspend fun getUser(id: UserId): AppResult<User, UserError>
}

Usage:

when (val result = repository.getUser(userId)) {
    is AppResult.Success -> {
        val user = result.value
        showUser(user)
    }

    is AppResult.Failure -> {
        when (val error = result.error) {
            UserError.NotFound -> showNotFound()
            UserError.Unauthorized -> showUnauthorized()
            is UserError.InvalidResponse -> showInvalidResponse(error.reason)
            is UserError.NetworkFailure -> showNetworkError(error.cause)
        }
    }
}

This avoids both nullable success values and ambiguous failures.


6. Convert nullable DTOs at the boundary

External APIs, databases, and JSON often contain nullable fields. Keep that nullability in DTOs only.

Example DTO:

data class UserDto(
    val id: String?,
    val name: String?,
    val email: String?,
    val subscription: SubscriptionDto?
)

Then map to a safe domain model:

fun UserDto.toDomain(): AppResult<User, UserError> {
    val id = id ?: return AppResult.Failure(
        UserError.InvalidResponse("Missing user id")
    )

    val name = name ?: return AppResult.Failure(
        UserError.InvalidResponse("Missing user name")
    )

    val email = email ?: return AppResult.Failure(
        UserError.InvalidResponse("Missing user email")
    )

    return AppResult.Success(
        User(
            id = UserId(id),
            name = UserName(name),
            email = Email(email),
            subscription = subscription.toDomainState()
        )
    )
}

Subscription mapping:

fun SubscriptionDto?.toDomainState(): SubscriptionState {
    if (this == null) {
        return SubscriptionState.None
    }

    return when (status) {
        "active" -> SubscriptionState.Active(
            plan = Plan(planName),
            renewalDate = RenewalDate(renewalDate)
        )

        "cancelled" -> SubscriptionState.Cancelled(
            cancelledAt = CancelledAt(cancelledAt)
        )

        else -> SubscriptionState.None
    }
}

In stricter systems, unknown statuses should return an error instead of None.


7. Model “not loaded” separately from “empty”

A common mistake is using nullable fields for lazy or partial loading:

data class Profile(
    val user: User,
    val orders: List<Order>?
)

Does orders == null mean “not loaded”, “failed”, or “user has no orders”?

Use a sealed class:

sealed interface LoadState<out T> {
    data object NotLoaded : LoadState<Nothing>
    data object Loading : LoadState<Nothing>

    data class Loaded<T>(
        val value: T
    ) : LoadState<T>

    data class Failed(
        val error: DomainError
    ) : LoadState<Nothing>
}

Then:

data class Profile(
    val user: User,
    val orders: LoadState<List<Order>>
)

An empty list now means truly loaded and empty:

Profile(
    user = user,
    orders = LoadState.Loaded(emptyList())
)

8. Use domain-specific alternatives to nullable primitives

Instead of:

data class Product(
    val discountPercent: Int?
)

Use:

sealed interface Discount {
    data object None : Discount

    data class Percentage(
        val value: Int
    ) : Discount
}

Then:

data class Product(
    val id: ProductId,
    val price: Money,
    val discount: Discount
)

This is clearer than checking whether discountPercent is null.


9. Combine sealed classes and result wrappers

A good pattern is:

sealed interface DataState<out T, out E> {
    data object Idle : DataState<Nothing, Nothing>
    data object Loading : DataState<Nothing, Nothing>

    data class Success<T>(
        val value: T
    ) : DataState<T, Nothing>

    data class Failure<E>(
        val error: E
    ) : DataState<Nothing, E>
}

Example:

data class UserViewModelState(
    val user: DataState<User, UserError>
)

Rendering:

fun render(state: UserViewModelState) {
    when (val userState = state.user) {
        DataState.Idle -> showIdle()
        DataState.Loading -> showLoading()

        is DataState.Success -> {
            showUser(userState.value)
        }

        is DataState.Failure -> {
            showUserError(userState.error)
        }
    }
}

10. Practical rule of thumb

Use nullable types only when null has exactly one obvious meaning.

Nullable may be okay here:

val middleName: String?

because “person has no middle name” is often obvious.

But avoid nullable here:

val user: User?
val error: Throwable?
val status: String?
val payment: Payment?
val permissions: List<Permission>?

because these often have multiple possible meanings.


Recommended structure

// External layer
data class UserDto(
    val id: String?,
    val name: String?,
    val email: String?
)

// Domain layer
data class User(
    val id: UserId,
    val name: UserName,
    val email: Email
)

sealed interface UserError {
    data object NotFound : UserError
    data object Unauthorized : UserError
    data class InvalidResponse(val reason: String) : UserError
}

sealed interface AppResult<out T, out E> {
    data class Success<T>(val value: T) : AppResult<T, Nothing>
    data class Failure<E>(val error: E) : AppResult<Nothing, E>
}

// Mapping boundary
fun UserDto.toDomain(): AppResult<User, UserError> {
    val id = id ?: return AppResult.Failure(
        UserError.InvalidResponse("Missing id")
    )

    val name = name ?: return AppResult.Failure(
        UserError.InvalidResponse("Missing name")
    )

    val email = email ?: return AppResult.Failure(
        UserError.InvalidResponse("Missing email")
    )

    return AppResult.Success(
        User(
            id = UserId(id),
            name = UserName(name),
            email = Email(email)
        )
    )
}

Summary

To avoid nullable types in complex data models:

  • Keep nullable fields in DTOs, not domain models.
  • Convert DTOs into non-null domain models at boundaries.
  • Use sealed classes for meaningful states.
  • Use Result or custom AppResult<T, E> for success/failure.
  • Model loading, missing, empty, failed, and unauthorized as separate states.
  • Make invalid states impossible to represent.

The core idea is:

// Avoid
val user: User?
val error: Throwable?

// Prefer
sealed interface UserState {
    data object Loading : UserState
    data class Loaded(val user: User) : UserState
    data class Failed(val error: UserError) : UserState
}

How do I use smart casting and flow control to eliminate redundant null checks in Kotlin?

In Kotlin, you can eliminate redundant null checks by letting the compiler smart cast a nullable value after you prove it is not null.

Basic smart cast

fun printLength(text: String?) {
    if (text != null) {
        println(text.length)
    }
}

Inside the if block, Kotlin knows text cannot be null, so it treats it as a non-null String.

You do not need this:

fun printLength(text: String?) {
    if (text != null) {
        if (text != null) {
            println(text.length)
        }
    }
}

The second check is redundant.

Use early returns for cleaner flow

A common Kotlin style is to return early when the value is null:

fun printLength(text: String?) {
    if (text == null) return

    println(text.length)
}

After the return, Kotlin knows that text must be non-null for the rest of the function.

This is useful when you want to avoid nesting:

fun processUserName(name: String?) {
    if (name == null) return

    println(name.uppercase())
    println(name.length)
}

Use Elvis with return

You can also combine the Elvis operator ?: with return:

fun processUserName(name: String?) {
    val nonNullName = name ?: return

    println(nonNullName.uppercase())
    println(nonNullName.length)
}

Here, if name is null, the function returns immediately. Otherwise, nonNullName is a non-null String.

Use Elvis with default values

If you want to continue with a fallback value instead of returning:

fun printLength(text: String?) {
    val value = text ?: ""

    println(value.length)
}

value is always a non-null String.

Use let for nullable scoped work

Use ?.let when you only want to run code if the value is non-null:

fun printLength(text: String?) {
    text?.let { nonNullText ->
        println(nonNullText.length)
    }
}

Inside the let block, nonNullText is non-null.

Smart casts with type checks

Smart casts also work with is checks:

fun printIfString(value: Any?) {
    if (value is String) {
        println(value.length)
    }
}

Inside the block, value is treated as String.

You can also invert the check:

fun printIfString(value: Any?) {
    if (value !is String) return

    println(value.length)
}

After the early return, Kotlin knows value is a String.

Combine conditions safely

Kotlin understands flow control in boolean expressions:

fun printLength(text: String?) {
    if (text != null && text.length > 3) {
        println(text.uppercase())
    }
}

Because text != null is checked first, text.length is safe.

This does not work if the order is reversed:

fun printLength(text: String?) {
    if (text.length > 3 && text != null) {
        println(text.uppercase())
    }
}

That fails because text.length is accessed before the null check.

Prefer immutable values

Smart casts work best with val values:

val name: String? = getName()

if (name != null) {
    println(name.length)
}

They may not work with mutable properties because the value could change between the check and the use:

var name: String? = getName()

if (name != null) {
    println(name.length)
}

Local var variables can sometimes be smart cast if the compiler can prove they are not modified, but mutable properties are more limited.

For properties, copy the value into a local val:

class User(var name: String?)

fun printUserName(user: User) {
    val name = user.name

    if (name != null) {
        println(name.length)
    }
}

Avoid !!

Instead of writing:

fun printLength(text: String?) {
    if (text != null) {
        println(text!!.length)
    }
}

write:

fun printLength(text: String?) {
    if (text != null) {
        println(text.length)
    }
}

The !! is unnecessary because smart casting already made text non-null.

Practical pattern

A concise, idiomatic pattern is:

fun handle(input: String?) {
    val text = input ?: return

    println(text.trim())
    println(text.length)
}

Use:

  • if (x != null) when you want a guarded block.
  • if (x == null) return when you want to avoid nesting.
  • val y = x ?: return when you want a non-null local variable.
  • x?.let { ... } when the work should happen only if x is non-null.
  • ?: defaultValue when you want to replace null with a fallback.