This should be an easy question hopefully but as a rookie I don’t know the answer.
In the code below if ignorePath is true I don’t want to enter the if statement even if tempPath is not null and tempPath length is not 0. I thought a single | would do this but it appears not to.
if (((tempPath != null) && (tempPath.Length != 0)) | ignorePath == false)
{
}
Thanks
I’d say the following code would be clearer:
First you verify that
ignorePathis false (because you don’t want the code to execute when it is true), then you check thattempPathis notnulland that its length is non-zero.The advantage here is that you’ve moved the check of the
ignorePathvariable to be first. Since it is apparently the most important thing to you (it overrides the other two conditions), it ought to come first for clarity and readability (and performance, I suppose, but that hardly matters here).Remember that there’s no reason to check Boolean types against the literal values
trueorfalse. Anifstatement already evaluates whether the value of the statement inside the( )istrueorfalse. Specifying it explicitly is just redundant.The only problem I see with the above code is that
!ignorePathis a little difficult to read. It creates a double negative, as you’re “not-ignoring” something. What exactly does that mean? That’s why most coding standards (including Microsoft’s recommended standards for .NET) encourage you to name Boolean variables with positive grammar. I’d call that variable something likecheckPath, instead.