CaffGeek
CaffGeek

Reputation: 22064

A or B, not both, not neither

Ever since reading Clean Code I have been trying to keep my code descriptive and easy to understand. I have a condition where either A or B must be filled in. But not both. And not neither. Currently the if statement to check for this condition is hard to follow at a glance. How would you write the following to make it clear at a glance what is being checked

if ((!string.IsNullOrEmpty(input.A) && !string.IsNullOrEmpty(input.B)) 
    || string.IsNullOrEmpty(input.A) && string.IsNullOrEmpty(input.B))
{
    throw new ArgumentException("Exactly one A *OR* B is required.");
}

Upvotes: 9

Views: 4221

Answers (8)

Justin Niessner
Justin Niessner

Reputation: 245439

Time for an XOR:

if(!(string.IsNullOrEmpty(input.A) != string.IsNullOrEmpty(input.B)))
    throw new ArgumentException("Exactly one A *OR* B is required.");

You may also see it written as:

if(!(string.IsNullOrEmpty(input.A) ^ string.IsNullOrEmpty(input.B)))
    throw new ArgumentException("Exactly one A *OR* B is required.");

Upvotes: 24

Pratik Deoghare
Pratik Deoghare

Reputation: 37172

What you need is called XOR i.e. exclusive OR operation.

Truth table will reveal it to you ;)

A   B   ⊕
F   F   F
F   T   T
T   F   T
T   T   F

In some languages(or in most of them) it is denoted by A ^ B.

good wiki article

Upvotes: 1

Hendrik
Hendrik

Reputation: 2041

Its an XOR, and its really easy to emulate.

Just to think about it:

Both cannot be true, both cannot be false. One has to be true, one has to be false.

So, we come to this:

if(string.IsNullOrEmpty(input.A) == string.IsNullOrEmpty(input.B)) {
   throw new ArgumentException("Exactly one A *OR* B is required.");
}

If both are equal, they are either both true, or both false. And both cases are invalid.

And all that without any special XOR operator that the language of choice might not have. ;)

Upvotes: 12

Nordic Mainframe
Nordic Mainframe

Reputation: 28757

if (string.IsNullOrEmpty(input.A) != string.IsNullOrEmpty(input.B)) {
 // do stuff
}

Upvotes: 13

Ian Jacobs
Ian Jacobs

Reputation: 5501

what you're looking for is XOR ( http://en.wikipedia.org/wiki/Exclusive_or ) logic.

You can write it as:

if (string.IsNullOrEmpty(A) ^ string.IsNullOrEmpty(B))
{
//Either one or the other is true
}
else
{
//Both are true or both are false
}

Upvotes: 2

falstro
falstro

Reputation: 35667

This is the very definition of exclusive or. There are a bunch of ways using boolean algebra, the simplest one is to use a XOR operator. In C, there's no logical xor though, but you can use the binary one, doubling the not operator to force any truth value to be one (as in 0x01)

!!string.IsNullOrEmpty(input.A) ^ !!string.IsNullOrEmpty(input.B)

Or do the negative test

!string.IsNullOrEmpty(input.A) ^ !string.IsNullOrEmpty(input.B)

which will be true if both A and B are set, or neither.

Upvotes: 0

Donald Miner
Donald Miner

Reputation: 39913

This relationship is called exclusive-or (xor).

Some languages provide it as an operator -- typically ^:

True ^ True -> False
True ^ False -> True
False ^ True -> True
False ^ False -> False

Upvotes: 7

Mitch Wheat
Mitch Wheat

Reputation: 300669

Use an exclusive-OR: A XOR B

Upvotes: 3

Related Questions