Skip to content

Files

Latest commit

 

History

History
33 lines (24 loc) · 879 Bytes

UseCheckOrError.md

File metadata and controls

33 lines (24 loc) · 879 Bytes

Pattern: Missing use of check()/error()

Issue: -

Description

Kotlin provides a much more concise way to check invariants as well as pre- and post conditions than to manually throw an IllegalStateException.

Example of incorrect code:

if (value == null) throw new IllegalStateException("value should not be null")
if (value < 0) throw new IllegalStateException("value is $value but should be at least 0")
when(a) {
  1 -> doSomething()
  else -> throw IllegalStateException("Unexpected value")
}

Example of correct code:

checkNotNull(value) {"value should not be null"}
check(value >= 0) { "value is $value but should be at least 0" }
when(a) {
  1 -> doSomething()
  else -> error("Unexpected value")
}

Further Reading