I have a conditional I’m writing that’s checking three things.
if(LoggedInMembershipUser == null || obj == null || boolVal)
In this case, "LoggedInMembershipUser" is just the Membership.GetUser(), "obj" is some random business object, and "boolVal" is obviously a boolean. When I write the statement as above, ReSharper tells me that the boolVal portion of the statement is always false. But when I put boolVal at the beginning as below, I don’t get that notice.
if(boolVal|| LoggedInMembershipUser == null || obj == null)
Why would the first one always be false but the second one not?
EDIT: This is in the row data bind of a grid view. The grid is displaying results from two objects with the same base class, so "obj" will have a value if it’s one of the object types but not the other. boolVal is an indicator for which type of object it is, so now that I think about it, I guess if obj is null then boolVal will always be true. Was ReSharper realizing that somehow? Oh I bet it was because looking at my code above the line I have:
if (!uploaded){
var obj = GetObjectLogic();
}
Ok thanks for the help comments. I guess this can be voted to be deleted or whatever.
Without more of the method it is hard to know for sure, but it sure looks ike the following:
ReSharper has determined that the only way that
boolValcan be true is if at least one ofLoggedInMembershipUserorobjis null. That firstifnever reaches theboolvalportion unless both are not null. Thus at the point whereboolValis evaluated it must be false.If you reorder the conditions, then that logic no longer holds. ReSharper could potentially analyze that expression, determine that all parts are fast and side effect free, and notice that
boolValis not necessary in the second case too, but that analysis is somewhat harder and apparently has not been written.