Boolean Naming Conventions - samuelkripto/styleguide GitHub Wiki

Tags: #coding-standards #clean-code #best-practices

Summary: This guide establishes the standard for writing clear, readable boolean variables across our codebase.


1. The Golden Rule

A boolean variable name should sound like a Yes/No question.

When placed inside a conditional statement (if), the code should read like a natural English sentence.

  • Good: if (isOpen) -> Reads as: "If it is open..."
  • Bad: if (open) -> Reads as: "If open..." (Ambiguous: Is this a command? A state?)

2. Choosing the Right Prefix

While is is the most common prefix, it is not always the most accurate. Choose the auxiliary verb that best describes the nature of the boolean.

A. State & Identity (is, has, uses, exists)

Use these for current properties or ownership.

Prefix Usage Example
is General state or identity. isValid, isVisible, isAdmin
has Ownership or containment. hasChildren, hasErrors, hasAccess
uses Is it currently employing X? usesEncryption, usesCache
Suffix Usage Example
exists Existence checks (often DB/File). fileExists, recordExists

B. Capabilities & Permissions (can, allows, supports, requires)

Use these to define what the system or user is able to do.

Prefix Usage Example
can Ability to perform an action. canExecute, canEdit
allows Permission or policy settings. allowsGuestAccess, allowsDuplicates
supports Technical/Hardware features. supportsBluetooth, supportsIPv6
requires A hard dependency. requiresAuth, requiresRestart

C. Lifecycle & Time (should, will, did, was, needs)

Use these for future intent, past history, or recommendations.

Prefix Usage Example
should Recommendation/necessity (often UI). shouldRender, shouldRetry
will Definitive future outcome. willOverwrite, willBlock
did Completed action (history). didFinish, didTimeout
was Previous historical state. wasCreated, wasSeen
needs Requirement for a future state. needsRefresh, needsMigration

D. Content (contains, includes, matches)

Use these for collections or string validation.

Prefix Usage Example
contains Membership in a collection. containsItem, containsSpecialChar
includes Membership in a collection. includesTax
matches Regex or pattern conformance. matchesFilter, matchesRegex

E. Plurals & Relationships (are)

Use this when checking the state of a group or a mutual relationship between multiple items.

Prefix Usage Example
are Plural states or mutual links. areFriends, areEqual, areAllReady

3. Anti-Patterns (What to Avoid)

🚫 The "Flag" Suffix

Do not put the type in the name. We know it's a boolean; tell us what it means.

  • Bad: loginFlag, validStatus, checkBit
  • Good: isLoggedIn, isValid, isChecked

🚫 Negatives

Keep logic positive. Standard convention is usually isEnabled.

  • Bad: isDisabled, disallowsGuest, excludesItem
  • Good: isEnabled, allowsGuest, includesItem

🚫 Double Negatives

Logic becomes incredibly hard to parse when you negate a negative variable.

  • Bad: isNotEnabled (Checking !isNotEnabled is painful).
  • Good: isEnabled

🚫 Ambiguous Nouns

A noun by itself usually implies an object (String, Class, Dictionary), not a boolean.

  • Bad: user, answer, file
  • Good: isUser, hasAnswer, fileExists

🚫 Third-person Singular Question Form

  • Acceptable: doesFileExist (Valid, answers Yes/No), doesSupportHdr
  • Better: fileExists, hasFile, supportsHdr

🚫 Commands

The word do is almost always a command.

  • Bad: doLog, doUpdate (looks like a function name: function doUpdate() { ... })
  • Good: shouldLog, isLoggingEnabled, isUpdated

4. Language-Specific Reference Table

Conventions vary slightly by language ecosystem. Adhere to the standard of the language you are writing in.

Language Case Style Convention Notes Examples
JavaScript / TS camelCase Prefixes (is, has) strongly encouraged to distinguish from objects. isLoading, shouldRender
Java / C# camelCase Strictly uses prefixes. For JavaBeans, getters must use is. isValid, canExecute
Python snake_case PEP 8 standard. Prefixes are standard. is_valid, has_permission
Ruby snake_case Exception: Methods ending in ? return booleans. Variables use is_. valid? (method), is_admin
Go CamelCase Prefixes common, but omitted if struct/package provides context. IsReady, HasPrefix
Rust snake_case Predicate methods must use prefixes per API guidelines. is_empty, starts_with
Swift / Kotlin camelCase Reads like a sentence. Kotlin properties interop with Java is getters. isUserInteractionEnabled
C / C++ Mixed STL uses snake_case. Application code varies, but prefixes are standard. is_open, has_value

5. Quick Check Checklist

Before committing, ask yourself:

  • Does it start with a verb (is, has, can, etc.)?
  • Does if (variableName) sound like a coherent English sentence?
  • Is it positive? (Avoid isNot... or disable...)
  • Is it free of type noise (Flag, Bit, Bool)?