Leaving the code in an else if() blank in order to escape the if statement

steve john :

I'm sorry for the strange title, I couldn't find the exact wording that I wanted but I'll do my best to explain my question here. Basically I have some code that goes like this

if(both inputs are not null)
{
    Do this
}
else if(both inputs are null)
{

}
else if(one input is null and the other isn't)
{
    throw new Exception("Both inputs must have a value or neither should");
}

if I don't use the middle else if the last else if, the program will throw the exception no matter whether one or both inputs are null. I'm wanting it so that the program sees that both inputs are null and does nothing while continuing with it's execution. I'm using this data to pass to a SQl query and if one of the inputs are null it acts up. I might just be messing up the logic but I was wondering if this is considered bad practice. I can't think of a problem because there isn't a way that this could execute code accidentally. If there is a better way or if this is considered bad practice I would like to hear other ways to go about this. Thanks.

EDIT: clarified question

Juan Gallardo :

You can simplify the code if the language you are using has an Exclusive OR operator. For example in C#:

string A = null;
string B = "Hello World";

if ( A != null && B != null)
{
    // Do this
}
else if ( A == null ^ B == null )
{
    throw new Exception("Both inputs must have a value or neither should");
}       

The result of x ^ y is true if x evaluates to true and y evaluates to false, or x evaluates to false and y evaluates to true.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=418863&siteId=1