Opposite of If statement

Started by
9 comments, last by Darg 13 years ago
Right now I am using this:
if (
world0->blockExists(x-1,y,z,world0->xOffset-location.x,world0->yOffset-location.y,world0->zOffset-location.z)==true &&
world0->blockGetAmount(x-1,y,z,world0->xOffset-location.x,world0->yOffset-location.y,world0->zOffset-location.z)==-1
)
{

}
else
{
code
}


I am trying to clean up this code along with 14 other files. However I can't afford to have the wrong answer.
So what would the if statement need to change to so code can be in the standard if and not the else?

It wouldn't be this, because the && complicates it:

if (
world0->blockExists(x-1,y,z,world0->xOffset-location.x,world0->yOffset-location.y,world0->zOffset-location.z)==false &&
world0->blockGetAmount(x-1,y,z,world0->xOffset-location.x,world0->yOffset-location.y,world0->zOffset-location.z)!=-1
)
If this post was helpful please +1 or like it !

Webstrand
Advertisement
De Morgan's laws
That's some horrid looking code IMHO

You could do this though

bool test = world0->blockExists(x-1,y,z,world0->xOffset-location.x,world0->yOffset-location.y,world0->zOffset-location.z)==true &&
world0->blockGetAmount(x-1,y,z,world0->xOffset-location.x,world0->yOffset-location.y,world0->zOffset-location.z)==-1;

if (!test)
{
...
}


To explain with a readable example (since you need to do 14 other files).

If you have:

if (a && B) {
} else {
// do something
}


You can rewrite it as:

if (!a || !B) {
// do something
}


If your statements inside if () are complex, or you simply can't be bothered with going through it, gazliddon's suggestion is a good alternative. It makes your code a little bit more reabable I suppose. You could also just negate the if statement like this:

// before
if (a && b && c && d && e) {
} else {
// do something
}

// after
if (! (a && b && c && d && e) ) {
// do something
}

Right now I am using this:
if (
world0->blockExists(x-1,y,z,world0->xOffset-location.x,world0->yOffset-location.y,world0->zOffset-location.z)==true &&
world0->blockGetAmount(x-1,y,z,world0->xOffset-location.x,world0->yOffset-location.y,world0->zOffset-location.z)==-1
)
{

}
else
{
code
}





Easier to read = easier to refactor.

bool blockExists = world0->blockExists(x-1,y,z,world0->xOffset-location.x,world0->yOffset-location.y,world0->zOffset-location.z)==true;
bool blockAmountIsInvalid = world0->blockGetAmount(x-1,y,z,world0->xOffset-location.x,world0->yOffset-location.y,world0->zOffset-location.z)==-1;

if (blockExists && blockAmountIsInvalid)
{

}
else
{
code
}


Now you can easily get rid of the else ...


bool blockExists = world0->blockExists(x-1,y,z,world0->xOffset-location.x,world0->yOffset-location.y,world0->zOffset-location.z)==true;
bool blockAmountIsInvalid= world0->blockGetAmount(x-1,y,z,world0->xOffset-location.x,world0->yOffset-location.y,world0->zOffset-location.z)==-1;

if (!blockExists || !blockAmountIsInvalid)
{
code
}


Although without seeing more of the code, that condition doesn't make much sense to me :P


edit: holy flood of answers, Batman

Easier to read = easier to refactor.

bool blockExists = world0->blockExists(x-1,y,z,world0->xOffset-location.x,world0->yOffset-location.y,world0->zOffset-location.z)==true;
bool blockAmountIsInvalid = world0->blockGetAmount(x-1,y,z,world0->xOffset-location.x,world0->yOffset-location.y,world0->zOffset-location.z)==-1;

if (blockExists && blockAmountIsInvalid)
{

}
else
{
code
}





I think this one ^^ is the better. You can even make it more readable by adding more variables:



int xof_param = world0->xOffset-location.x;
int yof_param = world0->yOffset-location.y;
int zof_param = world0->zOffset-location.z;

bool blockExists = world0->blockExists (x-1, y, z, xof_param, yof_param, zof_param);
bool blockAmountIsInvalid = world0->blockGetAmount(x-1, y, z, xof_param, yof_param, zof_param)==-1;

if (blockExists && blockAmountIsInvalid)
{

}
[size="2"]I like the Walrus best.
Thanks everyone! I think the best thing to do is just negate the statement.
I never even thought of that solution :P

Yea the If statements are pretty bad.
I really need to make them more readable somehow.

