Among the things that are hard in programming, naming a function or variable takes the top place.
In case you are writing a function that returns a Boolean, prefixing the method name using is
or can
or should
is a great idea. Here are some examples.
Dos
isActive
shouldLogin
canLogin
hasAccess
canDelete
hasValidToken
doesTokenExist
Remember to keep the method name + tone positive
Please Don't Do
isNotActive
cannotLogin
hasNoAccess
hasinvalidToken
In fact, There is more to this. The name of the function can be driven by the intent or action or check being done.
Are you checking some
State
? Prefix withis
--> isActiveAre you checking
possibility of an Action
? Prefix withcan
--> canDeleteAre you enforcing some
Condition
? Prefix withshould
--> shouldLoginAre you checking for a
resource
? Prefix withdoes
--> doesTokenExistAre you checking is something is `allowed`? Prefix with
can
--> canDelete
Naming variables is never easy, Keeping these principles in mind could be useful.
Conclusion
If Programming was a "TOUGH" cake, naming variables would be the cherry. It takes a lot of practice and meaningful code contributions to get this right. Keep going and keep trying. Every day is a learning opportunity.
Thank You
If you have reached so far, congratulations. Please feel free to share your thoughts via the comments.
Let's Connect
Feel free to connect with me on LinkedIn and Twitter if you have any questions.
Until Next time!