How to Use Mutual Exclusivity in Programming

Learn this common pattern and prosper

How to Use Mutual Exclusivity in Programming
Photo by Sangga Rima Roman Selia on Unsplash

Framing a problem is the first step to solving. At first, it may appear every problem is unique, but the reality is that common patterns emerge through repetition. Identifying these patterns is critical to quickly and accurately prescribing the answer.

One such common pattern is called mutual exclusivity. You may have heard the phrase “those are not mutually exclusive”. Do you know what that actually means?

In this tutorial, we’ll define mutual exclusivity, examine a real-world example, briefly explain its roots in logic, and finally introduce how it applies to programming.

What is Mutual Exclusivity?

When two items are mutually exclusive, that means that they cannot coexist. Going back to the phrase “those are not mutually exclusive”, this means that the items/states of being are able to coexist.

Liking sports and programming are not mutually exclusive; it’s perfectly valid that one would like both.

A Real-World Example

Expanding on the definition of mutual exclusivity, lets look at a more robust real-world example. Have you ever seen a question that asks “choose all that apply” but also has a “none of the above” as a response? Just in case, here’s an example.A square is... (choose all that apply)
a) rectangle
b) polygon
c) three-dimensional
d) none of the above

In the above context, option D, “none of the above” is mutually exclusive with the other three options. However, options A, B, and C are not mutually exclusive amongst each other.

Therefore, the following are valid responses:A
A, B
A, B, C
D

and the following responses are invalid:A, D
A, B, C, D
B, D

Identifying valid and invalid responses is a crucial step in defining the logic rules needed to express the relationship.

The Exclusive Or (XOR) Gate

The mutual exclusivity relationship is expressed a variety of different ways, but the most concise example is the exclusive or (XOR) gate found in digital logic. Essentially, the XOR gate is defined through the following truth table where inputs A and B have possible values of 1 (on/true) or 0 (off/false).

Using Mutual Exclusivity

So how does this apply to programming? We’ll use Python for the following code examples, primarily because its syntax is simple and semantically close to English.

Because most programming languages do not have a dedicated xor operator, we’ll need to rewrite the relationship in its long form.a = True
b = Falsexor_result = (a and not b) or (b and not a)

Notice that the two variables (a and b) hold boolean values. Furthermore, the xor is expressed as “one is true and the other is false”. Alternatively, since Boolean values are only able to be true or false, if we make the assumption a and b are boolean then we can write the expression as follows:a = True
b = Falsexor_result = a != b

This effectively evaluates to true if they are opposite values (true/false or false/true) and evaluates to false if a are the same value (double true or double false).

Going back to our real-world example, the important step is to treat options A, B, and C as one expression and option D as the second.x = a or b or c # True if any of the three is selected
y = d           # True if d is selectedxor_result = (x and not y) or (y and not x)

Conclusion

So, next time you see a requirement such as “one and not the other” or “mutually exclusive”, you’ll be prepared to distill the problem into its common pattern and solve accordingly!


Please share your experiences, questions, and feedback below. Follow Code 85 for more plain language programming guides. Thanks for reading!

Subscribe to Dreams of Fortunes and Cookies

Sign up now to get access to the library of members-only issues.
Jamie Larson
Subscribe