Use expression bodies for one clear result
An expression body removes braces and return when a function computes one value. Kotlin can infer its return type, but an explicit type is often useful for public APIs and numeric calculations.
fun isComplete(done: Int, total: Int): Boolean = done == total
fun completion(done: Int, total: Int): Double =
if (total > 0) done.toDouble() / total else 0.0
fun main() {
println(completion(3, 4))
}
The result is 0.75. A short function is not automatically a clear function: avoid packing validation, logging, and several side effects into a long chain merely to retain an expression body. A block body is appropriate when intermediate steps deserve names or several early returns simplify the contract.
Notice that isComplete(0, 0) returns true, while completion(0, 0) returns zero by policy. These choices are domain decisions, not properties of expression bodies.
Exercise
Implement remaining(done, total) as an expression body. Decide whether invalid negative counts throw or are clamped. Write examples that demonstrate your decision.
Check: switching between expression and block bodies should not silently change numeric precision, validation, or the public return type.