Positive wording in Boolean names

Link to this posting

Postby Ursego » 21 Feb 2013, 21:37

Give Boolean variables and functions positive names. Avoid both - direct negation (“NOT something”) and words which carry a tinge of negativity (like “disabled” instead of “enabled”).

Think positively! :P That will simplify reading and understanding of Boolean expressions, especially if they are nested.

For example, compare the expressions if !docNotPrinted() and if docPrinted(): they are logically equivalent, but the positive version is easier to understand. As you see, the difference appears even when the expressions are stand-alone, but imagine if they would be a part of a complex Boolean expression! You can contradict: and what if the calling code wants to ask if the document has not been printed yet? Whether if !docPrinted() is better than if docNotPrinted()? Ok, we always have a chance that our Boolean var or method will be used with NOT, but using of positive wording will cause maximum ONE negation in that situation, while using negative wording can result in TWO of them - "negation of negation"!

Negative words are not as problematic as the direct negation, but it’s better to avoid them too. The following lists contain a few examples of "negative" and "positive" words

NEGATIVE WORDS (TO AVOID): disabled, disallowed (prohibited), hidden, excluded, failed.

POSITIVE WORDS (TO USE INSTEAD): enabled, allowed, displayed, included, ok (succeeded).

Here we discuss only Boolean names. Of course, if your non-Boolean function performs a negative action, its name should reflect exactly that action, for example: disableSaveButton, prohibitPrinting, excludeExpiredRecords.
User avatar
Site Admin
Posts: 120
Joined: 19 Feb 2013, 20:33

IF you want to ((lose weight) OR (have unbelievable (brain function AND mental clarity))) THEN click:

free counters

eXTReMe Tracker