Ben
Ben

Reputation: 57207

Best practice: if logic control

With respect to coding standards, speed, and efficiency, which of the following is a better programming practice for this situation?

function foo() {
  if(bar)   { return 0; }
  if(baz)   { return 0; }
  if(qux)   { return 0; }
}

or

function foo() {
  if(bar || baz || qux) { return 0; }
}

I'd lean toward the first, since only one condition has to be evaluated and therefore would be faster, but having the multiple returns is not good...?

//EDIT

The languages I'd be applying this to are mainly PHP and Javascript, possibly C++ and Ruby.

Upvotes: 2

Views: 2004

Answers (7)

paxdiablo
paxdiablo

Reputation: 881393

It depends entirely upon the language. Many languages will short-circuit evaluations so that, if bar is true, the other two are not evaluated, and any half-decent compiler will optimise those to the same thing in that case. Of the four languages you mentioned (C++, Ruby, PHP and Javascript), they all do short circuit evaluations.

And, despite what the "avoid multiple returns" crowd will tell you, that's not a rule you should follow like a sheep. It's meant to avoid situations where it's hard to see where returns (or loop breaks) are happening. Your first solution does not suffer from that problem any more than your second.

Blind following of dogma without understanding the reasons behind it should be an offence punishable by painful torture.

Upvotes: 0

Eric K Yung
Eric K Yung

Reputation: 1784

The latter example is better in my opinion in terms of coding. In an OR statement, atmost one condition is true the statement is true. So if the first condition is true, no further conditions will be looked at. There is no loss in speed or efficiency.

Upvotes: 0

wizzardz
wizzardz

Reputation: 5874

In the second case since you are using logical-OR the second conditions are checked only if it is necessary,so wrt better coding standards I would like to go with second one.

Upvotes: 0

Victor Nicollet
Victor Nicollet

Reputation: 24577

Almost every single programming language today uses short-circuit evaluation for ||, which means the two examples will be equivalent in terms of control flow and thus performance.

Having multiple returns should indeed be avoided if they are spread all over the function and they return different things, because this decreases readability. On the other hand, it's fairly standard to have early-out conditions that detect inacceptable conditions and stop the execution flow:

function getFriendList() 
{
  if (! has_internet_connection() ) return null;
  if (! is_logged_in() ) return null;

  return server.getFriendList();
}

Upvotes: 6

Rob
Rob

Reputation: 10248

The latter but as:

function foo()
{

   var result = 1;

   if(bar || baz || quz)
   {
       result = 0;
   }

   return result;
}

Exiting your code willy-nilly with "return" is bad practice and makes debugging a nightmare - especially if it is someone elses code you are trying to debug! Flow of control should always exit at the bottom of the function!

Upvotes: 2

Liviu Mandras
Liviu Mandras

Reputation: 6617

In C# you can use the second version because is the same regarding performance but looks nicer. If bar is true then the other flags are no longer checked.

Upvotes: 1

Mark Byers
Mark Byers

Reputation: 838166

Regarding your second example the || is short-circuiting in most languages so only the necessary conditions will be evaluated. For example if bar evaluates to true, neither baz nor qux will be evaluated.

Knowing this, I would probably choose the second example.

Upvotes: 3

Related Questions