https://kotlinlang.org logo
#getting-started
Title
# getting-started
b

Ben Edwards

08/11/2022, 3:10 PM
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

Chris Lee

08/11/2022, 3:16 PM
Apparently you are trying to accomplish something that hasn’t been disclosed… what’s the problem / question here?
m

Michael de Kaste

08/11/2022, 3:17 PM
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

Casey Brooks

08/11/2022, 3:19 PM
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

Ben Edwards

08/11/2022, 3:23 PM
@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

Vampire

08/11/2022, 4:41 PM
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

Ben Edwards

08/11/2022, 4:44 PM
@Björn Kechel OK, how do the does (( )) add clarification?
v

Vampire

08/11/2022, 4:50 PM
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

Roukanken

08/12/2022, 7:57 AM
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

Vampire

08/12/2022, 9:41 AM
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
6 Views