The Invent with Python Blog

Writings from the author of Automate the Boring Stuff.

A Common Programmer Mistake: Dog is not the Opposite of Cat

Thu 21 June 2012    Al Sweigart

True is the opposite of false. Up is the opposite of down. What is the opposite of dog?

If you replied “cat”, what exactly makes cats the opposite of dogs the same way up is the opposite of down?

I use this example to point out a common problem that software developers make with variable and parameter names. When there are two possible values for some variable or parameter, it is very common that it should be defined as a boolean type.

For example, a parameter might be called foobarEnabled and a True or False value is passed for it. This makes sense. The foobar is either enabled or disabled. “Not enabled” has a distinct and unambiguous opposite value, “disabled”. A boolean here makes perfect sense.

However, there is also a case when the two (and only two) possible values are not necessarily opposites. In this case, a boolean value is the wrong data type to use. On a recent web app I was working on, there is a form that has an “add foo” and “edit foo” mode, depending on whether the widget is adding a new foo or editing an existing foo (they use pretty much the same form fields, but the backend needs to know for technical reasons.)

To differentiate between them, the function that handles this form has an isAddMode parameter. If the form was submitted in Add mode, a True value is passed to this parameter. If the form was submitted in Edit mode, a False value is passed to this parameter. Knowing that “add” and “edit” are the only two values, this makes sense to the programmer writing this code. (But then again, programmers always think the code they themselves write makes sense.)

But from the perspective of another programmer who has to maintain this code later, this is the completely wrong name and data type for this parameter. Having “not add mode” does not instinctively convey “edit mode” the same way “not enabled” conveys “disabled”.

Dog is not the opposite of cat.

The correct name for a parameter like this would be something like “mode” or “addOrEditMode” depending on how verbose you want to be. The correct data type for a parameter like this is an enum type with two values for “add” and “edit”.
It may seem like a trivial detail, but making your code as readable as English is a key to having cleanly written code which is easy to understand and easy to debug.

(By the way, if you would like to know what the opposite of “cat” is, it’s “triceratops”.)


Learn to program for free with my books for beginners:

Sign up for my "Automate the Boring Stuff with Python" online course with this discount link.

Email | Mastodon | Twitter | Twitch | YouTube | GitHub | Blog | Patreon | LinkedIn | Personal Site