android/kotlin-guides

Prefer parameters with default values last in the parameter list.

Opened this issue · 4 comments

https://android.github.io/kotlin-guides/interop.html#function-overloads-for-defaults
mentions this but in a different context.

This is useful even in pure Kotlin, regardless of Java interop because it doesn't require naming the other parameters at the call site.

data class Pizza(val cheese: Boolean, val toppings: List<String> = listOf())
Pizza(true)

instead of

data class Pizza(val toppings: List<String> = listOf(), val cheese: Boolean)
Pizza(cheese = true)

We can't add this to the style guide because I don't think we can make it a hard rule. But it's a good candidate for a hopefully forthcoming best practices / patterns section.

truuuuue. (feel free to close if this isn't the right place for a best practices request. looking forward to that!)

One notable exception is lambdas. Lambdas are a higher priority for being last in the parameter list.
fun foo(unused: Any? = null, a: (String) -> Unit) {}
foo {} works here without needing to name the a parameter. That is, no need for foo(a = {}).