Which is the better name for a method that returns a boolean?
IsSupportContentType
or
CanSupportContentType
Which is the better name for a method that returns a boolean?
IsSupportContentType
or
CanSupportContentType
Is vs. Can
According to the Microsoft naming convention recommendations, both "Is" and "Can" are OK (and so is "Has") as a prefix for a Boolean.
In plain English, "Is" would be used to identify something about the type itself, not what it can do. For example, IsFixed, IsDerivedFrom, IsNullable can all be found in CLR types and methods. In all of these cases, "Is" is followed by an adjective.
Meanwhile, "can" more clearly indicates a capability, e.g. CanEdit, CanRead, CanSeek. In each of these cases, can is followed by a verb.
Since "Support" is a verb, I think in your case CanSupportContentType is better.
Shorter alternative
On the other hand, the conventions say the prefix is optional. What's more, it's kind of cheesy to include the argument type in the method name, since a developer can see the type of the argument in intellisense. So you could just name your method Supports and define it like this:
public bool Supports(System.Net.Mime.ContentType contentType)
...which is shorter and still clearly communicates the purpose. You'd call it like this:
ContentType contentType = new ContentType("text/plain");
var someClass = new MediatorsClass();
bool ok = someClass.Supports(contentType);
Or as a compromise maybe this is best:
public bool CanSupport(System.Net.Mime.ContentType contentType)
if ( someClass.Supports(contentType) )
– candied_orange
May 09 '17 at 23:28
std::vector::empty(). From its name only, does it empty the vector? Or does it return whether the vector is empty? Actually the latter, since the former task is done by std::vector::clear(). But you must in general read the docs to be sure. As an opposite example, Qt's QVector is easier to understand in this regard, since its method of checking for emptiness is QVector::isEmpty().
– Ruslan
May 10 '17 at 21:08
It's worth mentioning that the "should" prefix can also be used. According to Apple's guideline, not just "can" and "should", modal verbs in general can be used to name functions that return boolean. I can't see many use of "will" but "should" is nice for advice-inquiring hooks, as seen in reactjs:
shouldComponentUpdate: (newProps: any) => boolean
is... but use should... in some function argument names, places where the boolean indicates what the function is supposed to change things to. If a function can optionally close a document, calling the parameter controlling that isClosed would be accurate (it’s not closed yet) and so we would use shouldClose to indicate that this is what the function is supposed to do. (Arbitrary example; we wouldn’t likely have a function like this, particularly since closing a document should be weighty enough to have a dedicated call.)
– KRyan
May 10 '17 at 18:36
will... is reserved for asynchronous functions that return a promise; if the function described in my previous comment is synchronous, using will... would be inconsistent with our naming.
– KRyan
May 10 '17 at 18:38
RequiresSupervisorPermission = MovingBackward || MovingMoreThanOne. Sticking to only 'Is' as a prefix feels overly limiting, I'm interested in what others think
– flumperious
Jul 27 '22 at 13:28
For the people against the use of "should" prefix for boolean arguments, here is my fresh last one used for an optional argument name (but I need to agree that I can't even remember last time I've used this prefix):
MethodNameToUpdateDataList(..., bool shouldDeactivateMissingIds = false) {}
Neither "is", "can" nor "will" prefix seems ok there... What do you think?
That is why, the caller of this method has to decide (himself) or let default but, he can force to true in some cases.
The method itself doesn't know yet when to do it or not, but the method still handle both logics.
The caller is the one knowing when here.
– Islem Feb 09 '22 at 13:16deactivateMissingIds? I don't want to tell it that it should do this thing. Just do the thing if it's relevant!
– Vapid
Aug 26 '22 at 07:15
ContentTypeand some content types are support content types, of course - in which caseIsSupportContentTypewould return true if the receiver is a support content type. But supported is a different word entirely. – user253751 May 10 '17 at 01:11IsSupportedContentTypeto be grammatically correct. (unless "support content type" acts as a noun, which seems unlikely) – CodesInChaos May 10 '17 at 08:40supportsContentType? The following is entirely readable:if (abc.supportsContentType("text/html")). "can support" implies that there are further conditions to support the content-type. – Olivier Grégoire May 10 '17 at 09:12SupportsContentTypeis the best choice. It is simpler, shorter, and reads better in a wider variety of statements. In general, when it's a toss-up like this, I look at how I expect the function to be called, and pick the one that ends up being the most readable with the surrounding code. The client of the library will either have to look it up or use IntelliSense anyway, so you might as well shoot for readability of the resulting code for future maintainers. – Cody Gray - on strike May 10 '17 at 09:27isprefix is assumed for Java Beans, and expected by the introspectors. So in Java, you should usually useisas the prefix for "getters forbooleanvalues". – Marco13 May 10 '17 at 12:40?-- so,(defn support-content-type? ...)– Charles Duffy May 10 '17 at 13:40IsContentTypeSupported? – daiscog May 10 '17 at 14:37?, or if that's not available, a trailing-p. (pfor "predicate".) For benighted languages I use a prefixporq. – davidbak Nov 13 '21 at 00:14