
Prefix Your Booleans – Learn the Right Way to Name True/False Values in Programming
Hello friends! 👋
Today I want to share something very useful for all of us who are learning coding and programming – how to name boolean variables properly.
If you are learning languages like C++, Java, Python, or JavaScript, you must have already used boolean values – these are variables that are either true or false. For example:
bool active = true; bool edit = false;
But wait... naming variables like active or edit is not the best way. Why? Because just looking at the name doesn’t clearly tell us what it means.
Instead, we should prefix our booleans properly. It makes our code easier to understand for others (and even for our future selves 😅). Let’s learn how to do that with a few examples.
1. Use is for simple states
If the boolean is telling us about a condition or state, like "active" or "authorized", we should start the variable with is.
❌ active
✅ isActive
❌ authorized
✅ isAuthorized
This clearly tells us: “Is it active?” or “Is the user authorized?”
2. Use has for ownership
When you are checking if something has a thing or feature, start with has.
❌ access
✅ hasAccess
❌ subscription
✅ hasSubscription
This way, you can easily understand: “Does the user have access?” or “Does the user have a subscription?”
3. Use should for expected behavior
If your boolean checks whether something should happen or not, prefix it with should.
❌ retry
✅ shouldRetry
❌ continue
✅ shouldContinue
It’s like asking the computer: “Should I retry?” or “Should we continue?”
4. Use can for abilities
When you want to check if someone can do something, like edit or comment, use the prefix can.
❌ edit
✅ canEdit
❌ comment
✅ canComment
So now, the code tells us: “Can the user edit?” or “Can the user comment?”
Why is this important?
When we use good naming habits:
- ✅ Our code becomes more readable.
- ✅ It is easier to debug and update.
- ✅ Others can understand our code quickly.
- ✅ It shows we are writing professional-level code – even as students!
Summary
Boolean variables are very common in programming. They help us make decisions in our code. But naming them properly is the key to writing clean and smart code.
So from now on, remember:
- is for states
- has for ownership
- should for decisions
- can for abilities
Let’s start prefixing our booleans the right way!
If you found this helpful, share it with your friends and classmates. Happy coding!