Skip to content

Files

Latest commit

 

History

History
33 lines (24 loc) · 743 Bytes

ThrowsCount.md

File metadata and controls

33 lines (24 loc) · 743 Bytes

Pattern: Too many throw statements

Issue: -

Description

Functions should have clear throw statements. Functions with many throw statements can be harder to read and lead to confusion. Instead prefer to limit the amount of throw statements in a function.

Example of incorrect code:

fun foo(i: Int) {
    when (i) {
        1 -> throw IllegalArgumentException()
        2 -> throw IllegalArgumentException()
        3 -> throw IllegalArgumentException()
    }
}

Example of correct code:

fun foo(i: Int) {
    when (i) {
        1,2,3 -> throw IllegalArgumentException()
    }
}

Further Reading