Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use short-circuit code?

Related Questions: Benefits of using short-circuit evaluation, Why would a language NOT use Short-circuit evaluation?, Can someone explain this line of code please? (Logic & Assignment operators)

There are questions about the benefits of a language using short-circuit code, but I'm wondering what are the benefits for a programmer? Is it just that it can make code a little more concise? Or are there performance reasons?

I'm not asking about situations where two entities need to be evaluated anyway, for example:

if($user->auth() AND $model->valid()){
  $model->save();
}

To me the reasoning there is clear - since both need to be true, you can skip the more costly model validation if the user can't save the data.

This also has a (to me) obvious purpose:

if(is_string($userid) AND strlen($userid) > 10){
  //do something
};

Because it wouldn't be wise to call strlen() with a non-string value.

What I'm wondering about is the use of short-circuit code when it doesn't effect any other statements. For example, from the Zend Application default index page:

defined('APPLICATION_PATH')
 || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

This could have been:

if(!defined('APPLICATION_PATH')){
  define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
}

Or even as a single statement:

if(!defined('APPLICATION_PATH'))
  define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

So why use the short-circuit code? Just for the 'coolness' factor of using logic operators in place of control structures? To consolidate nested if statements? Because it's faster?

like image 380
Tim Lytle Avatar asked Nov 16 '09 20:11

Tim Lytle


People also ask

What is short-circuit Boolean evaluation Why is it useful?

Short-circuit evaluation means that when evaluating boolean expressions (logical AND and OR ) you can stop as soon as you find the first condition which satisfies or negates the expression.

What is the use of short-circuit operator in Java?

In Java logical operators, if the evaluation of a logical expression exits in between before complete evaluation, then it is known as Short-circuit. A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned.

What is short-circuit code for Boolean expressions?

When two expressions are joined by OR ( || ), if the first one is true , the answer will always be true . When two expressions are joined by AND ( && ), if the first one is false , the answer will aways be false . It doesn't matter what the second expression is and hence, it doesn't need to be evaluated.

How do short circuited operators work?

In JavaScript, short-circuiting is the evaluation of an expression from left to right with || and && operators. If the condition is met and the rest of the conditions won't affect the already evaluated result, the expression will short-circuit and return that result (value).


3 Answers

For programmers, the benefit of a less verbose syntax over another more verbose syntax can be:

  • less to type, therefore higher coding efficiency
  • less to read, therefore better maintainability.

Now I'm only talking about when the less verbose syntax is not tricky or clever in any way, just the same recognized way of doing, but in fewer characters.

It's often when you see specific constructs in one language that you wish the language you use could have, but didn't even necessarily realize it before. Some examples off the top of my head:

  • anonymous inner classes in Java instead of passing a pointer to a function (way more lines of code).
  • in Ruby, the ||= operator, to evaluate an expression and assign to it if it evaluates to false or is null. Sure, you can achieve the same thing by 3 lines of code, but why?
  • and many more...
like image 56
JRL Avatar answered Sep 28 '22 10:09

JRL


Use it to confuse people!

like image 27
rlbond Avatar answered Sep 28 '22 09:09

rlbond


I don't know PHP and I've never seen short-circuiting used outside an if or while condition in the C family of languages, but in Perl it's very idiomatic to say:

open my $filehandle, '<', 'filename' or die "Couldn't open file: $!";

One advantage of having it all in one statement is the variable declaration. Otherwise you'd have to say:

my $filehandle;
unless (open $filehandle, '<', 'filename') {
    die "Couldn't open file: $!";
}

Hard to claim the second one is cleaner in that case. And it'd be wordier still in a language that doesn't have unless

like image 25
Dan Avatar answered Sep 28 '22 08:09

Dan