How to name factory like methods?

Coding StyleNaming ConventionsMethodsFactory

Coding Style Problem Overview


I guess that most factory-like methods start with create. But why are they called "create"? Why not "make", "produce", "build", "generate" or something else? Is it only a matter of taste? A convention? Or is there a special meaning in "create"?

createURI(...) 
makeURI(...)
produceURI(...)
buildURI(...)
generateURI(...)

Which one would you choose in general and why?

Coding Style Solutions


Solution 1 - Coding Style

Some random thoughts:

  • 'Create' fits the feature better than most other words. The next best word I can think of off the top of my head is 'Construct'. In the past, 'Alloc' (allocate) might have been used in similar situations, reflecting the greater emphasis on blocks of data than objects in languages like C.

  • 'Create' is a short, simple word that has a clear intuitive meaning. In most cases people probably just pick it as the first, most obvious word that comes to mind when they wish to create something. It's a common naming convention, and "object creation" is a common way of describing the process of... creating objects.

  • 'Construct' is close, but it is usually used to describe a specific stage in the process of creating an object (allocate/new, construct, initialise...)

  • 'Build' and 'Make' are common terms for processes relating to compiling code, so have different connotations to programmers, implying a process that comprises many steps and possibly a lot of disk activity. However, the idea of a Factory "building" something is a sensible idea - especially in cases where a complex data-structure is built, or many separate pieces of information are combined in some way.

  • 'Generate' to me implies a calculation which is used to produce a value from an input, such as generating a hash code or a random number.

  • 'Produce', 'Generate', 'Construct' are longer to type/read than 'Create'. Historically programmers have favoured short names to reduce typing/reading.

Solution 2 - Coding Style

Joshua Bloch in "Effective Java" suggests the following naming conventions

> valueOf — Returns an instance that has, loosely speaking, the same value > as its parameters. Such static factories are effectively > type-conversion methods. > > of — A concise alternative to valueOf, popularized by EnumSet (Item 32). > > getInstance — Returns an instance that is described by the parameters > but cannot be said to have the same value. In the case of a singleton, > getInstance takes no parameters and returns the sole instance. > > newInstance — Like getInstance, except that newInstance guarantees that > each instance returned is distinct from all others. > > getType — Like getInstance, but used when the factory method is in a > different class. Type indicates the type of object returned by the > factory method. > > newType — Like newInstance, but used when the factory method is in a > different class. Type indicates the type of object returned by the > factory method.

Solution 3 - Coding Style

Wanted to add a couple of points I don't see in other answers.

  1. Although traditionally 'Factory' means 'creates objects', I like to think of it more broadly as 'returns me an object that behaves as I expect'. I shouldn't always have to know whether it's a brand new object, in fact I might not care. So in suitable cases you might avoid a 'Create...' name, even if that's how you're implementing it right now.

  2. Guava is a good repository of factory naming ideas. It is popularising a nice DSL style. examples:

     Lists.newArrayListWithCapacity(100);
     ImmutableList.of("Hello", "World");
    

Solution 4 - Coding Style

"Create" and "make" are short, reasonably evocative, and not tied to other patterns in naming that I can think of. I've also seen both quite frequently and suspect they may be "de facto standards". I'd choose one and use it consistently at least within a project. (Looking at my own current project, I seem to use "make". I hope I'm consistent...)

Avoid "build" because it fits better with the Builder pattern and avoid "produce" because it evokes Producer/Consumer.

To really continue the metaphor of the "Factory" name for the pattern, I'd be tempted by "manufacture", but that's too long a word.

Solution 5 - Coding Style

I think it stems from “to create an object”. However, in English, the word “create” is associated with the notion “to cause to come into being, as something unique that would not naturally evolve or that is not made by ordinary processes,” and “to evolve from one's own thought or imagination, as a work of art or an invention.” So it seems as “create” is not the proper word to use. “Make,” on the other hand, means “to bring into existence by shaping or changing material, combining parts, etc.” For example, you don’t create a dress, you make a dress (object). So, in my opinion, “make” by meaning “to produce; cause to exist or happen; bring about” is a far better word for factory methods.

Solution 6 - Coding Style

Partly convention, partly semantics.

Factory methods (signalled by the traditional create) should invoke appropriate constructors. If I saw buildURI, I would assume that it involved some computation, or assembly from parts (and I would not think there was a factory involved). The first thing that I thought when I saw generateURI is making something random, like a new personalized download link. They are not all the same, different words evoke different meanings; but most of them are not conventionalised.

Solution 7 - Coding Style

I like new. To me

var foo = newFoo();

reads better than

var foo = createFoo();

Translated to english we have foo is a new foo or foo is create foo. While I'm not a grammer expert I'm pretty sure the latter is grammatically incorrect.

Solution 8 - Coding Style

I'd call it UriFactory.Create()

Where,

UriFactory is the name of the class type which is provides method(s) that create Uri instances.

and Create() method is overloaded for as many as variations you have in your specs.

public static class UriFactory
{
    //Default Creator
    public static UriType Create() 
    {
    }

    //An overload for Create()
    public static UriType Create(someArgs) 
    {
    }
}

Solution 9 - Coding Style

I'd point out that I've seen all of the verbs but produce in use in some library or other, so I wouldn't call create being an universal convention.

Now, create does sound better to me, evokes the precise meaning of the action.

So yes, it is a matter of (literary) taste.

Solution 10 - Coding Style

Personally I like instantiate and instantiateWith, but that's just because of my Unity and Objective C experiences. Naming conventions inside the Unity engine seem to revolve around the word instantiate to create an instance via a factory method, and Objective C seems to like with to indicate what the parameter/s are. This only really works well if the method is in the class that is going to be instantiated though (and in languages that allow constructor overloading, this isn't so much of a 'thing').

Just plain old Objective C's initWith is also a good'un!

Solution 11 - Coding Style

Factory method doesn't dictate on method name. You can have as many methods you want in your factory, provided all of them return the object from same family.

For more details, visit the url http://xeon2k.wordpress.com

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondeamonView Question on Stackoverflow
Solution 1 - Coding StyleJason WilliamsView Answer on Stackoverflow
Solution 2 - Coding StyleNelson TatiusView Answer on Stackoverflow
Solution 3 - Coding StyleIainView Answer on Stackoverflow
Solution 4 - Coding StyleDon RobyView Answer on Stackoverflow
Solution 5 - Coding StyleFrederik KrautwaldView Answer on Stackoverflow
Solution 6 - Coding StyleAmadanView Answer on Stackoverflow
Solution 7 - Coding StylerjaxView Answer on Stackoverflow
Solution 8 - Coding Stylethis. __curious_geekView Answer on Stackoverflow
Solution 9 - Coding StyleVinko VrsalovicView Answer on Stackoverflow
Solution 10 - Coding StyleWill SquireView Answer on Stackoverflow
Solution 11 - Coding StyleNitinView Answer on Stackoverflow