boolean method naming convention

boolean method naming convention

Summary

・ Usable patterns-> is + adjective, has + past participle, triad present verb + noun, auxiliary verb + verb -Anti-pattern-> Yes and No are not clear naming ・ When you want to describe the subject, exceptionally the subject + triad verb

Naming convention list

is + adjective

Ask if you are in an adjective state ex) isEnabled

has + past participle

Ask if you are in a verb state ex) hasSent

3 simple verb + noun

Ask if there is a noun in the state of the verb Can be omitted if the noun is clear ex) hasError

Auxiliary verb + verb

Ask if it is possible and if it should be ex) canRemoveItems ex) shouldContinue

Added on February 21, 2019 Is there an auxiliary verb? None? I received the comment. I often see booleans that use auxiliary verbs, and I think they also apply to the general rule of starting with a verb. Therefore, I added it.

Common mistakes

Yes and No are unclear

ex) checkValid

Literary mistake (not in line with naming conventions)

ex) isEnable, existError → Correctly isEnabled, existsError

When you want to write the subject

For example, when trying to express whether a user exists with boolean, I found the following as an expression in GitLab.

public boolean userExists

public boolean isUserExists

public boolean existsUser

I like the first pattern the most. When I try to express it in English If user exists, ... because. Since there is a rule to start with a verb in principle, there may be a second or third pattern, but it seems strange as an English grammar, so I do not use it much.

There was also a discussion about naming conventions when having a subject on stackOverflow. https://stackoverflow.com/questions/1566745/boolean-method-naming-readability

Even if you look at this, it seems that userExists is the most.

Also, if you have an object called user, you should write user.exists ().

Recommended Posts

boolean method naming convention
[Rails] Naming convention
Collection variable naming convention
Method