Evaluating boolean expression in C# - c#

I have written the code below to evaluate a boolean expression. The expression is coded in the form of objects.
It's one of those moments when I look at the code and think: I'm sure there's a better way to code that, using less boolean variables but can't see the right way to go. Any help? Unit tests have been written and are passing for a variety of inputs.
if (tree == null || !tree.IsActive || tree.FilterNodes == null)
{
return false;
}
var result = false;
foreach (var filter in tree.FilterNodes.Where(a => a.IsActive && a.ConditionNodes != null))
{
var tempBool = false;
foreach (var condition in filter.ConditionNodes.Where(a => a.IsActive))
{
if (!string.IsNullOrWhiteSpace(condition.FieldName) && values.ContainsKey(condition.FieldName))
{
var value = values[condition.FieldName];
if (filter.LogicalOperator == LogicalOperator.Or && ApplyCondition(condition.ConditionOperator, value, condition.FieldValue))
{
tempBool = true;
break;
}
else if (filter.LogicalOperator == LogicalOperator.And)
{
tempBool = ApplyCondition(condition.ConditionOperator, value, condition.FieldValue);
if (!tempBool)
{
break;
}
}
else
{
tempBool = false;
}
}
else if (!string.IsNullOrWhiteSpace(condition.FieldName) && filter.LogicalOperator == LogicalOperator.And)
{
tempBool = false;
}
}
result = tempBool;
if (!result)
{
break;
}
}
return result;

You could set tempBool = false first thing in the loop and leave out the else and last else if:
foreach (var condition in filter.ConditionNodes.Where(a => a.IsActive))
{
tempBool = false;
if (!string.IsNullOrWhiteSpace(condition.FieldName) && values.ContainsKey(condition.FieldName))
{
var value = values[condition.FieldName];
if (filter.LogicalOperator == LogicalOperator.Or && ApplyCondition(condition.ConditionOperator, value, condition.FieldValue))
{
tempBool = true;
break;
}
else if (filter.LogicalOperator == LogicalOperator.And)
{
tempBool = ApplyCondition(condition.ConditionOperator, value, condition.FieldValue);
if (!tempBool)
{
break;
}
}
}
}
EDIT
It gets even simpler:
foreach (var condition in filter.ConditionNodes.Where(a => a.IsActive))
{
tempBool = false;
if (!string.IsNullOrWhiteSpace(condition.FieldName) && values.ContainsKey(condition.FieldName))
{
var value = values[condition.FieldName];
tempBool == ApplyCondition(condition.ConditionOperator, value, condition.FieldValue);
if ((filter.LogicalOperator == LogicalOperator.And && !tempBool) || (filter.LogicalOperator == LogicalOperator.Or && tempBool))
{
break;
}
}
}
In the if you need ApplyCondition to be true and then set tempBool to true (also the result of ApplyCondition). In the else if you set tempBoolto the result of ApplyCondition. That means you can set tempBoolto the result of ApplyConditionin the first place. Now you just need to decide if you need to break.

Taking a more o-o approach, I think your operators need to be defined by classes that inherit from a base class. The base class would have an abstract Evaluate method that your operators implement. You can then use o-o polymorphism to evaluate your operators without worrying about the internal details. Effectively you have the beginnings of a simple interpreter.

A more formal way to code a boolean interpreter is considering a boolean expression as generated by a formal grammar and writing a parser and an interpreter for it. The interpreter could be implemented as an abstract syntax tree.
I made an open source library to achieve this, if you want you can take a look on GitHub.

Related

c# two nested if statements, one else

