Boolean operators and condition checks in C# - order of presedence
I'm looking at some C# source code trying to trace a bug and relying on my
basic understanding of programming and msdn to decipher, as I have no
experience developing with it. At a key point that the buggy behavior must
pass through, I found the following:
public static bool isObjectSpecialCheck(object someObject)
{
string someParam = getParam(someObject);
if (String.IsNullOrEmpty(someParam))
{
someParam = getParamSomewhereElse(someObject);
}
if (!(string.IsNullOrEmpty(someParam)))
{
try
{
if (!string.IsNullOrEmpty(paramIsSpecial(someParam)))
return (true);
else
return (false);
}
catch (System.Exception ex)
{
GlobalConstants.Log(log, "Error", "isObjectSpecialCheck", ex);
return (false);
}
}
return (false);
}
I have swapped out the original variables with dummies to try and keep the
question abstract. What I'm noticing is that .isNullOrEmpty is used three
distinct ways:
if (String.IsNullOrEmpty(someParam))
if (!(string.IsNullOrEmpty(someParam)))
if (!string.IsNullOrEmpty(paramIsSpecial(someParam)))
The first uses String as the type, first letter capitalized, and does not
use a negation.
The second has the negation outside parenthesis and is only passing in a
defined variable to the method.
The third has the negation right beside the expression, with the
IsNullOrEmpty being passed a function.
So I guess my questions are: Do these distinctions make a difference in
general? Do they appear to be required/intentional in the above code? If
they do make slight difference but the above choices appear to be
style-choices from different contributors, what are the potential logical
errors that could result?
The bug I am tracking down might occur if the above always returned true,
even when it shouldn't. I'm wondering if 99% of the time the above would
return true or false as expected but would return a false true if a
specific value were given (maybe 0 or a string literal 'NULL', etc).
No comments:
Post a Comment