Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Lambda formatting #10

Open
yole opened this issue May 31, 2016 · 6 comments
Open

Lambda formatting #10

yole opened this issue May 31, 2016 · 6 comments

Comments

@yole
Copy link
Contributor

yole commented May 31, 2016

When possible, put lambdas on a single line, using it for single-parameter lambdas:

elements.dropWhile { it !is KDoc }

Don't use it in nested lambdas; always declare parameter names explicitly.

When declaring parameter names in a multiline lambda, put the names on the first line, followed by the arrow and the newline:

appendCommaSeparated(properties) { prop ->
    val propertyValue = prop.get(obj)  // ...
}
@voddan
Copy link

voddan commented Jun 8, 2016

Simple collection operations inside nested lambdas are ok to use it in:

appendCommaSeparated(properties) { props ->
    props.map {it.name}
}

@damianw
Copy link

damianw commented Jun 8, 2016

To improve readability, multiline single-argument lambdas should also always name the argument instead of using it, since it can become unclear in longer lambdas.

// Good
appendCommaSeparated(properties) { prop ->
  val propertyValue = prop.get(obj)  // ...
}

// Bad
appendCommaSeparated(properties) {
  val propertyValue = it.get(obj)  // ...
}

@trevjonez
Copy link

what about the case where the method has multiple function args like with RxJava's subscribe method?

someObservable.subscribe { next -> doThing(next) }
someObservable.subscribe({ next -> 
    doThing(next) 
}) { error -> 
    error.printStackTrace() 
}
someObservable.subscribe({ next -> 
    doThing(next) 
}, { error -> 
    error.printStackTrace() 
}) { 
    logger.logStreamCompletion() 
}

Seems to me that once there are multiple lambda arguments, forcing the last one outside of the parenthesis may not be desirable?

someObservable.subscribe({ next ->
    doThing(next)
}, { error ->
    error.printStackTrace()
}, { 
    logger.logStreamCompletion() 
})

@Zhuinden
Copy link

@trevjonez I think at that point it is a better option to use named arguments without trailing lambdas, that way you don't confuse lambda order and keep it readable

@trevjonez
Copy link

trevjonez commented Jun 19, 2019

I do think we both agree that we want to optimize for reading clarity.

Anymore I find I always put it inside parenthesis when a follow up call might look ambiguous as to what it operates on to those with less context of the code.

again with an rxjava usecase as an example.

someSingle().subscribe({ doThings(it) }) { error ->
  logger.log(error)
  throw error
}.trackInMap(someMap, someKey)

is trackInMap operating on the lambda or on the result of subscribe?

Lets adjust the parens and see if it is more clear:

someSingle().subscribe({ doThings(it) }, { error ->
  logger.log(error)
  throw error
}).trackInMap(someMap, someKey)

the compiler can solve it fine either way, but for us humans that parenthesis disambiguates with minimal mental effort.

@Zhuinden
Copy link

Zhuinden commented Jun 19, 2019

Hrmm to be a bit more specific, I think the best practice in this case is to wrap it with an extension function that allows you to use named arguments.

Just like RxKotlin does with subscribeBy.

https://github.com/ReactiveX/RxKotlin/blob/d20fa56bbe72c369b53f553a9710841eca1b63de/src/main/kotlin/io/reactivex/rxkotlin/subscribers.kt#L34-L38

Because now you can do

someSingle().subscribeBy(
    onSuccess = { result -> doThings(result) },
    onError = { throwable -> 
        logger.log(throwable)
        throw throwable
    })

Now you definitely won't get confused about which lambda is for what.

shashachu added a commit to shashachu/ktlint that referenced this issue Jul 3, 2019
It was based on a styleguide discussion (Kotlin/kotlin-style-guide#10) that never made it into the official styleguide.
shashachu added a commit to shashachu/ktlint that referenced this issue Jul 5, 2019
It was based on a styleguide discussion (Kotlin/kotlin-style-guide#10) that never made it into the official styleguide.
shashachu added a commit to pinterest/ktlint that referenced this issue Jul 5, 2019
It was based on a styleguide discussion (Kotlin/kotlin-style-guide#10) that never made it into the official styleguide.
sowmyav24 pushed a commit to sowmyav24/ktlint that referenced this issue Jul 18, 2019
It was based on a styleguide discussion (Kotlin/kotlin-style-guide#10) that never made it into the official styleguide.
orchestr7 pushed a commit to saveourtool/diktat that referenced this issue Jun 29, 2020
It was based on a styleguide discussion (Kotlin/kotlin-style-guide#10) that never made it into the official styleguide.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants