Positive wording in Boolean names


Link to this posting

Postby Ursego » 21 Feb 2013, 21:37

Give DB fields, variables and functions of type Boolean 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 - "negation of negation" :twisted: !

Negative words are not as problematic as the direct negation, but it's better to avoid them too. The following list contains a few typical examples of NEGATIVE WORDS (TO AVOID) and POSITIVE WORDS (TO USE INSTEAD):

disabled -> enabled
closed -> open
disallowed (prohibited) -> allowed
hidden -> displayed (shown)
excluded -> included
failed -> ok (succeeded, successful)
doesn't exist (absent, missing, omitted) -> exists
expired -> in effect
undefined -> defined
unneeded -> needed

Here we discuss only Boolean identifiers. Of course, it's absolutely ok to use negative words in the names of non-Boolean DB fields and variables (like expiredPoliciesNum) and even Boolean if the negative word if not a part of the flag but describes the flagged entity (like hiddenChargesExist or failedBatchJobRegistered). Also, this rule has nothing to do with non-Boolean functions which perform negative actions - their names should describe exactly what the functions do (like disableSaveButton, prohibitPrinting, excludeInactiveCustomers, expirePolicy).
User avatar
Ursego
Site Admin
 
Posts: 128
Joined: 19 Feb 2013, 20:33



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




cron
free counters

eXTReMe Tracker