This blog post is about how to choose a coding style for boolean returning functions. It represents about 2 hours of research.
tl;dr Since there are sound reasons for several options, it is best to follow convention. For boolean returning functions, use a predicate and favor predicates that start with a verb and sound like a yes/no question.
There are at least three conventions for how to name
boolean returning functions:
- as a question with a yes/no answer e.g.
- as a predicate e.g.
- as an imperative before a question or predicate e.g.
How ought we to name
boolean returning functions, when we can make logically strict arguments for questions, predicates, and imperatives? Choose based on convention. To that purpose, we searched the web for
naming functions or methods that return boolean. What follows is a compilation of the results.
Apple's Cocoa suggest a yes/no question/predicate that starts with a verb.
(BOOL)isEditable; (BOOL)showsAlpha; (BOOL)acceptsGlyphInfo; (BOOL)canHide; (BOOL)shouldCloseDocument;
Interactive Python suggest a yes/no question/predicate prefixed with a verb.
StackExchange 19 points suggest a yes/no question/predicate prefixed with a verb.
isFish hasScales canSwim wasEgg eatAble
StackExchange 12 points suggest a yes/no question/predicate prefixed with a verb.
is_transparent Pixel_is_transparent pixel.transparent at_eof stream.is_eof array.contains(elem)
StackExchange 47 points suggest a yes/no question/predicate prefixed with a verb.
StackExchange 51 points suggest a yes/no question/predicate prefixed with a noun.
public boolean userExists(...)
StackExchange 44 points suggest a yes/no question/predicate prefixed with a verb.
StackExchange 71 points suggest a yes/no question/predicate prefixed with a verb.
The top answers from relevant StackExchange questions support the use of yes/no questions/predicates that start with a verb. There is also some mention of yes/no questions that start with a noun. Other answers with many fewer points (e.g. five points) suggested other less common conventions, such as imperative action-oriented verbs request something (e.g.
It would be worth looking at other data sources such as 1. the most popular open source projects and 2. the
boolean returning functions in popular software languages.
Questions, predicates, and imperatives could all be appropriate for boolean returning functions. A predicate states something about its subject. As such, a predicate is either true or false (metaphysics aside). A verb can represent a state of being (e.g. it is cold, he has a face); asking about that state will surface a yes/no answer. An imperative is a call to action (e.g. determine if it is cold). The action can investigate something's state of being, thus returning a
When there is more than one reasonable, logically sound option, it's often best to follow convention. For instance, there are sound arguments both for making clothing optional outdoors and for making clothing mandatory outdoors. When in a place that says clothing is mandatory, it is probably best to follow that convention. There are times both for civil disobedience and for self-expression through style, but software development usually is not the place for that.
Even when there is clearly a more logically sound option among many, it is often best to follow convention. For instance, Latin is a more logically sound language than English is. Also, Haskell is more logically sound language than Java is. Those who speak Latin and write Haskell will have more trouble finding people with whom to speak and to code, and there is no moral force compelling us to choose one over the other.
There are times, of course, when it is worth breaking with the status quo, and I can appreciate that this is somewhat of an art. For instance, if most people have short hair, self-expression warrants long hair. If most people are driving gas cars, concern for the environment warrants an electric car. If most people are driving cars on the right side of the road, follow convention.