问题
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?
回答1:
How about GetOrCreate()
回答2:
UPDATE
I know better these days, and so would apply Command Query Separation (CQS). CQS says that a method should either mutate state, or return a result, never both.
As a result, my current standard is:
Foo GetFoo(id); // Throws if Foo cannot be found for id
Foo FindFoo(id); // Returns null if the Foo cannot be found.
void CreateFoo(id[, args]); // Creates a Foo with supplied args.
OLD POST - IGNORE
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.
回答3:
I would go with EnsureFoo()
.
回答4:
This is generally known as lazy creation. How about LazyGetFoo()?
回答5:
so you want to be a creator, right? then ...
LetThereBeFoo();
hehehe
to check if it exists
IsThereFoo();
回答6:
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;
}
}
回答7:
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.
回答8:
I name these RequireFoo()
.
回答9:
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.
回答10:
In my Foo Class I would have GetExistingOrNew(Guid id) This would return an existing Foo if its exists or return a New Foo.
回答11:
I like ForceFoo
. In your situation it would be EnforceExistanceOfFoo
or ForceGetFoo
.
回答12:
I recently started to use defineFoo()
as I got sick of getOrCreateFoo()
回答13:
Seriously:
issetFoo();
Taking the Piss:
AreWeThereYetFoo(); helloFoo(); booFoo();
来源:https://stackoverflow.com/questions/1238386/function-name-for-creating-something-if-its-not-there-yet