Please excuse the question that seems simple, but for some reason I cannot think of an elegant solution at the moment.
I have the following situation:
if (Request.QueryString["name"] != null)
{
if (Request.QueryString["name"].ToString() != "")
{
nameSpan.InnerHtml = Request.QueryString["name"].ToString();
}
}
The problem is, if I want to hide the nameSpan if querystring["name"] is either null or emtpy. The ugly solution would be:
if (Request.QueryString["name"] != null)
{
if (Request.QueryString["name"].ToString() != "")
{
nameSpan.InnerHtml = Request.QueryString["name"].ToString();
}
else
{
nameSpan.Visible = false;
}
}
else
{
nameSpan.Visible = false;
}
I would like to have a situation where both the nameSpan.Visible = false; sections could be merged into one area so I only have to write it once.
As far as I am aware, it is not possible to do the following:
if (Request.QueryString["name"] != null && Request.QueryString["name"].ToString() != "")
{
nameSpan.InnerHtml = Request.QueryString["name"].ToString();
}
else
{
nameSpan.Visible = false;
}
But please tell me if I am wrong! If you have a different solution which changes the logic then I am more than happy to have different views! Thank you!
Your && solution should be fine. If left side of && is false, right side it not evaluated so there will be no exception.
If you want, you could use String.IsNullOrEmpty static method:
if (!string.IsNullOrEmpty(Request.QueryString["name"]))
{
nameSpan.InnerHtml = Request.QueryString["name"].ToString();
}
else
{
nameSpan.Visible = false;
}
if you are after compact code, I'd use next (NameValueCollection returns null if key does not exist, and indexer returns string):
nameSpan.InnerHtml = Request.QueryString["name"];
nameSpan.Visible = !string.IsNullOrEmpty(nameSpan.InnerHtml);

C# error - "Not all code paths return a value"

