# Better Boolean Variable Names π
Coming up with good variable names can always be a challenge. So there is some convention you can follow that makes the process easier. For boolean values, you can simply prefix it with is
, has
, or can
. Just by reading the name, you can easily infer that this variable will give you boolean value. Awesome! π
// Better Boolean Variable Names
// β bad
let person = true;
let age = true;
let dance = true;
// β
Prefix with: is, has, can
let isPerson = true;
let hasAge = true;
let canDance = true;
# Why Variable Names Matter?
Having a well named variable is definitely one of the most important thing for code readability. A good variable name should also provide meaning. That way itβs easy for others to read your code or even yourself in the future. Iβm sure we all had the frustration of going back to our code and wonder what the heck is variable βadtβ. Donβt ask me, I donβt even know. Acronym is probably the worst. Unless, itβs something super common like DVD.
# Bad Variable Names to Avoid
You should be descriptive with your naming. So make sure you avoid these bad variable names.
Avoid these bad variable names:
- β single letter names
- β acronyms
- β abbreviations
- β meaningless names
// Avoid Single Letter Names
let h; // π± huh, what's h??
// Avoid Acronyms
let cra; // I bet you have no idea what this is unless you're from Canada π¨π¦
// Avoid Abbreviations
let categ; // Sure we can deduce you're saying category here, but let's just used the full name, so it's not a guessing game π
// Avoid Meaningless Names
let foo; // what is foo? π§
# Community Feedback
@thecodercoder: Good advice! Descriptive names are way better. I used to try to keep names as short as possible, but realized that they really need to explain what they are!
@tirpus_hahs: It is very important to name variables what it describes so we don't have to comment out code. This allows us to read code like a story telling.
@__offblackYeah: my booleans go to the level of "isAnimatedWhenNotInViewport", "isScrollPositionGreaterThanTolerance" lol
@masonhale: Good suggestion. I also use βdoβ as a prefix for Boolean settings/preferences as in doSendReminder
or doShowDetails
@ben336: This is good advice. Also avoid negative variable names like isNotLoaded. The positive forms are almost always clearer
@sunnysinghio: What do you think about handleValidateForm
considering it's an event handler? It's a more common practice that I've seen, albeit longer than did
.
@styfle: I use handleClick for the function that will handle it and onClick for the property name which typically just passes it through
<Form url="example.example/post" onClick={handleClick} />
@maxstalker: What are your thoughts on adjectives
const sortable = true; // instead of canBeStorted
const hidden = true; // instead os ifShown
is/has + noun
enable/disable + verb