62

I always have trouble figuring out if I should name a certain method starting with getSomething versus findSomething.

The problem resides in creating helpers for poorly designed APIs. This usually occurs when getting data from an object, which requires the object as a parameter. Here is a simple example:

public String getRevision(Item item) {
    service.load(item, "revision");
    // there is usually more work to do before getting the data..
    try {
        return item.get_revision();
    }
    catch(NotLoadedException exception) {
        log.error("Property named 'property_name' was not loaded", exception);
    }
    return null;
}

How and why to decide between naming this method as getRevision() or findRevision()?

  • 3
    the best helper for a poorly designed API is not to mess with tricky naming but establish an Anticorruption Layer: "If your application needs to deal with a database or another application whose model is undesirable or inapplicable to the model you want within your own application, use an AnticorruptionLayer to translate to/from that model and yours." – gnat Jan 9 '13 at 22:00
  • 1
    I have never heard of this concept before. Do you have any better links with examples? – knownasilya Jan 10 '13 at 0:11
  • 1
    Search the web, there's quite a lot of info on it. Eg, Anatomy of an Anti-Corruption Layer, Part 1 "it is likely that... you are inevitably faced with the task of interacting with the spaghetti that is already there. Enter the Anti-Corruption Layer..." – gnat Jan 10 '13 at 5:02

10 Answers 10

96

I use Get when I know the retrieval time will be very short (as in a lookup from a hash table or btree).

Find implies a search process or computational algorithm that requires a "longer" period of time to execute (for some arbitrary value of longer).

| improve this answer | |
  • 3
    +1 I use get when retrieving and find when work has to be done to do a get. – Jim Jan 10 '13 at 3:51
  • 6
    Taking in account that code changes (some parts become optimized and algorithms changes) and changing API is often impossible it doesn't look like a right criteria. What would you do if you replaced find with a hash-table algorithm later? – meze May 17 '13 at 13:34
  • 4
    I would also assume, when reading a call, that "find" may be called when the find doesn't succeed because the search criterion doesn't succeed, while "get" is expected to succeed unless there is some unusual problem. – gnasher729 Jul 30 '17 at 17:41
  • What if the function accepts an optional parameter to filter the results based off some condition? Both get and find would apply depending on how it's used. – ESR Aug 28 '18 at 10:54
74

I would say that find may fail but get shouldn't.

| improve this answer | |
  • 31
    If you mean that find can return NULL while get will never return NULL but might throw (or assert), I agree. – Sjoerd Jan 10 '13 at 0:33
  • 2
    I totally agree with @Sjoerd on this. – mhr Jan 22 '13 at 15:07
  • And what if the find() returns Optional<>? In that case find is also null safe. – TheCoder Sep 8 '19 at 9:22
  • 2
    @TheCoder I think it's not about null safety. It's about your intention: When you get you are sure what you get exists and if it doesn't it's an exceptional case. Whereas when you find something, you're not sure if it exists or not so not finding it is totally expected. – disklosr Mar 12 at 9:19
53

To quote a conversation I often have with my kids:

me: Hey kid! Go find me some batteries

kid: But where are they?

me: That's why I told you to go find them. If I knew where they were, I would have told you to go get them. Or you could ask your mother.

The same idea holds:

  • use "get" for a method which returns a cheaply available piece of information (and can probably be inlined or otherwise optimized away), or for a piece of information uniquely owned by this object.

  • use "find" for a method which does work to get a piece of information, or uses other objects to find it.

| improve this answer | |
  • 21
    Only a programmer would have this conversation with their kids. "Do you want to take out the trash?" "No." "Will you take out the trash?" "Yes." – Robert Harvey May 19 '16 at 14:51
  • @RobertHarvey I think I'm having this problem with people. Whenever someone tries to explain something, or asks a question, I usually ask questions back and tell them to be explicit about it. Otherwise we usually end up with a XY problem. If I don't do that, I feel like a walking autocomplete feature. You don't know what's on your mind, you can't put it into words, you babble couple words and expect me to do all the "thinking" for you and help you with that? Nope, not happening :) – akinuri Nov 2 '19 at 10:56
7

I apply the following pattern:

  • Foo GetFoo() cannot return null and its complexity is at most O(log(n))
  • bool TryGetFoo(out Foo) can return null and its complexity is at most O(log(n))
  • Foo FindFoo() cannot return null and its complexity is at least O(log(n))
  • bool TryFindFoo(out Foo) can return null and its complexity is at least O(log(n))

