Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

share|improve this question
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
    
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
add comment

8 Answers 8

up vote 40 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).

share|improve this answer
    
+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
    
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
add comment

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

share|improve this answer
7  
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
    
I totally agree with @Sjoerd on this. –  mri Jan 22 '13 at 15:07
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
add comment

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;
    }
}
share|improve this answer
    
+1 for the try, short and precise –  SpaceTrucker Jan 29 '13 at 19:10
add comment

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.

share|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
add comment

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

share|improve this answer
6  
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
add comment

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');
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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