Doing JetBrains Kotlin Basics cause and when I do ...
# getting-started
b
Doing JetBrains Kotlin Basics cause and when I do this
val arranged: Boolean = ((h1 >= h2 && h2 >= h3) || (h1 <= h2 && h2 <= h3))
I get effectively marked down for adding the brackets for readability. So is this really affecting performance negatively? I'm a big fan of readability 🙂 Am I missing something?.
1
c
Apparently you are trying to accomplish something that hasn’t been disclosed… what’s the problem / question here?
m
adding the outer brackets, to me, doesn't really add to any readability. But it doesn't matter at all. In fact, if you then want to do something with that result inlined, you would need to do it anyways.
Copy code
((h1 >= h2 && h2 >= h3) || (h1 <= h2 && h2 <= h3)).let(::println)
c
it shouldn’t affect performance, but the outer brackets do add unnecessary noise making it harder to read. I would personally omit those ones, giving you
val arranged: Boolean = (h1 >= h2 && h2 >= h3) || (h1 <= h2 && h2 <= h3)
which I think makes it more readable than having them there the
&&
operator has higher precedence than
||
so the inner parentheses are technically unnecessary, but I find it can help with readability, especially for folks that aren’t comfortable with operator precedence. So I would personally keep those ones in this expression
b
@Casey Brooks exactly. I now get the outer () are not needed but the inner ones add clarity.
@Chris Lee that h1, h2, and h3 are either ascending or descending. So now i know the outer () should probably be missed out but the inner ones do add clarity.
👍 1
v
I'd probably even do
val arranged: Boolean = ((h1 >= h2) && (h2 >= h3)) || ((h1 <= h2) && (h2 <= h3))
That's also what the "add clarifying parentheses" in Java code does, and what it also does in Kotlin now iirc.
b
@Björn Kechel OK, how do the does (( )) add clarification?
v
Wrong Björn K. you pinged. 😉 While the precedence between
<=
and
&&
is easier to remember or deduct, you still have to remember it and also first read half of the line to understand the expression. With my / IntelliJs variant, it is instantly obvious what the sub-expressions are and you can just read it from left to right and right away understand it. It is simply simpler to grasp and understand and thus reduces brain-load.
Besides that I would probably (depending on the types of hX, I'd write
val arranged: Boolean = (h2 in (h3..h1)) || (h2 in (h1..h3))
r
Honestly, too many parentheses can reduce readability too if the expression is so complex that you would need to add parenthesis around operators like
<=
then it's time to split it
Copy code
val isDescending: Boolean = (h1 >= h2) && (h2 >= h3)
val isAscending: Boolean = (h1 <= h2) && (h2 <= h3)
val arranged: Boolean = isDescending || isAscending
Unnecessary nested parenthesis just obscure which ones are the pairs. Yes, IDE helps with that, or stuff like Rainbow brackets plugin, but it still denies you the just knowing start/end pairs on first glance
v
Well, that's highly subjective of course. Just saying how I do it and how the IDE also thinks it makes sense. 🙂 Of course anyone can do it like he wants or split things up or whatever.
🙂 1