Naming Boolean Flags

A Boolean name should allow a reader to interpret the true case as a complete statement about the domain.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Choose one meaning for true
  2. Make the boundary easy to read
  3. Choose a name that reads as a complete question
  4. Trace a negation in a case with a clear outcome

Choose one meaning for true

A flag named is_closed answers whether a venue is closed. A test named can_enter may also depend on a ticket and capacity. Keep these separate if both facts are useful. Predicate functions can centralize the derived rule.

Make the boundary easy to read

A conditional combining is_closed with a negation and an exception for staff can be checked from a few concrete rows. Test a closed venue for staff and for a guest. A truth table documents the policy before renaming or simplifying the expression.[1][2]

Choose a name that reads as a complete question

In a room booking service, is_closed is a fact about the room. can_reserve can include whether the room is open, the request is valid, and enough seats are free. A variable named status gives no clue about which of these facts a true value would mean. Give Boolean results names that let a reader verify the conditions at a glance. See predicate functions for making the question reusable.

Trace a negation in a case with a clear outcome

Suppose editing is allowed when review is pending and the writer has permission. Double negatives can obscure whether a guard rejects unauthorised actions or allows eligible ones. List the expected answers for pending and not pending, then check both permissions. Keep the accepted case visible in the truth table.

Consider the lifetime of a flag as well. A flag computed from an earlier state can become stale after a change. If the name implies current permission, the operation may need a fresh check. A test modifying the underlying state before use can reveal unintended caching.

References

  1. Python Software Foundation, Python Tutorial: Control Flow ↩
  2. Python Software Foundation, Python Language Reference: Expressions ↩