문제

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)

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 softwareengineering.stackexchange
scroll top