Return Int instead of Boolean in a function

kokchai
1 min readJan 24, 2021

If there is a function that you want to know it’s true or false, it’s very straightforward to declare it as a Boolean return type. Like,

fun isThatSo(): Boolean {
return when {
dummy01() -> true
dummy02() -> true
somethingWrong() -> false
else -> false
}
}
fun dummy01(): Boolean {}
fun dummy02(): Boolean {}
fun somethingWrong(): Boolean {}

There is an issue, you might lose the information about which dummy() function is called or if anything goes wrong ?

How about I change the function like,

companion object {
const val RESULT_CODE_ISTHATSO = 10
}
fun isThatSo(): Int {
return when {
dummy01() -> RESULT_CODE_ISTHATSO + 1
dummy02() -> RESULT_CODE_ISTHATSO + 2
somethingWrong() -> -(RESULT_CODE_ISTHATSO + 3)
else -> -(RESULT_CODE_ISTHATSO + 4)
}
}
fun dummy01(): Boolean {}
fun dummy02(): Boolean {}
fun somethingWrong(): Boolean {}

I have more information to run in unit test instead of mocking them for each cases and there is some space if I have to extend this function.

--

--