We help IT Professionals succeed at work.

# Rewrite of if else logic

on
I want to rewrite this peice of code below, because if the condition is true then nothing is done. Which is what the does is supposed
to do, but I don't like the idea of nothing being done inside of the braces. So I have come up with two alternative pieces of code, marked as Question 1 and 2 below. Can someone validate my rewritten code logic that I labeled as Question 1 and 2 below?
// Here is the old code :
if (GamePlayer[2].hasJumped == true && iterator == 2)
{
// do nothing
}
else
{
GamePlayer[iterator].Processed = true;
}

Question 1:

So would the following new code rewrite be correct :

if (!(GamePlayer[2].hasJumped == true && iterator == 2))
{
GamePlayer[iterator].Processed = true;
}

Or also would this be correct as well:
Question 2:

if (GamePlayer[2].hasJumped == false || iterator != 2)
{
GamePlayer[iterator].Processed = true;
}
Comment
Watch Question

## View Solutions Only

Software Engineer
Commented:
Let me start by saying that having an empty code block can be valid in terms of readability/maintainability, maybe just have a more meaningful comment describing why in that case nothing is done.

If you can't think of a more meaningful description then the first transition can also be the best way to code if the way a human would read that condition makes sense in that form as how they would think about that particular situation taking place, and would still makes sense thinking about it as "not  that particular situation". Remember, the computer will understand either way but it may be far more difficult to read for a human when normalized like you seem to desire.

That being said, the second transition is also correct as you can see here: https://www.wolframalpha.com/input/?i=!%28P+||+Q%29

Boolean logic has similar normalization rules like multiplication: (a + b) * x = a * x + b * x but with not  there is this interesting situation where it flips between or/and. You can verify this by looking at the truth tables:
``````a AND b

|   a   |
+---+---+
| F | T |
--+---+---+---+
| F | F | F |
b +---+---+---+
| T | F | T |
--+---+---+---+

=======================
!(a AND b)

|   a   |
+---+---+
| F | T |
--+---+---+---+
| F | T | T |
b +---+---+---+
| T | T | F |
--+---+---+---+

=======================
!a OR !b

|   a   |
+---+---+
| F | T |
--+---+---+---+
| F | T | T |
b +---+---+---+
| T | T | F |
--+---+---+---+
``````
or in words: the AND operation is only true when both  expressions are true. Because you only want the else part you put a NOT around the entire expression; that expression is true unless  both expressions are true. And finally the normalized expression shows the same table as the previous one, that could be described as: a or b needs to be false (or both).

Or in a slightly different form:
Most Valuable Expert 2011
Top Expert 2015
Commented:
having an empty code block can be valid in terms of readability/maintainability
I think I'll disagree...for the majority. It should be exceedingly rare that you ever have an empty code block. I think it's more plausible that you should refactor your code in such a way that the empty block is not needed. In the above, I would advocate that writing the code without the empty block would be more readable.

Now, to get all computer-sciencey on you, keep in mind DeMorgan's laws. Basically, what they say is that when you have a certain set of boolean conditions, you can get the opposite of that condition by negating each condition (i.e. turning each true condition to a false condition, and each false condition to a true condition) and using the opposition conditional operator (i.e. turn ANDs into ORs and ORs into ANDs).

You can see this in action in your second snippet above. Whereas you originally had "hasJumped" compared to true, you changed it to false, and whereas you had "iterator" compared equal to 2, you changed it to not equal. Then you changed your && to a ||.
Freelance programmer / Consultant
Commented:
In simple english.
You have this
``````if (GamePlayer[2].hasJumped == true && iterator == 2)
{
// do nothing
}
else
{
GamePlayer[iterator].Processed = true;
}
``````

which if you changed to this is the same
``````if( ! (GamePlayer[2].hasJumped == true && iterator == 2) )
{
GamePlayer[iterator].Processed = true;
}
``````

note the !
That means not and as the other experts have explained the not swaps a true to false or vice versa.  (Also the complete condition for the if statement is enclosed in brackets)
contractor

Commented:
Thanks Guys,

I kind of like the idea of the empty code block. Having gone through many code reviews, they would have not allowed for that. But hey, it's my party and I will cry if I want to right? In other words, it's my project and I'll do what I want to. Thanks you all