kotlin-style-guide icon indicating copy to clipboard operation
kotlin-style-guide copied to clipboard

Function signature formatting

Open cypressious opened this issue 9 years ago • 8 comments
trafficstars

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
}

cypressious avatar Jun 01 '16 14:06 cypressious

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

udalov avatar Jun 01 '16 15:06 udalov

Fixed it

cypressious avatar Jun 01 '16 16:06 cypressious

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

voddan avatar Jun 08 '16 08:06 voddan

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

damianw avatar Jun 08 '16 17:06 damianw

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.

yole avatar Jun 12 '16 09:06 yole

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.

elizarov avatar Nov 08 '16 09:11 elizarov

@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?

xenomachina avatar Feb 23 '17 02:02 xenomachina

Is this resolved? In Docs In Source

magneticflux- avatar Jan 17 '18 22:01 magneticflux-