That way the code is pretty clear on the intent and on the complexity you can expect.

Typically, the Getters are for direct list or dictionary/set access.
The Finders are deep search, full scan of list, etc...

In your case:

public bool TryGetRevision( Item item, out String revision ) 
{
    service.load( item, "revision" );
    // there is usually more work to do before getting the data..
    try 
    {
        revision = item.get_revision();
        return true;
    }
    catch( NotLoadedException exception )
    {
        log.error( "Property named 'property_name' was not loaded", exception );
        revision = "";
        return false;
    }
}
| improve this answer | |
  • 2
    +1 for the try, short and precise – SpaceTrucker Jan 29 '13 at 19:10
4

Do not use find or get prefixes. This is a violation of UniformAccessPrinciple coined by bertrand meyer. Why not create a method like the following:

public String revision(Item item)
| improve this answer | |
  • i completely agree with u, great!!!! – Irakli Gabisonia Apr 24 '18 at 11:59
  • 2
    methods do something. The method name should be a verb. "revision" is a horrible method name in this context. – Blechdose May 12 at 17:04
  • I have lately seen the recommendation to name pure methods with nouns and impure methods with verbs. See the book Elements of Clojure with its nice chapter on naming and also this other blog on naming functions – beluchin Oct 4 at 13:07
3

Find implies not having the result, like when executing a database query with some parameters that may change between calls. Get, on the other hand, implies the results are known to the method beforehand or won't change once known, that there are no parameters to the call.
So, I'd use for example Customer findCustomerById(long customerId) and Customer getCustomer()

| improve this answer | |
2

get is appropriate in any case _ in fact it is often assumed that in order to get something you need to find that first. So if you're not sure, use get.

I would use find for methods like findMinimum() or findOptimal(), i.e. where there is some special algorithm which calculates the return value, and doesn't simply make a request to the DB, file system, remote server, etc. to receive some data.

| improve this answer | |
  • 1
    Good points. Personally I'd probably not use find as a prefix in the examples you provided. For computational tasks, like the ones in your example, I would use calculate or compute. – knownasilya Jan 9 '13 at 21:52
1

To me, find implies there can possibly be more than one result present. get implies only one.

| improve this answer | |
  • 8
    Seems like it has that feel, but I'm not sure I completely agree. Think of it this way: getCat vs findCat vs getCats vs findCats. The find.. still represents singular objects being returned. The plural should be added to the noun, in my opinion. – knownasilya Jan 9 '13 at 21:44
1

I will generally use Get to retrieve an object/value, and Find to retrieve its location (in an array, for example).

for ex:

object o = obj.GetItem( 'name');

integer i = somearray.Find( 'name');
| improve this answer | |
1

I have mostly seen (and like to use) this convention:

  • getFoo() - the caller expects that foo exists, and if not, it throws. Mostly, it has some kind of ID as param.

    • Example: getProductByCodeName("BTTF")
  • findFoos() - the caller expects that foo may not exist, in which case the methods returns null. Parameters are typically some "foreign keys" - ids to other objects, or enum values.

    • Example: findCarModelsMadeBy(CarMaker.FORD)
  • listFoos() - provides a list of foos, or empty list if none are present. Typically, gives items that are already prepared (eagerly loaded), and may return an Iterator, Stream, Sequence, etc.

    • Example: invoice.listItems()
  • searchFoos() or queryFoos() - does some kind of active search by given values. May also search by vague criteria like patterns, search queries etc.

    • Example: searchBooks(QueryBuilder.withTopicKeywords("japan middle age anjin"))
  • lookupFoo() - typically calls a remote service to get some information by an ID. Comes from the conventions of JavaEE.

    • Example: bankInfoService.lookupBankInfo("NL12")
  • provideFoos() - typical for some kind of repositories which do not change too often and is not supposed to be called too often, like, a provider of implementations of some interface, called during application boot.

    • Example: ProductConfigProvider.provideProductConfigs()
  • loadFoos() - inditaces that getting the operation may involve costly I/O (disk access, database roundtrip, web service call).

    • Example: s3archive.loadFilesFromDay(LocalDate.now())
  • try*Foos() - explicitly indicates that it will handle errors / exceptions, either by returning null, empty list, or a predefined wrapping exception. Often used as a util method to wrap a method unconveniently throwing if it does not find.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.