98

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

Deduplicator
  • 9,209
knownasilya
  • 3,284

10 Answers10

125

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

Robert Harvey
  • 200,592
100

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

coredump
  • 6,015
71

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.

jimwise
  • 7,668
14

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;
    }
}
Cyril Gandon
  • 1,346
12

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

  • getFoo(<id>) - the caller expects that foo exists. If not, getFoo(0). throws. Mostly, it has some kind of ID as param.

    • Example: getProductByCodeName("BTTF")
  • findFoos(<criteria>) - 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(<ids>) - 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(<criteria>) 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(<uri-like-name>) - typically calls a remote service to get some information by an ID. Comes from the conventions of JavaEE. The ID is usually in some known format, e.g. URI.

    • 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 loading 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.

  • deriveFoos() or computeFoos() - indicates that Foos are computed in a non-constant time. For instance, converted from a collection on-the-fly at the call-time.

  • buildFoo(...) - the method simply assembles Foo from the given building blocks. Especially useful for testing.

  • formatFoo(...) - whenever you create a String from the parameters.

  • translateFoo(Bar) - when there's a 1:1 relation between Foos and Bars, and this method finds the counterpart.

6

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

jwenting
  • 10,099
6

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

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

Karl Bielefeldt
  • 148,830
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.

superM
  • 7,373
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');
GrandmasterB
  • 39,412