views:

253

answers:

11

From time to time I write a function that just creates something if it's not there yet and otherwise does nothing.
Names like CreateFooIfNecessary() or EnsureThereIsAFoo() do work but they feel a bit clumsy.

One could also say GetFoo() but that name doesn't really imply that foo may be created first and it only works if the function returns a handle/pointer/reference to foo.

Can those of you more familiar with the English language come up with a better way to name these functions?

+2  A: 

This is generally known as lazy creation. How about LazyGetFoo()?

Avi
+8  A: 

Often I use this standard:

GetFoo(); // Always returns a Foo, so creates if there isn't one already.
FindFoo(); // Returns null if the Foo cannot be found.

Especially useful in data access, when you're loading an employee or something based on a key, and it may or may not exist.

To be specific - this is a documented coding standard. Everyone working on the codebase knows this standard and implements it in the same way. That's how we can get away with these nice short names that are fairly generic.

Neil Barnwell
Although your answer has the highest rating, I go for GetOrCreate. As you said, this has to be a coding standard; otherwise it might not be obvious.
mxp
The point of a coding standard is that it doesn't *have* to be obvious. Prefixing a variable with underscores isn't obvious that it's a private member variable **unless you already knew that was a standard**. The idea here is you get the best of both worlds - nice short names, but a documented standard that tells people what they mean.
Neil Barnwell
+1  A: 

I name these RequireFoo().

chaos
I'd use this if the method *doesn't* return the value.
Joachim Sauer
+4  A: 

I would go with EnsureFoo().

RichieHindle
A: 

Seriously:

issetFoo();

Taking the Piss:

AreWeThereYetFoo(); helloFoo(); booFoo();
Shahmir Javaid
+10  A: 

How about GetOrCreate()

Sean
If one is picky, it says that something is *either* retrieved *or* created. On the other hand that would hardly make any sense and it is really short.
mxp
A: 

In my Foo Class I would have GetExistingOrNew(Guid id) This would return an existing Foo if its exists or return a New Foo.

Mark Redman
+2  A: 

so you want to be a creator, right? then ...

LetThereBeFoo();

hehehe

to check if it exists

IsThereFoo();
pageman
+2  A: 

I think just having CreateFoo() is fine. The documentation should indicate what the function does in different scenarios. Look at some of the File I/O API's out there, for instance the CreateFile(...) from the Win32 API. Based on a parameter it will create a new file or open existing one if it exists. Yours doesn't have to be parameter based, but it's behavior can be different based on the state of the program. So fundamentally I think yours could be along the same lines.

ewrankin
+3  A: 

Why not:

private FooType m_foo;

public FooType Foo
{
   get
   {
      if (this.m_foo == null)
      {
         this.m_foo = new Foo();
      }
      return this.m_foo;
   }
   set
   {
      this.m_foo = value;
   }
}
Kirk Broadhurst
+1  A: 

If it's clear that only one Foo object will exist, I'd just call it CreateFoo(), because you would expect multiple calls to only create the object once.

If you are returning the value, I'd just call it GetFoo(), because lazy loading is (probably) an implementation detail, and as such, shouldn't be evident in the interface.

If multiple Foo objects can be created, and they aren't being returned, then I'm not sure. Sounds like a pretty strange situation to begin with.

Tom Dalling