Pattern: Exit outside main()
Issue: -
Flags use of System.exit()
and Kotlin's exitProcess()
when used outside the main
function. This makes code more
difficult to test, causes unexpected behaviour on Android, and is a poor way to signal a failure in the program. In
almost all cases it is more appropriate to throw an exception.
Example of incorrect code:
fun randomFunction() {
val result = doWork()
if (result == FAILURE) {
exitProcess(2)
} else {
exitProcess(0)
}
}
Example of correct code:
fun main() {
val result = doWork()
if (result == FAILURE) {
exitProcess(2)
} else {
exitProcess(0)
}
}