Design calls that explain themselves
Default arguments remove the need for many overloads. Named arguments make same-typed parameters easier to distinguish. Keep defaults predictable: a caller should not need to inspect hidden mutable state to know what an omitted argument means.
fun reminder(topic: String, minutes: Int = 25, urgent: Boolean = false): String {
require(minutes > 0)
val prefix = if (urgent) "Now" else "Next"
return "$prefix: $topic for $minutes minutes"
}
fun main() {
println(reminder("Kotlin"))
println(reminder(topic = "Compose", urgent = true))
}
The second call keeps the default duration while changing urgency. Parameter names become relevant to Kotlin source callers who use named arguments, so renaming a public parameter can break their compilation. Named-argument syntax is generally unavailable for Java methods, whose parameter names may not be retained reliably.
Default expressions run when the argument is omitted at the call, not once when the function is declared.
Exercise
Add a function for scheduling a study session with a default label and duration. Test an explicit duration, omitted duration, and invalid duration.
Check: named arguments should make two consecutive Boolean or integer parameters unnecessary to guess from call-site position.