kotlin-guides icon indicating copy to clipboard operation
kotlin-guides copied to clipboard

Be concrete about && and || at the beginning or end of a line

Open vRallev opened this issue 6 years ago • 3 comments

I think Java prefers

if (condition1
    && condition 2
    || condition3) {

    // do something
}

This style would make sense to me in Kotlin as well, but I couldn't find anything regarding to that in the IJ Kotlin style guide or Android style guide. Note that there has been some discussion going on in the ktlint project what prefers

if (condition1 &&
    condition2 ||
    condition3) {

    // do something
}

https://github.com/shyiko/ktlint/issues/168

vRallev avatar Apr 09 '18 21:04 vRallev

Tragically (in my opinion), Kotlin forces you into the latter as otherwise things like

foo
+ bar

could be interpreted as

foo
unaryPlus(bar)

depending on what is in scope.

I thought we covered this somewhere but perhaps it was left ambiguous. Would be good to include.

JakeWharton avatar Apr 09 '18 21:04 JakeWharton

That example doesn't actually apply to the binary boolean operators, but I see no reason to create archaic rules around which binary operators and types you're operating on so we'll normalize to end-of-line rather than start-of-line.

JakeWharton avatar Apr 09 '18 21:04 JakeWharton

I agree that it's really sad. At least I have something else besides ktlint that I could reference. Thanks

vRallev avatar Apr 09 '18 21:04 vRallev