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

Assignment and a boolean operator

Open voddan opened this issue 8 years ago • 6 comments

In a case of "assignment" syntax with a boolean expression == put brackets around the expression:

Good:

// assignment:
val x: Boolean = (this == other)

// expression body:
fun foo() = (a == b)

// function expression:
 doStaff(fun(x: Int) = (x == 1))

Bad:

val x: Boolean = this == other
fun foo() = a == b
doStaff (fun(x: Int) = x == 1)

The idea is to visual separate = and ==

voddan avatar Jul 01 '16 11:07 voddan

Not sure I agree. What's the benefit.

cypressious avatar Jul 01 '16 11:07 cypressious

What does doStaff (fun(x: Int) = x == 1) line mean?

igor-korotenko avatar Jul 01 '16 11:07 igor-korotenko

The benefit is in visual separating = and ==

voddan avatar Jul 01 '16 11:07 voddan

@igor-korotenko doStaff() is a method accepting a lambda (edited)

voddan avatar Jul 01 '16 11:07 voddan

Also I think the brackets improve similar situations with other comparison operators:

val x = (100 > 200)

vs

val x = 100 > 200

But that does not apply to other operators. Complitelly OK:

val x = 1 + 2
val y = a && b
val x = c or d

voddan avatar Jul 01 '16 11:07 voddan

Look like a quite cool idea and I see some visual benefits, but most of code we meet every day don't have this.

igor-korotenko avatar Jul 01 '16 11:07 igor-korotenko