This is fairly simple method. I use entity framework to get some data and then check some values in a if statement. However right now the method is marked with red.
This is my method:
private bool IsSoleInProduction(long? shoeLastID)
{
if (shoeLastID == null)
{
MessageBox.Show(Resources.ERROR_SAVE,
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return false;
}
ISoleService soleService =
UnityDependencyResolver.Instance.GetService<ISoleService>();
List<Sole> entity =
soleService.All().Where(s => s.ShoeLastID == shoeLastID).ToList();
if (entity.Count() != 0)
{
foreach (var items in entity)
{
if (items.Status == 20)
{
return true;
}
else
{
return false;
}
}
}
else
{
return false;
}
}
What am I missing?
You need to take advantage of LINQ with Any, replace your code:
if (entity.Count() != 0)
{
foreach (var items in entity)
{
if (items.Status == 20)
{
return true;
}
else
{
return false;
}
}
}
else
{
return false;
}
with simpler code:
return entity.Any(item => item.Status == 20);
Or even better performance:
return soleService.All()
.Any(s => s.ShoeLastID == shoeLastID
&& s.Status == 20);
Edit: With you comment, below code is what you need:
List<Sole> entity = soleService.All()
.FirstOrDefault(s => s.ShoeLastID == shoeLastID);
return entity == null ? false : entity.Status == 20;
If there's no item in your entity collection, then neither of the containing if/else branches will be executed. In this case there's no return statement anymore, because the else part won't be executed, and outside your foreach you have no return statement.
The compiler does not "see" that if
entity.Count() != 0
then your loop
foreach (var items in entity)
will run at least once. Therefore it sees a possibility of running the forech zero times, and not running the else block.
Suppose first time the entity is enumerated, it yields some (finite number of) items. Then the Count will be non-zero. Then suppose next time the same entity is enumerated then it yields no items! That would cause your code to "fall through" without returning.
It is very probably that you can guarantee that the source yields the same number of items each time it is re-enumerated. But the compiler cannot.
Solution: Just skip if (entity.Count() != 0) and do foreach right away.
You haven't retrun anything from this code block
if (entity.Count() != 0)
{
foreach (var items in entity)
{
if (items.Status == 20)
{
return true;
}
else
{
return false;
}
}
// return someting
}
You might consider doing the following. This will adhere to the "single exit-point" principle (which can sometimes help improve code clarity), and ensure you have a default value in any case:
private bool IsSoleInProduction(long? shoeLastID)
{
// The main change: A default value, assuming "no":
var isSoleInProduction = false;
if (shoeLastID == null)
{
MessageBox.Show(Resources.ERROR_SAVE,
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
isSoleInProduction = false;
}
ISoleService soleService =
UnityDependencyResolver.Instance.GetService<ISoleService>();
List<Sole> entity =
soleService.All().Where(s => s.ShoeLastID == shoeLastID).ToList();
if (entity.Count() != 0)
{
foreach (var items in entity)
{
if (items.Status == 20)
{
isSoleInProduction = true;
}
else
{
isSoleInProduction = false;
}
}
}
else
{
isSoleInProduction = false;
}
return isSoleInProduction;
}
What will be your entity.Count() is not 0 and your entity doesn't have any items?
That means your if block will work but foreach part will not work. Since your if part doesn't have any return statement, that's why you get an error.
You should put return statement in your if part.
if (entity.Count() != 0)
{
foreach (var items in entity)
{
if (items.Status == 20)
{
return true;
}
else
{
return false;
}
}
//return true or false
}
If your entity collection has no elements you will not reach a return statement - you need to add a return false forexample as last statement
As the error states there can be cases in which none of your return clause is evaluated (e.g. if there are no elements in your list).
To quickly solve it you can put a default return statement, for example by moving the last return clause outside the else statement. But it really depends on the behavior you'd expect.
private bool IsSoleInProduction(long? shoeLastID)
{
if (shoeLastID == null)
{
MessageBox.Show(Resources.ERROR_SAVE, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
ISoleService soleService = UnityDependencyResolver.Instance.GetService<ISoleService>();
List<Sole> entity = soleService.All().Where(s => s.ShoeLastID == shoeLastID).ToList();
if (entity.Count() != 0)
{
foreach (var items in entity)
{
if (items.Status == 20)
{
return true;
}
else
{
return false;
}
}
}
return false;
}
The compiler can't guarantee that the first call to Count means that the foreach will loop at least once (with good reason, because you could, if you wished, create a collection where this wasn't true). You can do this instead (with no need for the outer if):
foreach (var items in entity)
{
if (items.Status == 20)
{
return true;
}
else
{
return false;
}
}
return false;

Check if an array contains an invalid value and return false?

This is probably very simple but I guess I haven't had enough of coffee yet.
I have an array with four values and I want to check if any of them is invalid and then set a boolean value to false, else to true.
bool validDecoding = false;
foreach (string decodedValue in arrayOfvalues)
{
if (decodedValue.Contains("invalid") || decodedValue.Contains("length") || decodedValue.Contains("bad"))
{
validDecoding = false;
}
else
{
validDecoding = true;
}
}
But if the last does not contain invalid, length or bad then validDecoding is set to true but I want it to be false if one or more values are invalid.
Please help?
Thanks in advance.
Include System.Linq namespace and you can do following:
validDecoding = !arrayOfvalues.Any(
value => value.Contains("invalid") || value.Contains("length") || value.Contains("bad"));
bool validDecoding = false;
foreach (string decodedValue in arrayOfvalues)
{
if (!decodedValue.Contains("invalid") && !decodedValue.Contains("length") && !decodedValue.Contains("bad"))
{
validDecoding = true;
break;
}
}
Set valid to true at first, then set it to false in your loop if it's invalid.
bool validDecoding = true;
foreach (string decodedValue in arrayOfvalues)
{
if (decodedValue.Contains("invalid") || decodedValue.Contains("length") || decodedValue.Contains("bad"))
{
validDecoding = false;
break;
}
}
That way it's never set back to true! (you need more coffee ;-) )
Set your validDecoding to 'true' initially, and only reset to false if it breaks.
bool validDecoding = true;
foreach (string decodedValue in arrayOfvalues)
{
if (decodedValue.Contains("invalid") || decodedValue.Contains("length") || decodedValue.Contains("bad"))
{
validDecoding = false;
}
else
{
//Do Nothing
}
}
I like Andrew's answer. If you don't have Linq available, try this option:
// Note: Name boolean variables like they are a question
bool isValidDecoding = true;
foreach (string decodedValue in arrayOfvalues)
{
isValidDecoding &= !decodedValue.Contains("invalid")
&& !decodedValue.Contains("length")
&& !decodedValue.Contains("bad");
}
I particularly like this option when your tests contain logging, and you want to find all failures before termination.
I'd be wary of decodedValue.Contains("length") though. Maybe it works in your scenario, but it might give false negatives for other cases, and definitely makes your code less clear. You should double check that there isn't a more unique/indicative value that ensures that you have a bad decoding for those cases.

Return a variable as soon as it is set to a certain value. Equality overloading

I am overloading the Equality method for value comparison and was wondering if there is a clean way to return false as soon as one of the value comparisons returns false. For example, here is the basic idea:
public class MyClass
{
private int _valOne;
private int _valTwo;
private int _valThree;
public MyClass(int valOne, int valTwo, int valThree)
{
_valOne = valOne;
_valTwo = valTwo;
_valThree = valThree;
}
public override bool Equals(object obj)
{
// If the object is null return false
if (obj == null)
{
return false;
}
// If the object is not of MyClass type return false
MyClass myClass = obj as MyClass;
if (myClass == null)
{
return false;
}
// Now compare all the field values
bool areEqual = false;
areEqual = (this._valOne == myClass._valOne);
areEqual = (this._valTwo == myClass._valTwo);
areEqual = (this._valThree == myClass._valThree);
return areEqual;
}
}
Say the _valOne's do not equal. The most efficient way to compare is to return false as soon as it is known two values are not equal. Something like the following...
// Now compare all the field values
bool areEqual = false;
areEqual = (this._valOne == myClass._valOne);
if (!areEqual)
{
return false;
}
areEqual = (this._valTwo == myClass._valTwo);
if (!areEqual)
{
return false;
}
areEqual = (this._valThree == myClass._valThree);
return areEqual;
So now after the comparison of the _valOnes no more value comparisons are made. This seems very repetitive, clunky, and (most importantly) horrible for readability. I want to know if there is any way this code can be reduced to the same effect in a clean way without the use of the && operator.
You can take advantage of the short-circuiting nature of the logical AND (&&) operator, like this:
return this._valOne == myClass._valOne
&& this._valTwo == myClass._valTwo
&& this._valThree == myClass._valThree;
As soon as any one of the comparisons evaluates to false, the whole condition evaluates to false. If all three are true, the condition returns true.
Use an and condition:
areEqual = (this._valOne == myClass._valOne)
&& (this._valTwo == myClass._valTwo)
&& (this._valThree == myClass._valThree);
&& implements short-circuiting by default.
Another way could be to do this:
if (this._valOne != myClass._valOne)
return false;
if (this._valTwo != myClass._valTwo)
return false;
if (this._valThree != myClass._valThree)
return false;
return true;
A matter of choice I guess. I'd think the && one is more readable..

Determine value of object in C#

What would be the best way to determine if an object equals number zero (0) or string.empty in C#?
EDIT: The object can equal any built-in System.Value type or reference type.
Source Code:
public void MyMethod(object input1, object input2)
{
bool result = false;
object compare = new object();
if(input != null && input2 != null)
{
if(input1 is IComparable && input2 is IComparable)
{
//do check for zero or string.empty
//if input1 equals to zero or string.empty
result = object.Equals(input2);
//if input1 not equals to zero or string.empty
result = object.Equals(input1) && object.Equals(input2); //yes not valid, but this is what I want to accomplish
}
}
}
Using Jonathan Holland code sample with a minor modification, here is the solution that worked:
static bool IsZeroOrEmpty(object o1)
{
bool Passed = false;
object ZeroValue = 0;
if(o1 != null)
{
if(o1.GetType().IsValueType)
{
Passed = (o1 as System.ValueType).Equals(Convert.ChangeType(ZeroValue, o1.GetType()))
}
else
{
if (o1.GetType() == typeof(String))
{
Passed = o1.Equals(String.Empty);
}
}
}
return Passed;
}
What's wrong with this?
public static bool IsZeroOrEmptyString(object obj)
{
if (obj == null)
return false;
else if (obj.Equals(0) || obj.Equals(""))
return true;
else
return false;
}
Michael, you need to provide a little bit more information here.
strings can be compared to null or string.Empty by using the method
string x = "Some String"
if( string.IsNullOrEmpty(string input) ) { ... }
int, decimals, doubles (and other numeric value-types) can be compared to 0 (zero) with a simple == test
int x = 0;
if(x == 0) { ... }
You can also have nullable value-types also by using the ? operator when you instantiate them. This allows you to set a value type as null.
int? x = null;
if( !x.HasValue ) { }
For any other object, a simple == null test will tell you if its null or not
object o = new object();
if( o != null ) { ... }
Hope that sheds some light on things.
Not quite sure the reasoning behind this, because .Equals is reference equality on reference types, and value equality on value types.
This seems to work, but I doubt its what you want:
static bool IsZeroOrEmpty(object o1)
{
if (o1 == null)
return false;
if (o1.GetType().IsValueType)
{
return (o1 as System.ValueType).Equals(0);
}
else
{
if (o1.GetType() == typeof(String))
{
return o1.Equals(String.Empty);
}
return o1.Equals(0);
}
}
Do you mean null or string.empty, if you're talking about strings?
if (String.IsNullOrEmpty(obj as string)) { ... do something }
Oisin
In the first case by testing if it is null. In the second case by testing if it is string.empty (you answered your own question).
I should add that an object can never be equal to 0. An object variable can have a null reference though (in reality that means the variable has the value of 0; there is no object in this case though)
obj => obj is int && (int)obj == 0 || obj is string && (string)obj == string.Empty

Categories

Resources