Antti said:
Now that I read it more carefully I must mention one thing. In
number 14 you recommend using "is" and "has" prefix for boolean
type accessors. The example "hasLicense" points an obvious
problem: what is the variable you are accessing?
If the variable is "license" then it is obviously badly named
as a variable with that name should refer to the actual license.
If the variable is "hasLicense" then you're breaking your own
naming convention by not having a prefix.
First, the general rule is to use "is" prefix for boolean queryies.
This fits 99.9% of the time. The counter example may be badly choosen,
but still points to the fact that using "is" sometimes makes the term
artifitial.
A licensed software module will (depending on impelementation) start and
then request a license. If it doesn't get one it may continue to run but
with limited functionality. It may be appropriate for a sub component to
ask if it "has license" in order to complete its tasks. getLicense() is
a different query, and same with isLicensed(). isLicenseAvailable() can
be used but then I'd prefer hasLicense(). Opinions are welcomed!
Note the Java API is very inconsistent on this point:
boolean Calendar.before()
boolean Calendar.after()
boolean Component.hasFocus()
boolean Component.requestFocusInWindow()
boolean Vector.contains()
boolean Collection.add()
boolean String.matches()
boolean String.startsWith()
boolean Comparable.equals()
to give a few examples.
You also try to reason the rule with a faulty example. A method
named "getStatus" which returns a boolean value fits just as
well as "isStatus" does.
When naming boolean variables, find a term that fits with "is"-prefix,
like "visible" or "open" etc. "status" fails this test and is thus a
bad name for a boolean setting.
I like to use the "no exceptions" naming rule with boolean
variables too: all accessors are prefixed with "get" and "set".
There are no exceptions.
"getHasLicense" is not that much longer to write and you can
still see from the method name that you are accessing a boolean
variable. Same goes with "getCanEvaluate" and "getShouldAbort".
I don't find these terms as readable as the originals. Also, what you actually
access in the back-end is irrelevant. This is an API, and the query is boolean.
You also weaken your own rule by failing to mention the "is"
and "has" (and "can" and "should") prefixes in number 23.
The "is" prefix has to do with boolean names. Rule #23 has to do with naming
and term usage in general. There is no connection between the two.
And exceptions are part of the main design of a program (but that
is out of scope for this discussion).
Depends on the definition of "exceptional behaviour". I probably agree with
you and should tune the rationale for Rule #27. It doesn't change the
naming convention though.