Is there a better way to check for boolean logic - c#

I have an xml filelike so
<Config>
<Allowed></Allowed>
</Config>
The Allowed tag is read like this:
string isAllowed = (string)xml.Root
.Element("Config")
.Elements("Allowed")
.SingleOrDefault();
isAllowed is supposed to take a default value of true when
The tag is not present
Is present but is empty
Has any other value other than true, false, yes, or no.
Here is the code which does this:
if (isAllowed == null)
{
DoSomething();
return true;
}
if (isAllowed.Length == 0)
{
DoSomething();
return true;
}
if (isAllowed.Length != 0)
{
if (isAllowed.ToUpper() != "FALSE" && isAllowed.ToUpper() != "NO")
{
DoSomething();
return true;
}
}
There's got to be a better way to do this?

if (isAllowed == null)
{
DoSomething();
return true;
}
if (isAllowed.Length == 0)
{
DoSomething();
return true;
}
Can be replaced with:
if (string.IsNullOrEmpty(isAllowed)
{
DoSomething();
Return true;
}
But actually, given your criteria I think string.IsNullOrWhiteSpace(isAllowed) would be more appropriate as it will return true if the tag's contents are "empty".
Also, you don't need the following condition a second time, because if if the condition was met the first time around the function will return (short circuit evaluation). This means that the statements you currently have in the second If block would never be executed anyway.
if (isAllowed.Length != 0)
My first instinct to make this cleaner was to take the same approach as Jon did in his answer, there's no advantage in repeating it. However, I did consider this as another good design as should you introduce more conditions it will be much cleaner:
private static bool Validate(string isAllowed)
{
var defaultTrueConditions = new[] {"true", "false", "yes", "no"};
if (string.IsNullOrWhiteSpace(isAllowed) ||
defaultTrueConditions.Contains(isAllowed, StringComparer.OrdinalIgnoreCase))
{
DoSomething();
return true;
}
return false;
}

It sounds like you might be better off like this:
// This deals with the nullity aspect. (The "Yes" is just for clarity - it could
// be any value other than "No" or "False" in some form.)
isAllowed = isAllowed ?? "Yes";
bool isFalse = isAllowed.Equals("No", StringComparison.OrdinalIgnoreCase) ||
isAllowed.Equals("False", StringComparison.OrdinalIgnoreCase);
return !isFalse;
Basically the fact that you're defaulting to true means that the return value should only be false if you find an element and it's got a value of No or False, in a case-insensitive way. Note that I've used an ordinal match here - you may want to change that, e.g. to CurrentCultureIgnoreCase or InvariantCultureIgnoreCase.
It's not clear where the DoSomething method call comes in, but I would separate that out anyway. Write one method which just determines the appropriate value, as shown above - and then have:
bool allowed = CheckAllowed(doc);
if (allowed)
{
DoSomething();
}
// And use allowed here too
That's a much cleaner separation to my mind.

Related

Check if value exists. Equals(...) vs. patternMatching

I want to check if a given object value is null or is an empty string. I got the following ideas:
...
public void Cmp1(object a) {
if(a == null || Equals(a, string.Empty)) {
Value = null;
return;
}
....
}
public void Cmp2(object a) {
if(a == null || (a is string stringValue && string.IsNullOrEmpty(stringValue))) {
Value = null;
return;
}
....
}
Which one is better? Is Equals exposing some danger? Or (a is string stringValue && string.IsNullOrEmpty(stringValue)) is super slow?
The problem with the second option is you almost certainly want to use stringValue after the conditional block, meaning you must do the work a second time.
I might use the as operator, like this:
public void Cmp(object a)
{
var stringValue = a as string;
if (string.IsNullOrEmpty(stringValue)) {
Value = null; //this suggests the function should return something, instead of a void method
return;
}
// ...
}
Or I might negate the condition (and remove the null check):
public void Cmp(object a) {
// pattern matching is still safe to use if `a` is null
if(a is string stringValue && !string.IsNullOrEmpty(stringValue)) {
// stringValue is in scope here
}
else {
// but not here... but we no longer need it
Value = null;
}
}
If you don't intend to use stringValue again at all, I'd write it like this:
public void Cmp(object a)
{
if (!(a is string) || string.IsNullOrEmpty((string) a)) {
Value = null;
return;
}
// ...
}
But most of all... none of this is likely to drive the performance of your program. You're spending a bunch of time worrying about details that would be much better spent looking at profiler results, which can tell you exactly where to spend your time optimizing to make your app faster.

While loop multiple condition check with single variable

I am wondering if there is a way to check multiple while loop conditions using the same variable. That's a bit vague, but this example should clear it up:
while (myFunction(x) == 0 || myFunction(x) == 13639 || myFunction(x) == -4261.9583)
{ x++; }
Is it possible to only evaluate myFunction(x) once per loop while checking the three conditions, so that the function doesn't have to run three separate times for a result that is the same each time?
I am doing this for optimization/efficiency purposes. myFunction() could be a pretty time-consuming function, so I want it to run the minimum amount of times necessary.
Typically I would define the value of myFunction(x) before I start the while loop, but in this case, the value of myFunction(x) will be changing as the loop goes through each iteration, since the value of x will be changing.
Yes, this can be done:
double result;
while ((result = myFunction(x)) == 0 || result == 13639 || result == -4261.9583)
{ x++; }
Three options:
Do it backwards
Check a list for the result rather than checking the result against the list.
var list = new float[] { 0F, 13639F, -4261.9583F );
while (list.Contains(myFunction(x))
{
x++;
}
Write a function
Extracting the logic to another function is always a nice way to break down the problem.
bool IsValid(float input)
{
var result = myFunction(input);
return (result == 0 || result == 13639 || result == -4261.9583);
}
while (IsValid(x))
{
x++;
}
Use while(true)
Whenver the condition of a while loop is complicated, a common option is to remove the check from the () and put it in the {} instead. When you do this, use while (true).
while (true)
{
var result = myFunction(x);
if (result != 0 && result != 13639 && result != -4261.9583) break;
x++;
}
You can do this simply by moving the check into a separate method, which takes the return value of the function as parameter, and then performs the 3 checks on the value directly and returns a boolean accordingly.
Assuming the function returns a int typed value this may for example look something like this:
bool CheckResultValue(int value) {
return value == 0 || value == 13639 || value == -4261.9583;
}
Then your while loop could look something like this:
while (CheckResultValue(myFunction(x)))
{ x++; }

C# - Foreach loop with if statement

How can I go about doing this so if the "if" statement is true, to skip the code below the foreach loop and to go on with the rest of the program
void()
{
foreach()
{
if()
{
}
}
//code I want to skip if "if" statement is true
}
There's no way to directly do what you want (without "goto" labels -- perish the thought!), but you can use the "break" keyword, and set a variable you can refer to later.
void()
{
var testWasTrue = false;
foreach()
{
if()
{
testWasTrue = true;
break; // break out of the "foreach"
}
}
if( !testWasTrue ) {
//code I want to skip if "if" statement is true
}
}
I know this was already answered, but I figured I'd throw in my 2 cents since nobody considered abstracting the check to a separate method:
void()
{
if (ShouldDoStuff(myCollection))
DoStuff(myCollection);
else
DoOtherStuff(myCollection);
}
private bool ShouldDoStuff(collection)
{
foreach()
{
if ()
return true;
}
return false;
}
This provides a much cleaner code at the higher level for dealing with your algorithms and removes all the clutter discussed about. It cleanly separates the tasks in void() of checking and performing the actions and readers instantly know exactly what the program flow is without having to discern what they're doing with a boolean or break logic lurking about. No single method has more than 1 responsibility or task.
Yeah, it's possible the poster wants to do other work in their foreach, but that's an entirely different discussion and not what was described in their question. If you simply want to check if the given collection (or object) satisfies a certain condition, that check can be moved to a separate method. Even leaves the door open for automated unit tests for all three components.
Even if DoStuff and DoOtherStuff are not abstracted to their own methods, it provides nicer readability and logical flow.
void()
{
bool process = true;
foreach()
{
if()
{
process = false;
break;
}
}
if (process)
{
//code I want to skip if "if" statement is true
}
}
As was mentioned in my comment you may do this through extra bool variable.
void()
{
bool positiveResult; // by default it gets false value
foreach()
{
if()
{
positiveResult = true;
// you may use "break" to skip the loop
break;
}
}
if( !positiveResult )
{
//code I want to skip if "if" statement is true
}
}
The 'break' keyword will break out of the loop.
foreach (someClass a in someArray)
{
if(a.someProperty) // bool property
{
//Stuff to do if that condition is true
doSomethingElse();
//Calling the break keyword will stop the loop and jump immediately outside of it
break;
}
//Other code to run for each iteration of the loop
}
//Here is where execution will pick up either after break is called or after the loop finishes
Only way I know how is a bool flag.
void()
{
bool x = false;
foreach()
{
if()
{
x = true;
break;
}
}
if(!x)
{
//Code to skip if "if" statement is true.
}
}
Not super elegant, but easy.
Edit: beat by 12 secs :)
void()
{
bool skip = false;
foreach()
{
if()
{
skip = true;
}
}
if(!skip)
{
//code I want to skip if "if" statement is true
}
}
If the collection you are iterating through contains The IEnumerable Interface, You could use Any() with a Lambda!
int[] myArray = { 1, 2, 3 };
if( myArray.Any((a) => a == 1) )
{
return;
}
It is read: if my array contains any value a where a is equal to 1, then return out of this function.
Plus if you want to make it harder to read, you can omit the curly braces/brackets.
if( myArray.Any((a) => a == 1) )
return;

c# understanding the bool expression

This is a really simple question I am sure, but I cannot figure out why this assertion fails...
basically if IsApple is false or IsBannana is false assertion should fail, however if one of the two is true assertion should pass, could anyone explain why this assertain fails?
[Test]
public void IsApplesOrBannans()
{
bool IsApple = true;
bool IsBannana = false;
if (!IsApple || !IsBannana)
Assert.Fail();
Assert.Pass();
}
What you're saying makes no sense.
Here's how I (and the compiler) understand what you're saying:
basically if IsApple is false or IsBannana is false assertion should fail
If IsApple is false the assertion should fail
If IsBanana is false, the assertion should fail
In other words, if one of them is false, you don't care whether or not the other is also false.
however if one of the two is true assertion should pass
If one of them is true, you don't care whether the other one is also true.
Those requirements contradict each others.
Perhaps you meant "if IsApple is false AND IsBanana is false"; that is, if they are both false.
But what you wrote was "if IsApple is false OR IsBanana is false", that is, if one of them are false.
!IsBannana is true so the if evaluates to true.
I bet you wanted:
if(!IsApple && !IsBananna)
Assert.Fail();
The assert will fail if EITHER is false, it will only pass if BOTH are true.
Edit: I would re-write this as:
if (IsApple || IsBanana)
{
Assert.Pass();
}
else
{
Assert.Fail();
}
Interestingly, the question as posed cannot be answered (since if one is true and the other is false according to the question the expected result is amiguous).
Do this instead:
if(!IsApple && !IsBannana)
if (IsApple != IsBannana)
Assert.Fail();
I think this trick != is a C# FAQ as poor man's XOR
You dont want the case where neither a nor b is false, or rather at least one should be true, so
[Test]
public void IsApplesOrBannans()
{
bool IsApple = true;
bool IsBannana = false;
if (!(IsApple || IsBannana))
Assert.Fail();
Assert.Pass();
}
Your pass condition is IsApple || IsBannana, so your fail condition can be written as:
if (!(IsApple || IsBannana))
Or alternatively you can say both must be false to fail:
if ((IsApple==false) && (IsBannana==false))
Which usually is written as:
if (!IsApple && !IsBannana))
Personally I think the first of these three alternatives is the best. But that's just a stylistic choice. functionally they are equivalent.
Or even better, you could switch your then and else part of the if:
public void IsApplesOrBannans()
{
bool IsApple = true;
bool IsBannana = false;
if (IsApple || IsBannana)
Assert.Pass();
else
Assert.Fail();
}
Or even throw out the if statement entirely:
public void IsApplesOrBannans()
{
bool IsApple = true;
bool IsBannana = false;
Assert.IsTrue(IsApple || IsBannana);
}
Your pass condition contradicts your fail condition, because if only one is true, the other one is false and thus fulfills the fail condition.
if you want it to make sense, change it to this:
if (!IsApple && !IsBanana)
Assert.Fail():
or, if you also don't want it to be an apple and banana at the same time
if (IsApple == IsBanana)
Assert.Fail():
(!A|| !A) = !!(!A || !B) = !(A && B) = !(true && false) --> false

