Positive wording in Boolean names


Link to this posting

Postby Ursego » 21 Feb 2013, 21:37

Give Boolean DB fields, variables and functions positive names. Avoid both - direct negation ("NOT something") and words which carry a tinge of negativity (like "unissued" instead of "issued").

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!

What if we want to ask if the document has not been printed? Is if !docPrinted() better than if docNotPrinted()? Ok, we always have a chance that our Boolean var or method will be used with NOT, but a positive wording will result in ONE negation in the worst case scenario, while a negative wording will result in TWO - "negation of negation", which can be confusing and bug-prone :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):

closed -> open
hidden -> displayed (shown)
failed -> ok (succeeded, successful)
doesntExist (absent, missing, omitted) -> exists
expired -> inEffect

Do not use negative prefixes in names:

excluded -> included
disabled -> enabled
disallowed (prohibited) -> allowed
nonStandard -> standard
unchecked -> checked
undefined -> defined

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: 142
Joined: 19 Feb 2013, 20:33



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




cron
Traffic Counter

free counters

eXTReMe Tracker