Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a good name for a method that gets or creates an object?

Say you have a cache, and a method that will do something like the following:

if (wanted Foo is not in cache)     cache.Add(new Foo()) Return Foo from cache 

What would you call that method? GetFoo(), GetOrCreateFoo() or something else (and better)? Or should this really be divided into two methods?

like image 557
Tor Livar Avatar asked Jul 06 '10 07:07

Tor Livar


People also ask

What makes a good function name?

The name should clearly, without ambiguity indicate what the function does. You don't have to jump around searching for the truth. Function names should apply the lower camel case form: addItem() , saveToStore() or getItemById() . Every function is an action, so the name should contain at least one verb.

Should method names be verbs?

Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters.


2 Answers

In most cases a simple GetFoo suffices as the caller doesn't need to know that you are creating and caching it. That's what encapsulation is all about.

However, in some circumstances, creating is an expensive operation, so it's useful to know that you may be creating something on demand and in some cases it will be slow. In this case, a different naming convention makes it clearer to the caller. GetOrCreate() or Get(Options.CreateIfMissing) is a good hint to the caller.

(The behaviour should of course be noted in the documentation, but it's good to use a method name that reminds people about side effects while they are reading the code, without them having to bring up and read the documentation for every method that is called)

The sort of case I find this happening in most often is (for example) when finding a tree node (e.g. in an XML document) you might have CreateNode (to create a node without adding it to the tree) and AddNode (to add an existing node to the tree). In this case, an "Add a node if it doesn't already exist" needs to have a different, descriptive name, so I will use something like EnsureNodeExists to differentiate it and make the purpose clear.

like image 109
Jason Williams Avatar answered Sep 19 '22 08:09

Jason Williams


I know I'm very late at the question, but can I propose GrabFoo() as a convention for get-or-create-if-missing to differentiate it from GetFoo()?

Looking at the synonyms of Get, I see several suitable verbs, some already commonly used as method verbs. For example, Fetch already connotes fetching something from an external system. (e.g. database or network)

Grab seems like rarely used, and might be carrying an (albeit weak) semantic of I want you to get-or-create it, no matter what.

like image 35
Ibrahim Arief Avatar answered Sep 20 '22 08:09

Ibrahim Arief