PHP: IF statement vs. JS style expression

In situations when we need to write something like this:

if ($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.

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *