How to Name Booleans Values
I have been reviewing code for a few months now and many people are not naming the variables right. Especially the booleans, so “How to name the boolean values?”
A boolean variable serves as a truthiness holder in programming. Naming variables, especially boolean ones, is often considered one of the most challenging aspects of programming due to its subjective nature. Finding a good name for variables, particularly boolean ones, can be time-consuming. Proper naming is crucial to enhance program readability, aiding fellow programmers in understanding the ideology and thoughts behind the code. Effective communication and networking systems are built collaboratively by peer coders.
This post aims to share insights on designing meaningful boolean variable names and provides a helpful list of examples to guide you.
Tips on Naming Conventions:
Categorization: Categorizing these tips provides a perspective on tackling naming complexities.
Approach: When naming boolean variables, always approach from the positive side in your naming convention. Embrace the truthiness of the boolean, making it easier to comprehend. For instance:
isVisible
is more comfortable thanisInvisible
.!isVisible
is simpler than!isInvisible
.
By adopting a positive perspective, unnecessary complexity is reduced, especially when dealing with negation statements related to the variable.
Prefer Nouns: Simplify names using easily recognizable nouns. Stick to the formula: is/has + Noun
. Prefer nouns over verbs for simplicity. For example:
shouldShow
can be simplified toisVisible
.
Stay in the Present: Use present tense in variable names. For example:
isVisible
is preferable overwasVisible
.
Present tense aligns with the code execution, making it more straightforward.
Examples: Here’s a list of good boolean variable names compared to less optimal ones to help illustrate the concepts discussed in this post.
+=================+===============================================+
| Good | Not so good |
+=================+===============================================+
| isOpen | isClosed |
+-----------------+-----------------------------------------------+
| isVisible | isInvisible, isHidden, isShowing, show, hide |
+-----------------+-----------------------------------------------+
| isResolved | isRejected |
+-----------------+-----------------------------------------------+
| isRight, isGood | isWrong, isLeft, isBad |
+-----------------+-----------------------------------------------+
| hasValue | isEmpty |
+-----------------+-----------------------------------------------+
| isDefined | isUndefined |
+-----------------+-----------------------------------------------+
| isOwner | isOwning, own, wasOwner, willBeOwner |
+-----------------+-----------------------------------------------+
Naming boolean values is an important aspect of writing clean and readable code. Here are some best practices for naming boolean variables:
1. Use Positive Affirmations:
— Choose names that reflect the positive state or action.
— Example: Instead of `isNotRunning`, use `isRunning`.
2. Use Clear and Concise Names:
— Keep names short and to the point while maintaining clarity.
— Example: Instead of `userIsLoggedIn`, use `isLoggedIn`.
3. Avoid Negations:
— Try to avoid using negations in boolean variable names.
— Example: Instead of `isNotEnabled`, use `isEnabled`.
4. Be Explicit:
— Make sure the name clearly indicates the purpose or condition.
— Example: Instead of `flag`, use a more descriptive name like `isFlagSet`.
5. Consistent Naming Conventions:
— Follow a consistent naming convention across your codebase.
— Example: If you use camelCase for variables (`isRunning`), stick to it consistently.
6. Use Question Form for Functions:
— When dealing with functions returning boolean values, consider using a question form.
— Example: Instead of `validate()`, use `isValid()`.
7. Group-Related Booleans:
— If you have multiple boolean values related to a specific functionality, group them with a common prefix.
— Example: `isLoading`, `isSuccessful`, `hasError`.
8. Avoid Ambiguity:
— Ensure that the name is not ambiguous and clearly represents the intended meaning.
— Example: Instead of `flag`, use a more specific name like `hasPermission`.
9. Consider Enumerations:
— For multiple related boolean values, consider using enumerations or bit flags.
— Example: Instead of multiple boolean variables, use an enum like `Status { Pending, Approved, Rejected }`.
10. Update Names When Conditions Change:
— If the meaning or conditions change, update the variable name accordingly.
— Example: If the condition represented by `isActive` changes, update the name to reflect the new meaning.
Remember, the goal is to make your code easily understandable for both yourself and other developers who may work with your code.
Thanks a lot for reading this article. If you like it or have some suggestions, feel free to chip in via comments. Have a nice day!