News for hackers. Stuff that matters.
Re:The rise of never-ending job interviews by HWR_14 on Monday August 02, 2021 @07:59AM (#28035467)
You're not mentioning or alluding to the one obvious syntax error. I don't know how to spoiler it so I will say there is an issue on the second line that, again repeating, I don't know how to call out.

And yes, I work in multiple languages.

Re:The rise of never-ending job interviews by leetcrew on Monday August 02, 2021 @08:56AM (#28035885)
also if you can't spot that error, you might not realize a subtle, yet important, detail about that line (if written correctly). contrary to what you might expect, it cannot short-circuit the way || does, leading to possible performance or even correctness issues.
Re:The rise of never-ending job interviews by HWR_14 on Monday August 02, 2021 @09:13AM (#28036075)
This is very true. I hope that people are not using the short-circuit feature in a way that impacts correctness! I would have an issue with that code for trying to be too clever even if there were no bugs and it worked well.

Performance issues, on the other hand, I can see accidentally arising.

Re:The rise of never-ending job interviews by bryondowd on Monday August 02, 2021 @02:29PM (#28040717)
I see short-circuit for correct behavior all the time, most frequently in the format: if (pointer && pointer->member == value)

Where you want to make sure a pointer you've been given isn't null before you try to dereference it. Without short-circuit, this becomes a segfault.

Re:The rise of never-ending job interviews by HWR_14 on Monday August 02, 2021 @09:34PM (#28044788)
Yes, the "and" pattern is common. I've seen plenty of "ensure this exists before operating on it" chains, where it even goes if ( pointer && pointer->otherPointer && pointer->otherPointer->member == value )

I've never seen someone use an "or" pattern in a non-confusing way.

That is, I've seen "and" patterns that act as a safety/early out. I fail to see the use of an "or" pattern where you want to stop executing if the first value is true (ignoring using nots to invert the logic of an and pattern, and I would criticize that).

Re:The rise of never-ending job interviews by lucumo on Monday August 02, 2021 @09:50AM (#28036466)
That boolean assignment operator was addressed. Whether or not C++ has them (it doesn't) is exactly the point of that code snippet.

They do exist in other languages. ES has it in exactly that form. Java has it in the |= form, not to be confused with the bitwise OR of the same form.

Whether or not these short-circuit is not all that interesting for most boolean logic. (Though it can be useful to know if these are used as hacky error-handling and default-setting. It depends on how you read "something" whether or not that's going on here.)

Re:The rise of never-ending job interviews by HWR_14 on Monday August 02, 2021 @10:03AM (#28036648)
> That boolean assignment operator was addressed.

Where and when? Because I was the only person alluding to it, and the replies to my post.

> Whether or not C++ has them (it doesn't) is exactly the point of that code snippet.

C++ has boolean assignment operators that operate the way that the code is clearly meant to operate. That code does not have have a valid one. Where do you think Java got them from?

Re:The rise of never-ending job interviews by lucumo on Monday August 02, 2021 @10:31AM (#28036996)
> > That boolean assignment operator was addressed. > Where and when? Because I was the only person alluding to it, and the replies to my post.

The words "boolean assignment operator" are in the first sentence after the code snippet in megous' post.

> C++ has boolean assignment operators that operate the way that the code is clearly meant to operate. That code does not have have a valid one. Where do you think Java got them from?

As far as I can tell the |= operator in C++ is the same as in C, i.e. a bitwise OR operator. It works for booleans due to their bit pattern, but it's not the same. My C++ knowledge is extremely limited, so I looked it up and I may be misinformed though.

Java's |= is different for ints and boolean. There are no bitwise operators for booleans: bool1 | bool2 is a strict logical operation (that doesn't short-circuit). bool1 |= bool2 is a logical operation that will fail when other types are mixed in. int1 |= int2 is a bitwise operation. Java does not have a short-circuiting ||= operation (but ES does).

For the most part these differences aren't that important, but they do trip people up when switching between languages.

Re:The rise of never-ending job interviews by HWR_14 on Monday August 02, 2021 @09:39PM (#28044826)
> As far as I can tell the |= operator in C++ is the same as in C, i.e. a bitwise OR operator.

This is true.

> It works for booleans due to their bit pattern, but it's not the same.

This makes less sense. A bool in c++ is just an integer value with few inputs. It is, as far as I can tell, the exact same.

> Java's |= is different for ints and boolean.

Java has bool and int as different types. There were versions of C++ compilers that just straight out had bool as a typedef of int.

Re:The rise of never-ending job interviews by megous on Monday August 02, 2021 @10:21AM (#28036873)
C++ does not have boolean assignment operator in a sense the ||= syntax works in ES.

https://www.w3schools.com/cpp/trycpp.asp?filename=demo_oper_...

||= will not assign anything unless the LHS variable is falsy. It will not even evaluate the right side unless LHS is falsy.

|= will work the same in C++ and ES mostly (depending on types)

Re:The rise of never-ending job interviews by HWR_14 on Monday August 02, 2021 @09:41PM (#28044844)
I'm just thrown for a loop. Why is the default to assume I'm familiar with the ||= operator in ES?

The fact that the ||= operator short circuits makes sense from an optimization point of view. I will maintain that there is no good use for the correctness (as opposed to optimization) of the code to depend on that feature.