It’s a simple logic, let’s follow it together:
- The boolean variable answers the “yes or no?” question,
- …which is used to do something conditionally,
- …which in many cases is done through the
if
-statement.
In other words,
if (should_do_something) ⇒ do_something()
So here is a conclusion: since we write code not only for the compiler/interpreter, but also for ourselves, let’s write the code in plain English! It’s more natural to read the code statement as a sentence. Hence, it’s natural to start the boolean variable names with verbs because they are used in the if
-clauses!
✅ if (should_do_something) ⇒ do_something()
💩 if (something) ⇒ do_something()
✅ if (can_login) ⇒ login()
💩 if (!user && online) ⇒ login()
✅ <div v-if="hasNotifications">...</div>
💩 <div v-if="user.notifications.length > 0">...</div>
Despite of both lines are clear enough, the top lines are faster to grasp. They require less “mental cache” to virtualize the logic in your mind. Therefore, more “mental energy” left for analyzing the purpose of the code: the more vodka good fellas drink, the less remains for criminals.