Return Count

ID

kotlin.return_count

Severity

info

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Kotlin

Tags

complexity, readability

Description

Reports functions that contain more return statements than the configured maximum. Many small early-returns can be perfectly idiomatic, but once a function accumulates many exit points the control flow becomes hard to trace.

Rationale

A function with multiple exit points forces each reader to enumerate every branch to understand the post-condition. While Kotlin makes early-return clean (if (…​) return …​), beyond a threshold the cost of mental tracking outweighs the local readability benefit. Splitting the function or computing a single value through a when/if expression generally improves clarity.

// Bad — five returns
fun classify(n: Int): String {
    if (n < 0) return "neg"
    if (n == 0) return "zero"
    if (n < 10) return "small"
    if (n < 100) return "medium"
    return "big"
}

// Good — single return through when
fun classify(n: Int): String = when {
    n < 0 -> "neg"
    n == 0 -> "zero"
    n < 10 -> "small"
    n < 100 -> "medium"
    else -> "big"
}

Remediation

Use a when expression to compute the result in a single place, or extract the classifying logic into a helper function. Adjust the rule’s max property if your project’s style guide tolerates more returns per function.