Alex Rivera | Logout

Silly question about how you format long if statements

Asked 2011-05-26T02:49:26.990
10

On long if statements where they take up more than one line, do you put the conditions like AND or OR on a new line like this:

               if (something
                   && something else)

Or like this:

               if (something &&
                   something else)
Edit
Report

1 Answer

1

Given my druthers, I'd avoid long if tests in the first place. I'd rather do something like:

bool fTest1 = A == B ;
bool fTest2 = C ;
bool fTest3 = f(1,2,3) ;
bool fSuccess = ( fTest1 | ftest2 ) & fTest3 ;
if ( fSuccess )
...

Otherwise something like this:

if (  A == B
&& (  C == D
   || E == F
   )
&&    Z >  Y
) {
    ...
  }
else
  {
    ...
  }

YMMV, of course.

The former is far easier to debug, test, log, etc.

answered 2011-05-26T02:56:19.187

Your Answer