80

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?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
ashtee
  • 993
  • 1
  • 7
  • 10

12 Answers12

102
IsRetrievable()

I think that a method that returns a boolean value should be named as a yes-no question.

participant
  • 2,923
  • 2
  • 23
  • 40
Aziz
  • 20,065
  • 8
  • 63
  • 69
  • 11
    I like the `is` part, but just because it is retrievable doesn't mean it's OK to retrieve it. I would call it `isOKToRetrieve` – Thomas Owens Sep 03 '09 at 00:26
  • 1
    Absolutely. The "Is" prefix is a coding standard on our team. – Rap Sep 03 '09 at 00:28
  • 3
    ALl I would add TO Ariz' answer, (And this is a nitnoi) is to make a distinction as t oexactly what the function is checking... Is it whether the data IS retrievable, or whether the current code CAN retrieve it, or whether it SHOULD rectrieve it, etc. etc. e.g., if the function is examining the validity of the data and 'authorizing the retrieval when the data passes some set of conditions, then IsComplete() or IsValid, or IsTransactionallyConsistent, or something that describes the nature of the check that is being done to 'pass' might be somewhat more informative. – Charles Bretana Sep 03 '09 at 00:31
  • @bpayne Uhhh how would you use "Is" for something like shelf.HasProducts() or body.ContainsKnife()? – mxmissile Sep 03 '09 at 00:33
  • 3
    mxmissile: I would call those methods `isEmpty()` (and, if the shelf has a limit, `isFull()`) and I would NEVER make a `containsKnife()` method, but rather a `contains()` method that accepts a parameter. And yes, `contains()` is an exception to the is rule. – Thomas Owens Sep 03 '09 at 00:37
  • I think what Aziz has suggested about having it as a Yes No question is right. So, as Thomas Owens has commented above I am going with IsOkToRetrieve(). – ashtee Sep 03 '09 at 01:55
  • @bpayne fair enough, i'm assuming isEmpty() and isFull() would contain parameters if shelf could contain more than "products"? – mxmissile Sep 03 '09 at 23:36
  • @Aziz This seems to be the popular convention on SO but for some reason, I am not sure why, there is something I don't like about it. – theblang Jan 30 '13 at 15:58
  • Name the function as a statement, not a yes/no-question. `IsRetrievable` might be interpreted as an abbreviated "is it retrievable?" which is an actual yes/no question, or "it is retrievable", which is a statement. I would use the latter, since then if statements would read better. Another example, which makes it more obvious: `if AreFiltersTheSame(x,y)` uses a yes/no question, and `if FiltersAreTheSame(x,y)` uses a statement. I prefer the latter, since it reads better (especially with long if statements with many conditionals). In your case `IsRetrievable` works fine as a statement though. – Magne Jun 04 '14 at 13:17
  • @Magne Long if statements with many conditionals should be refactored because it is unclear what the code does. What about when you have ternaries `myVar = FiltersAreTheSame(x,y) ? 1 : 0`, how does that read for you? – Frederik Krautwald Apr 25 '15 at 09:41
  • @FrederikKrautwald Yes, it probably should be refactored, but often isn't, so one probably has to deal with it whether one likes it or not (due to other developers not refactoring, or you not taking the time to refactor their code). Ternaries do not read as well. Good point. I see regular if statements used more often though, so I would lean towards optimising for that. – Magne Apr 25 '15 at 14:45
25

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).

eKek0
  • 23,005
  • 25
  • 91
  • 119
23

How about using the prefix 'should'?

ShouldRetrieve(SomeData data);
dannie.f
  • 2,395
  • 2
  • 22
  • 20
15

Methods mean action. Therefore, I prefer method names to start with a verb. How about?

CheckIsRetrievable(SomeData data)
Mehmet Aras
  • 5,284
  • 1
  • 25
  • 32
15

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.

Cary Meskell
  • 312
  • 2
  • 10
  • I like this one as well. Most answers suggest just the is, but to me just prefixing with is would indicate a boolean var, not a function that returns a boolean. – Thiago Pereira Maia Nov 01 '21 at 15:12
10

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

arviman
  • 5,087
  • 41
  • 48
8

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.

Noon Silk
  • 54,084
  • 6
  • 88
  • 105
3

In Naming conventions, it is written that method name should be verb

user2679146
  • 81
  • 1
  • 1
  • 3
2

if you are doing more checks and isRetrievable() isn't appropriate you could use:

IsValid()
davidsleeps
  • 9,393
  • 11
  • 59
  • 73
1

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!

spender
  • 117,338
  • 33
  • 229
  • 351
1

I would prefer isOKToRetrieve or isRetrieveOK over variants without "is" under the convention that functions and methods should be verbs.

Tyler McHenry
  • 74,820
  • 18
  • 121
  • 166
0

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.

Billy
  • 89
  • 1
  • 4