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

Method call formatting

Open yole opened this issue 9 years ago • 6 comments

Use the named argument syntax when a method takes multiple parameters of the same primitive type, or for parameters of Boolean type.

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

Do put spaces around the = sign separating the argument name and value.

yole avatar May 31 '16 18:05 yole

When calling methods that don't fit on a single line, I use the following syntax:

foo(
    bar(
        "a",
        "very long argument"
    ),
    parameterName = 1
)

I like it because it looks like a DSL or a tree, similar to JSON.

cypressious avatar Jun 01 '16 14:06 cypressious

When wrapping a method call, align the parameters on the first one. Group short parameters by meaning:

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

voddan avatar Jun 08 '16 08:06 voddan

Alternative syntax:

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

Even though having the parans on separate lines is a bit more verbose, I like the style better.

cypressious avatar Jun 08 '16 08:06 cypressious

This style is very domain-specific. Look at any reasonable 2D drawing code filled with moveTo and lineTo method invocations. It will become unnecessary verbose if this style is followed.

elizarov avatar Nov 08 '16 16:11 elizarov

I wonder how important this is if IntelliJ's java feature for putting parameter names inline into the editor is carried over to Kotlin.

mikehearn avatar Jan 05 '17 15:01 mikehearn

@mikehearn Will it be, though? The additional readabilty you get by the inlined parameter names can be achieved in kotlin by using named parameters, with the added benefit of managing whether it should be shown or not at the call site, so I see no benefit in it being ported over to kotlin.

roschlau avatar Apr 03 '17 18:04 roschlau