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()?

  • 2
    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
up vote 73 down vote accepted

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

  • 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
  • 4
    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
  • 2
    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 at 10:54

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

  • 24
    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
  • 1
    I totally agree with @Sjoerd on this. – mrtnrdl Jan 22 '13 at 15:07

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.

  • 12
    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

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

I apply the following pattern :

  • Foo GetFoo() can't return null and it's complexity is O(log(n)) or less
  • bool TryGetFoo(out Foo) can return null and it's complexity is O(log(n)) or less
  • Foo FindFoo() can't return null and it's complexity is more than O(log(n))
  • bool TryFindFoo(out Foo) can return null and it's complexity is more than 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;
    }
}
  • +1 for the try, short and precise – SpaceTrucker Jan 29 '13 at 19:10

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.

  • 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

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)

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');

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

  • 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

protected by gnat Oct 29 '17 at 14:02

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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