BlogPost_BooleanNames #22
@ -9,7 +9,7 @@ desc: Choosing a good boolean variable name
|
||||
Choosing good names for variable is important. They are a form of documentation and can remove [complexity](http://localhost:1313/blog/complexity/) from code. Using a bad name for a variable leads to confusion, bugs, and wasted engineer time. In this blog post I will discuss what makes a variable name good or bad, for [booleans](https://en.wikipedia.org/wiki/Boolean_data_type) in particular.
|
||||
|
||||
## Purpose
|
||||
A boolean's purpose is to toggle between true and false. Therefore, the name should represent something that is true or false.
|
||||
A boolean's purpose is to toggle between true and false. Therefore, the name should represent something that can be true or false.
|
||||
|
||||
## Good Boolean Names
|
||||
|
||||
@ -20,6 +20,8 @@ Variable names for booleans: avoid names that are already a negation (notReady,
|
||||
|
||||
find examples in OSS (Gitea notify)
|
||||
|
||||
The book, _The Elements of Style_<sup>1</sup>, is a concise guide to writing. Some rules can be used as a guide for writing code as well. For example, rule 11, "Put statements in positive form," can be applied to naming boolean variables. Keep boolean names in the positive as well, with one additional requirement: do not use the positive form of a negative statement. `enabled` is in the positive form and a positive statement. `disabled` is in the positive form, but is a negative statement. `notEnabled` is both in the negative form and a negative statement. In writing, it is best to use enabled or disabled, and to avoid "not enabled"; they are more concise and stay in the positive form. The extra requirement of using a positive statement for names is because boolean variables' negations have meaning as well; in writing "not disabled" is a double negative and is discouraged. In code, `!disabled`, is also a double negative.
|
||||
|
||||
|
||||
Read PoP and PoSD for bool blog post
|
||||
Use an unambiguously negatable name. No context should be needed for understanding. Ex: “FlexibleName”
|
||||
@ -28,3 +30,5 @@ Should be in the affirmative (useProd, not noProd)
|
||||
|
||||
|
||||
answerable with yes or no (true or false)
|
||||
|
||||
<sup>1</sup> https://tinyurl.com/y3uxhzxc
|
||||
|
Loading…
Reference in New Issue
Block a user