In situations when we need to write something like this:
if ($doStuff) {
$obj->doStuff();
}
is it acceptable to write something like this?
$doStuff && $obj->doStuff();
If not, why?
Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.
Sign up to join this communityIn situations when we need to write something like this:
if ($doStuff) {
$obj->doStuff();
}
is it acceptable to write something like this?
$doStuff && $obj->doStuff();
If not, why?
For me, it's not good practice.
First, it hides the logic. Everyone can easily see what the if-construction means. The other is just... "clever" coding, which you shouldn't do.
You can't reorder the two things, although generally, you should be able to (oh wait, there are side effects!).
The result of the expression is not used? Hmm... I might just delete that line of code.
Also, I don't think doing boolean operations with parameters that are maybe void/null/... is a good thing.
if
s require attention. Also I like it to be different from standard boolean operations, so I useand
/or
operators (visible in IDE).