News for hackers. Stuff that matters.
Re:Dennis Ritchie on the priorities of && || vs. == etc. (1982) by thomasahle on Friday January 05, 2024 @07:09PM (#38886833)
I think most people agree "a*x**b + c" is more clear than "(a*(x**b))+c". Or "a+b+c+d" is more clear than "a+(b+(c+d))".

Why are we happy to avoid parentheses for these operations but not for && and ||? Probably because we are all really used to the precedences for + and *.

So at the end of the day, what's more clear depends on How familiar the engineers working on your code are with a given set of operators.

Re:Dennis Ritchie on the priorities of && || vs. == etc. (1982) by djur on Friday January 05, 2024 @11:22PM (#38888393)
Most people might agree with that, I don't know, but I know I don't think your first example is clearer. And I've had the operator precedence rules memorized for decades. Parentheses save my brain a step.

The second example is irrelevant because addition is commutative so the parentheses are meaningless. (This is why languages shouldn't override + to be a concatenation operator.)

Re:Dennis Ritchie on the priorities of && || vs. == etc. (1982) by tsimionescu on Saturday January 06, 2024 @02:48AM (#38889403)
The important part that makes the parens in the second operation unnecessary is that addition is associative, not that it's commutative. And concatentation is also associative, so even if those were strings, the parens would still be unnecessary.

That is, (a+b)+c == a+(b+c).

Interestingly, C integer addition is not actually associative, since (1+INT_MAX) + (-1) is UB, but 1+(INT_MAX+(-1)) should in principle be defined as INT_MAX.

Re:Dennis Ritchie on the priorities of && || vs. == etc. (1982) by xigoi on Saturday January 06, 2024 @03:43PM (#38895251)
And floating-point addition is not associative in any language:
Re:Dennis Ritchie on the priorities of && || vs. == etc. (1982) by akira2501 on Saturday January 06, 2024 @12:21AM (#38888673)

    ((x**b) * a) + c
Is equivalent and is the clearer way to express it.
Re:Dennis Ritchie on the priorities of && || vs. == etc. (1982) by saberience on Saturday January 06, 2024 @01:11AM (#38888926)
I think the opposite, I think most people would prefer the version with parentheses, including myself, also, in 20 years experience all companies I’ve worked for would prefer the second expression.

The first expression looks messy and confusing, the second is completely clear in how it will work.

Re:Dennis Ritchie on the priorities of && || vs. == etc. (1982) by userbinator on Saturday January 06, 2024 @01:57AM (#38889133)
Parentheses need to be matched. That adds cognitive overhead. It's especially frustrating when you find the closing parenthesis and then realise it wasn't even necessary.

IMHO it's on a similar level as "== true" "== false" and variations thereof --- absolutely redundant and unnecessary, and shows a lack of knowledge. The same "explicit is better than implicit" mantra is often repeated to justify the latter, but if you think

    if(x == true)
is somehow more "explicit", then surely

    if((x == true) == true)
is even better?

So at the end of the day, what's more clear depends on How familiar the engineers working on your code are with a given set of operators

If someone is not familiar then they should be encouraged to learn and level up, rather than pulling down everyone else.

Re:Dennis Ritchie on the priorities of && || vs. == etc. (1982) by wruza on Saturday January 06, 2024 @11:25AM (#38892707)
I got little scars from knowing precedences and then switching languages. I also got them from short-testing booleans that later turned into T|boolean. And all these clean just_p’s incrementally evolved into !just_p ? !un_q : t’s. Because I was flying through a mass-change after a day and had no mental capacity to repack the meaning of the previous expression and retain the context of a changeset.

Crystallized syntax is clear, but also extremely fragile, if you take humans into account. We have limits. Everything pushes us to these limits. Any complexity spike that overlaps with secondary complexity or fatigue is above our limits. That’s where we make mistakes.

Re:Dennis Ritchie on the priorities of && || vs. == etc. (1982) by mkbosmans on Sunday January 07, 2024 @04:32AM (#38899830)
The big difference between adding the first `== true` to `if (x)` and adding more is that for a bare `x` you need more context to know whether the expression inside the `if` is of boolean type or something that will implicitly be casted to boolean. With `x == true` you know just looking at the `if` statement that it is a boolean expression. Adding more `== true` does not make it more explicit.
Re:Dennis Ritchie on the priorities of && || vs. == etc. (1982) by 9dev on Saturday January 06, 2024 @04:37AM (#38889956)
If you drag it out of context, sure the first example looks more clear. But there is a difference between Boolean logic and arithmetic, and as soon as you use proper variable names, that becomes evident:

(isAdmin || (canReadDocument && canModifyMetadata)) && accountIsActive

Using parentheses makes the code a lot easier to understand here. All these variables carry state with lots of subtleties. Figuring out the implications of all the different cases becomes a lot easier when the code clearly tells them apart.

Re:Dennis Ritchie on the priorities of && || vs. == etc. (1982) by teo_zero on Saturday January 06, 2024 @05:00AM (#38890075)
Just nitpicking: "a+b+c+d" in C is equivalent to "((a+b)+c)+d" not to "a+(b+(c+d))".

I otherwise agree with your comment: in most cases, unnecessary parentheses make the code slower to read, just like unnecessary "==true".

Re:Dennis Ritchie on the priorities of && || vs. == etc. (1982) by naitgacem on Saturday January 06, 2024 @07:36AM (#38890933)
just a fun little anecdote.

I wrote a piece of code that said ` if ( boolean_variable == true)`

this was meant to be "if neither false nor null", since the variable was nullable (kotlin), until someone else tried to "fix the beginner mistake" :P

luckily kotlin requires you handle the nullability and the other person immediately figured out what was going on.

The other person was me, a couple of months down the line.

Re:Dennis Ritchie on the priorities of && || vs. == etc. (1982) by MaxBarraclough on Saturday January 13, 2024 @07:48AM (#38979490)
If the purpose of quirky code isn't immediately clear from the context (and if the code can't easily be reworked to make it cleaner), it's a good candidate for commenting.