Name booleans carefully.

Give them names that suggest a binary outcome.

When you’re using a boolean variable, you want to make it clear to the reader that this variable can only have one of two values: true or false. Names like “status” or “flag” are bad for booleans.

While “engineFunctioningStatus” may be stored as a boolean, using the word status seems to imply that there maybe several different possible values beyond true or false, such as “degraded” or “performance mode.”

A better boolean name would be something like “engineIsRunning.” It either is, or it isn’t. You want a name that strongly suggests a binary outcome.

Always use positive names. Having to figure out double negatives in your head quickly leads to bugs and errors. You would not want a boolean variable named “engineIsNotRunning.”

Other good examples include “fileIsFound”, “orderIsReadyForShipment”, “deliQueueEmpty”. Use words from the domain to clarify the meaning.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: