Re:Google broke a conditional statement that verifies passwo...
by gpderetta on Friday July 23, 2021 @05:08AM
(#27928696)
You could make the opposite argument that short-circuiting is the surprising behavior [1] and you should make it explicit with an if.
[1] and in this very thread many people were actually surprised by it!
Re:Google broke a conditional statement that verifies passwo...
by qalmakka on Friday July 23, 2021 @06:57AM
(#27929321)
Short circuiting for logical operators has existed at least since the first C versions in the early '70s. It's maybe not intuitive, it's probably a crummy idea, but that's how basically everything based on C works (including modern languages like Go and Rust), so there's not that much that can be done about that.
Also, after seeing how weird JavaScript is in comparison, I have to say that C++ definitely isn't the strangest kid in the block when accounting for weirdness and unintuitive behaviours.
In both cases, I think the risk-benefit ratio is way too skewed towards the risk of creating ugly, hard to detect bugs. When I don't want short circuiting from happening, I just assign the RHS of the expression to a variable beforehand; if I want a boolean to become an integer, I make it explicit with a cast.
Sometimes, using certain C and C++ debatable features feels like attempting to shave yourself using a dagger. The fact you could do that doesn't mean you should, or even that it is a good idea.