I noticed this piece of code in using CodeIgniter database migrations:
$this->migrations->verbose AND print "Creating table '{$table}'...";
$verbose is a config value.
We had a debate in the office about whether or not this is valid and readable code. It basically replaces the need for an IF statement as it executes the 2nd part of the condition if the first part is true. I actually quite like it, but the guys in the office think it’s an accident that it works and that this would be more readable:
if( $this->migrations->verbose ) print "Creating table '{$table}'...";
What do you think?
This is called ‘short-circuit evaluation’, and the technique is frequently used in shell scripts – for example:
It’s more common in some languages than others, and a lot of people do see it as something to be avoided, as it can be confusing. (People usually assume that logical expressions don’t have side effects.)