可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have an interface in c# that helps retrieving of data from a custom archive on server. The interface looks like this:
public interface IRetrieveData
{
bool OkToRetrieve(SomeData data); // Method in question...
bool RetrieveToLocal(SomeData data);
}
This interface is implemented by the clients that retrieve the data to the local database. There are different flavors of clients that have access to each others data. So, when the processing component calls IRetrieveData.OkToRetrieve right before actual retrieve, the call goes to the client code where the decision is made on whether the data should be retrieved or not. At this point the client can return false and that piece of data is skipped or return true and the processing component calls RetrieveToLocal and send the data to the client which then processes it.
Where I am getting confused is whether to rename the method "OkToRetrieve" to just "Retrieve" or "CanRetrieve" or leave it as OkToRetrieve.
Does anyone have any suggestion?
回答1:
IsRetrievable()
I think that a method that returns a boolean value should be named as a yes-no question.
回答2:
Allways name boolean methods with names similar to questions that can be answered Yes or No.
In your case, CanRetrieve would be a good name (just to use your own suggestion).
回答3:
How about using the prefix 'should'?
ShouldRetrieve(SomeData data);
回答4:
Methods mean action. Therefore, I prefer method names to start with a verb. How about?
CheckIsRetrievable(SomeData data)
回答5:
In this specific case, I'd probably name it:
public bool IsReady (SomeData)
Because it more clearly demonstrates what will happen once this returns true
.
回答6:
Depends on your use case. I like to prefix them with words such as 'is', 'does' or 'Can':
IsSomePropertySoAndSo
, DoesNounSupportFeature
and as your example CanVerb
回答7:
CanRetrieve sounds fine to me. I've seen the Can stem used in Microsoft APIs. The only other real option IMO is IsRetrievable (from Aziz) which somehow seems too linguistically twisted!
回答8:
if you are doing more checks and isRetrievable() isn't appropriate you could use:
IsValid()
回答9:
I would prefer isOKToRetrieve or isRetrieveOK over variants without "is" under the convention that functions and methods should be verbs.
回答10:
MayRetrieve()
could be a better name if the result is determined by the user's permission/access.
IsRetrievable()
is more ambiguous, which may be more appropriate if there are other considerations in addition to, or other than, permission.
回答11:
In Naming conventions, it is written that method name should be verb
回答12:
Generally, methods/functions indicate actions so they should prefixed with verbs. e.g. check, get, make, etc.
The common naming convention for boolean variables is to prefix them with helping verbs e.g. is, does, will, can
I would think that the combination of the two conventions would lead to a pretty good, discerning pattern. So getIsRetreivable() or checkIsRetrievable() would look pretty good to me.