android / kotlin-guides

A set of guides for writing Kotlin for Android.
https://android.github.io/kotlin-guides/
1.69k stars 134 forks source link

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

Open vRallev opened 6 years ago

vRallev commented 6 years ago

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

JakeWharton commented 6 years ago

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 commented 6 years ago

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.

vRallev commented 6 years ago

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