Home » Javascript & Libraries, PHP/MySQL, Programming Techniques » Short Circuit Evaluation and its Use and Advantages in PHP and JavaScript – IF-less Flow Control

4

We call it Short Circuit Evaluation (also known as minimal evaluation, or McCarthy evaluation) when a programming language DOES NOT evaluate/execute the second operand in a condition. if the first operand alone can fully determine the outcome of that condition.

We have been using this concept in our codes always. All PHP guys must be familiar with this piece of code.

mysql_connect(localhost,$user,$password)  or die( “Unable to connect”);

Yes, this is Short Circuit Evaluation! If the above code can connect to a mysql database successfully, the second operand “die” never gets executed. But if the mysql_connect function returns false, then the program dies.

Let’s take two examples to understand how it works:

if ($_POST && isset($_POST['name']) {
//Do something here
}

This is an AND operation and the statements inside IF block will get executed only if both the conditions are TRUE (or Truthy – It doesn’t really have to be a boolean TRUE , but just need have a value other than any Falsy values). So there is no point in evaluating the second condition if the first one is False. It stops execution if $_POST is not defined and skip the IF block.

if ($password == ”  || strlen($password) < 4) {
//Throw error here
}

Here the second operand will be only evaluated if the first condition is False that is when the $password variable is not blank.

IF-less flow control.
In both the above examples, we used IF blocks. But we can actually use this Short Circuit evaluation to get rid of IFs and Elses in our code. If you look at the code of many advanced programmers, you can see that they use this Short Circuit Evaluation method to write small and clean code.

The mysql_connect example at the top is an example for OR Short Cut. Another example:

$from && $to && $msg && sendMail();

This code is equal to,

if ($from && $to && $msg) {
sendMail();
}

In PHP a conditional expression only returns Boolean values. So the Result Type of these Short Cut operators are known as Boolean. See this example.

$name = ‘John’;
$title = $name || “no name”;
echo $title;

$title will hold a value of 1 because the expression returned a True Result.  Even if the $name is false, the $title will hold a value of 1.

But in JavaScript, a conditional expression returns the last evaluated sub expression.

So the Javascript version of the above code would be different.

var name = ‘John’;
var title = name || “no name”;
alert(title);

This will alert “John” hence javascript returns the Last Value evaluated. This feature allows us to write more simple and cleaner code in Javascript.

var name = ‘John’;
var title = name && “no name”;
alert(title);

The above example (a stupid example) will alert “no name” because, the first operand “name” is Truthy and it moves on to evaluate the second expression and returns it as the “Last Value” regardless of the evaluation result.

Do more. Write Less. ;)

4 Comments

  1. William says:

    Most of the times you can just write simple little experiments to figure out whether or not a language uses short circuit evaluation.

    http://www.theblogaholic.com/2011/01/25/does-php-short-circuit-if-statements/

  2. ToT says:

    What’s the advantage(s) and disadvatage(s) of a short circuit anyway???

  3. HaHa says:

    HaHa Very funny comment

Leave a Reply

Page optimized by WP Minify WordPress Plugin