Is there a reason to use if (1 ||! Foo ())?

I read the legacy code:

if ( 1 || !Foo() ) 

Is there a reason why not write:

 if ( !Foo() ) 
+56
c ++ c syntax if-statement logical-operators
Oct 10 '13 at 9:42 on
source share
4 answers

Two of them are not the same. The former will never evaluate Foo() because 1 closes || .

Why this happened - maybe someone wanted to force an entry in the then branch for debugging purposes and left it there. It may also be that it was written before source control, so they did not want the code to be lost, but now it just went around.

+134
Oct 10 '13 at 9:44
source share

if (1 || !Foo() ) will always execute. !Foo() will not even be reached due to evaluation of short circuits .

This happens when you want to make sure that the code under if will be executed, but you do not want to remove real conditions in it, perhaps for debugging purposes.

Additional information that may help you:

  • if(a && b) - if a false , b will not be checked.
  • if(a && b) - if a is true , b will be checked, because if it is false , the expression will be false .
  • if(a || b) - if a is true , b will not be checked, because it is true anyway.
  • if(a || b) - if a is false , b will be checked, because if b is true , then it will be true .



It is strongly recommended that you have a macro for this purpose, say DEBUG_ON 1 , which will make it easier to understand what the programmer means and not have magic numbers in the code (thanks @grigeshchauhan).

+44
Oct 10 '13 at 9:44
source share
 1 || condition 

always true, regardless of whether the condition true or not. In this case, condition never evaluated. The following code:

 int c = 5; if (1 || c++){} printf("%d", c); 

outputs 5 , since c never increases, however if you change 1 to 0 , c++ is actually called, making output 6 .




The usual practical use of this is in a situation where you want to test the part of the code that is called when a condition is satisfied that evaluates to only true:

 if (1 || condition ) { // code I want to test } 

This condition method will never be evaluated, and therefore // code I want to test always called. However, this is definitely not the same as:

 if (condition) { ... 

which is an instruction where condition will actually be evaluated (and in your case Foo will be called)

+11
Oct 10 '13 at 9:44
source share

They answered the question correctly - the difference is that the right part of the operation is either short-circuited, which means that this is the debugging code for forcing input into the if block.

But in the interests of best practices, at least my rough blow at best, I would suggest alternatives in order of increasing preference (last best):

note: noticed that after the coded examples it was a C ++ question, examples are C #. Hope you can translate. If someone needs me, just post a comment.

Comment in line:

 if (1 /*condition*/) //temporary debug 

Off line comment:

 //if(condition) if(true) //temporary debug 

Name-indicative function

 //in some general-use container bool ForceConditionForDebug(bool forcedResult, string IgnoredResult) { #if DEBUG Debug.WriteLine( string.Format( "Conditional {0} forced to {1} for debug purposes", IgnoredResult, forcedResult)); return forcedResult; #else #if ALLOW_DEBUG_CODE_IN_RELEASE return forcedResult; #else throw new ApplicationException("Debug code detected in release mode"); #endif #endif } //Where used if(ForceConditionForDebug(true, "condition"))... //Our case if(ForceConditionForDebug(true, "!Foo()"))... 

And if you want a truly reliable solution, you can add a repository rule to control the source code to reject any validated code called ForceConditionForDebug. This code should never have been written this way because it clearly does not convey intent. It should never have been checked (or allowed for verification) (verification of version control?) And it certainly should never be allowed to be performed in production in its current form.

+10
Oct 10 '13 at
source share



All Articles