android/kotlin-guides

Clarify formatting for multi-line, single-argument method calls

Opened this issue · 0 comments

The Kotlin language code style documentation makes clear that for multi-line, multi-argument method calls the arguments should appear on their own lines (potentially in groups):

drawSquare(
    x = 10, y = 10,
    width = 100, height = 100,
    fill = true
)

where ( ends the line it is on while the ) appears on its own line. The specific rule is

In long argument lists, put a line break after the opening parenthesis. Indent arguments by 4 spaces. Group multiple closely related arguments on the same line.

Is there any guidance for long method calls that wrap to multiple lines when there is only a single argument?

For example, are both the following correct?

setOnClickListener(object : View.OnClickListener {
    override fun onClick(v: View) {
        //...
     }
})

setOnClickListener(
        object : View.OnClickListener {
            override fun onClick(v: View) {
                //..
            }
         }
)

or is one preferred (ignoring for now that you'd just use a lambda here)? What if there is wrapping involved in the argument value itself? Are each of these valid:

someMethod(callsAVeryVeryLongFunctionWithHorribleName(
        andTonsAndTonsOfParameters,
        andMoreParameters
))

someMethod(
        callsAVeryVeryLongFunctionWithHorribleName(
                 andTonsAndTonsOfParameters,
                 andMoreParameters
        )
)