What does this snippet of C# code do?

What does result.IsVisible equal?
if(a==b)
result.IsVisible = obj1.status.abc_REPORT == 'Y'
&& obj1.AnotherValue.ToBoolean() == false;
That depends on the values of obj1.status.abc_Report and obj1.AnotherValue.ToBoolean() (and it all depends on whether a==b or not).
I'm not quite sure of what the real question is here - which bit is confusing you?
One bit which may be confusing you is the shortcircuiting && operator (and possibly the lack of bracing!)
The && operator will only evaluate its right hand side if the left hand side evaluates to true: and the overall result of the expression is true if and only if both sides evaluates to true. (I'm assuming no strange user-defined conversions here.)
So another way of writing it would be:
if (a == b)
{
bool visibility = false;
if (obj1.status.abc_REPORT == 'Y')
{
if (obj1.AnotherValue.ToBoolean() == false)
{
visibility = true;
}
}
result.IsVisible = visibility;
}
Note that a condition comparing Booleans, like this:
obj1.AnotherValue.ToBoolean() == false
would usually be written like this:
!obj1.AnotherValue.ToBoolean()
(Note the exclamation mark at the start - the logical "not" operator.)
The same as this, in many less lines:
if (a==b) {
if (obj1.status.abc_REPORT == 'Y') {
if (obj1.AnotherValue.ToBoolean() == false) {
result.IsVisible = true;
}
else {
result.IsVisible = false;
}
}
else {
result.IsVisible = false;
}
}
In simple words:
If a is equal to b:
result will be visible only if:
object1's status's abc_report is Yes(Y = Yes most probably) AND object1's other value cannot be converted to a Boolean
I'm guess result.IsVisible is a boolean
It will be true if the following conditions are true:
obj1.status.abc_REPORT == 'Y'
and
obj1.AnotherValue.ToBoolean() == false
Also, a == b must be true to enter the initial if
lets go line by line:
if(a==b)
obvious if value of a equals value of b the execute following line
result.IsVisible = obj1.status.abc_REPORT == 'Y'
&& obj1.AnotherValue.ToBoolean() == false;
result is some object (maybe winforms controls etc) which has a property IsVisible set it to true if obj1.status.abc_REPORT is equal to 'Y' and also obj1.AnotherValue.ToBoolean() is equal to false;

Categories

Resources