3

I am having a code, which contains

bool fn()
{
...
//all the following are boolean functions.
return isTrue()
       &&isMsgReceived()
       &&isMsgSent();
}

The problem is that each of the return boolean functions are themselves quite lengthy and takes much computation. Actually, there is no point in checking subsequent functions, if previous one already failed (and condition).

Would you suggest simpler ways to return false, in case one of the beginning functions already failed and not go for further checking. The aim is to reduce compute time.

RBarryYoung
  • 55,398
  • 14
  • 96
  • 137
SKPS
  • 5,433
  • 5
  • 29
  • 63
  • 3
    c does that. Its called short circuit evaluation http://en.wikipedia.org/wiki/Short-circuit_evaluation – Harry Sep 08 '13 at 16:14
  • Not directly addressing your question, but it seems that you may find the State pattern appealing. The canonical example (presented in the Gang of Four book) uses a case study dealing with a connection. – Escualo Sep 08 '13 at 20:30
  • If there is no dependence of those three function calls. You call call those function by the order of the P([compute time], [false probability]). For example, if time of isMsgReceived() is the fastest and most likely return false, you can rewrite it as isMsgReceived() && isTrue() && isMsgSent(). And even you can figure out the formular how to sort the function call. – ZijingWu Sep 09 '13 at 05:52

2 Answers2

25

&& already does that for you. If isTrue() returns false, the next two functions are not even evaluated.

It therefore makes sense to put the least expensive function first in a chain of &&s.

us2012
  • 16,083
  • 3
  • 46
  • 62
  • 2
    Cool. That was much helpful. Especially this part "It therefore makes sense to put the least expensive function first in a chain of &&s" – SKPS Sep 08 '13 at 16:12
  • 4
    This is called "short circuit evaluation" – bobobobo Sep 08 '13 at 16:13
  • 2
    Not necessarily the least expensive, but the most likely to fail – Johannes S. Sep 08 '13 at 16:42
  • 1
    @JohannesS. Yeah, it really depends on the relative cost and likeliness to fail. If two functions read data from disk/network while one of them only checks state that is available in memory, you should always put the least expensive one first. If the costs of the functions are not very far apart, your rule makes more sense. It's really a case by case decision. – us2012 Sep 08 '13 at 17:14
  • @us2012: exactly, the general solution is to compute the cost of each call as well as its likelihood of failure and then derive the cost of each and every combination. Of course, most of us will preferably trust our guts (for better or worse). – Matthieu M. Sep 08 '13 at 17:53
10

Your code already does that.

The standard says explicitly:

5.14 Logical AND operator [expr.log.and]

The && operator groups left-to-right. The operands are both contextually converted to bool (Clause 4). The result is true if both operands are true and false otherwise. Unlike &, && guarantees left-to-right evaluation: the second operand is not evaluated if the first operand is false.

Then if isTrue() is evaluated to false, the next functions will not be evaluated.

You should then put the least expensive function at first.

Community
  • 1
  • 1
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
  • Unless the left operand is a user-defined type with an overloaded `operator &&`. (Doesn't apply to the OP's question, but this is something to watch out for when trying to do "fancy" stuff.) – wjl Sep 08 '13 at 18:44