kotlin-style-guide
kotlin-style-guide copied to clipboard
Assignment and a boolean operator
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 ==
Not sure I agree. What's the benefit.
What does doStaff (fun(x: Int) = x == 1)
line mean?
The benefit is in visual separating =
and ==
@igor-korotenko doStaff()
is a method accepting a lambda (edited)
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
Look like a quite cool idea and I see some visual benefits, but most of code we meet every day don't have this.