Click here to Skip to main content
11,496,146 members (671 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
See more: C#3.0 C++ C C# C#4.0
f1(), f2(), f3() is a function of type bool, which of the following style is better?
 A. if (f1 () && f2 () && f3 ()) do ();
 B. if (f1 ())
      if (f2 ())
        if (f3 ())
           do ();
If three function execution time is longer, is B method is better? Or they are just the same effect in different write way?
Posted 21-Jan-13 6:40am
zqliu359
Edited 21-Jan-13 7:02am
v3
They should be about the same. I prefer A; it's more polished. Any fool can do B.
  Permalink  
v2
Comments
rizwan muhammed khan gouri at 22-Jan-13 8:58am
   
Why you give Answare For this time besting type of quertion...........
PIEBALDconsult at 22-Jan-13 9:01am
   
Ummm... what?
It actually doesn't matter from a run-time point of view. In other words, A and B will run equally fast. For readability purposes, however, I would always prefer A. or better yet

if (f1() && f2() && f3())
    doSomething ();
  Permalink  
I'd prefer A. If (f1 () && f2 () && f3 ()) do (); because it is much more clearer and easier to understand.

You are using && which is a short circuit operator.
If the first condition fails the second won't execute so there will be no difference in execution time in either case.
  Permalink  
v3
You left out a 3rd variant:
bool hasSomethingToDo = f1() && f2() && f3();
if (hasSomethingToDo)
{
   DoSomething();
}
and a 4th variant:
bool hasSomethingToDo = f1()
                     && f2()
                     && f3();
if (hasSomethingToDo)
{
   DoSomething();
}

Usually, your functions are not that short as you show above. So, if the function calls are a bit more text, than I go for the 4th variant.

With one or two function calls, I go for the 1st variant. The second variant is the one I avoid as much as possible.

BTW: Execution time shoud not differ in a relevant manner - it's a question of style (if it's relevant, measure it!).

Cheers
Andi
  Permalink  
Any C/C++ compiler nowadays will treat both the same way and generate either the same code or almost the same code.

A point perhaps more important than performance for this one-liner is maintainability. In most cases, 'A' is the better choice - if the logical tests are truly function names or very short expressions.

But this is a razor-thin advantage. If the tests get more complicated, it might be better to break out into multiple 'if' statements for readability purposes. ... and if you ever need to insert an 'else' clause, then 'B' for at least that part of it is better. Don't repeat boolean tests to avoid 'B' logic.

Also never do

if(x == true) {...}
if(x == false) {...}

or you'll never get near my code base...
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 RyanDev 230
1 Sergey Alexandrovich Kryukov 114
2 Richard Deeming 100
3 OriginalGriff 89
4 Mostafa Asaduzzaman 59
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600


Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 21 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100