views:

87

answers:

5

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?

+1  A: 

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.

Amadan
+4  A: 

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 langugaes 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 commmon 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.

  • 'Generate' to me implies a calculation, such as generating a hash code or a random number.

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

Jason Williams
A: 

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) 
    {
    }
}
this. __curious_geek
A: 

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.

Vinko Vrsalovic
+1  A: 

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

Don Roby