I Always seem to over complicate things.
If this post was helpful please +1 or like it !

Webstrand

Thanks everyone! I think the best thing to do is just negate the statement.
I never even thought of that solution :P

No, that's the easy way out. The best thing to do it to understand boolean algebra and design the logic to properly describe what you want it to do.


Yea the If statements are pretty bad.
I really need to make them more readable somehow.

I Always seem to over complicate things.

In my opinion, what makes your if-statement ugly and difficult to read is the way your function calls look like. Many calculations and multiple parameters that perhaps should be collected in structures. For example, not only should you factor out the calculations like owl demonstrated, but is there a reason you're not sticking your (what appears to be) vectors in a vector structure to collect three linked parameters into one?

If you do, then your two functions only take two parameters, and one can be calculated outside the function calls. Then you don't even have to calculate the individual boolean results like owl did. What you gain by that is short circuiting, in that the second function call doesn't have to be evaluated if the first one completely determines the outcome of the entire expression. And the if-statement doesn't become messy anyway, since the function calls are clean and compact looking.

[quote name='coderWalker' timestamp='1301522881' post='4792361']
Thanks everyone! I think the best thing to do is just negate the statement.
I never even thought of that solution :P

No, that's the easy way out. The best thing to do it to understand boolean algebra and design the logic to properly describe what you want it to do.


Yea the If statements are pretty bad.
I really need to make them more readable somehow.

I Always seem to over complicate things.

In my opinion, what makes your if-statement ugly and difficult to read is the way your function calls look like. Many calculations and multiple parameters that perhaps should be collected in structures. For example, not only should you factor out the calculations like owl demonstrated, but is there a reason you're not sticking your (what appears to be) vectors in a vector structure to collect three linked parameters into one?

If you do, then your two functions only take two parameters, and one can be calculated outside the function calls. Then you don't even have to calculate the individual boolean results like owl did. What you gain by that is short circuiting, in that the second function call doesn't have to be evaluated if the first one completely determines the outcome of the entire expression. And the if-statement doesn't become messy anyway, since the function calls are clean and compact looking.
[/quote]

Me and Owl must of posted at the exact same time. I didn't even see it, but now I do. I think Owl's is best also
combining it like he did also saves some computing time. Since it just reads from memory and doesn't have to calculate.
If this post was helpful please +1 or like it !

Webstrand

Me and Owl must of posted at the exact same time. I didn't even see it, but now I do. I think Owl's is best also
combining it like he did also saves some computing time. Since it just reads from memory and doesn't have to calculate.


What I posted is exactly what teutonicus proposed but I just made the calculations outside the function call for readability's sake. I don't it's gonna be faster and if it is it'd be a despicable improvement, but I've learned to be willing to trade a few cycles for readability and code maintainability

You might want to explore what Brother Bob proposed: Implementing algebraic vectors into your code:

[font="'Courier New"][/font]
[font="'Courier New"]class vector3[/font]
[font="'Courier New"]{[/font]
[font="'Courier New"] public: // Make it right[/font]
[font="'Courier New"] int x;[/font]
[font="'Courier New"] int y;[/font]
[font="'Courier New"] int z;[/font]
[font="'Courier New"]};[/font]
[font="'Courier New"]
[/font]
[font="'Courier New"]// make a method in class world that combines [/font]blockExists and blockGetAmount
[font="'Courier New"]// the offset seem to be inside world so you don't even need to pass it as an argument.[/font]
[font="'Courier New"]World::BlockValidExists(vector3& v_pos, vector3& v_xof)[/font]
[font="'Courier New"]{[/font]
[font="'Courier New"] return ([/font][font="'Courier New"]blockExists ([/font]pos_vector, xof_vector) && (blockGetAmount(pos_vector, xof_vector)!=-1));
[font="'Courier New"]}[/font]
[font="'Courier New"]
[/font]
[font="'Courier New"]// and then ...[/font]
[font="'Courier New"]
[/font]

[font="'Courier New"]vector3 xof_vector (world0->xOffset-location.x, [/font]world0->yOffset-location.y, world0->zOffset-location.z);
[font="'Courier New"]vector3 pos_vector ([/font]x-1, y, z)


if (world0->BlockValidExists(pos_vector, xof_vector))
[font="'Courier New"]{[/font]
[font="'Courier New"]
[/font]
[font="'Courier New"]
[/font]
[size="2"]I like the Walrus best.

This topic is closed to new replies.

Advertisement