Kotlin / kotlin-style-guide

Work-in-progress notes for the Kotlin style guide
288 stars 14 forks source link

Function signature formatting #15

Open cypressious opened 8 years ago

cypressious commented 8 years ago

If the function signature doesn't fit on a single line, I propose the following syntax, similar to the class header syntax proposed by me in #2:

fun longMethodName(
    argument: ArgumentType = defaultValue,
    argument2: AnotherArgumentType
): ReturnType {
    // body
}
udalov commented 8 years ago

:+1:, except that there should be no space before the colon

cypressious commented 8 years ago

Fixed it

voddan commented 8 years ago

Since a class primary constructor declaration has a lot in common with a function declaration, I believe the style for a muti-line function header must be the same as for a multi-line class header at #2

damianw commented 8 years ago

How does this relate to #1? Should expression bodies be permitted for long function signatures?

yole commented 8 years ago

I'm not a big fan of the idea of requiring wrapping before the closing parenthesis of a long parameter list, or after the opening one. It's important that parameters should be aligned vertically, but I'd allow more flexibility in how exactly the parameter list is laid out, depending on the lengths of the identifiers involved.

elizarov commented 7 years ago

Notice how the topic starter's example is using 4 spaces for continuation indent. Currently, auto-format will "fix it" to 8 spaces. I'd prefer to see 4 space there a written.

xenomachina commented 7 years ago

@yole If you don't wrap after the open paren you end up with:

fun longMethodName(argument: ArgumentType = defaultValue,
                   argument2: AnotherArgumentType

so parameters after the first are vertically aligned with the character after the open paren. If anything happens that changes the position of that open paren (eg: renaming the method or changing its visibility) the entire parameter list needs to be realigned. Like you said in #23:

The problem with any kind of vertical alignment is that, while it's easy to implement in a batch code formatting tool, it's much more difficult to maintain during regular code editing, both when you initially type a declaration and when you change something.

Doesn't that imply that mutli-line parameter lists should wrap after the open paren so that they can use a standard amount of indent rather than being vertically aligned with the first parameter?

magneticflux- commented 6 years ago

Is this resolved? In Docs In Source