Ben Edwards
08/11/2022, 3:10 PMval 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?.Chris Lee
08/11/2022, 3:16 PMMichael de Kaste
08/11/2022, 3:17 PM((h1 >= h2 && h2 >= h3) || (h1 <= h2 && h2 <= h3)).let(::println)
Casey Brooks
08/11/2022, 3:19 PMval 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 expressionBen Edwards
08/11/2022, 3:23 PMBen Edwards
08/11/2022, 3:46 PMVampire
08/11/2022, 4:41 PMval 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.Ben Edwards
08/11/2022, 4:44 PMVampire
08/11/2022, 4:50 PM<=
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.Vampire
08/11/2022, 4:53 PMval arranged: Boolean = (h2 in (h3..h1)) || (h2 in (h1..h3))
Roukanken
08/12/2022, 7:57 AM<=
then it's time to split it
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 glanceVampire
08/12/2022, 9:41 AM