0

I have a method that checks a data structure for the presence of a thing that might be missing but is required. If it is missing, it adds it.

Essentially it is this (although the method is much, much more complicated due to the complexity involved in adding the thing, and the thing comes from a thid party service):

void MethodThatNeedsAName()
{
    if (!ThingExists) AddThing(); 
}

What would you name such a method?

CheckForAndAddThing()

EnsureThingIsThere()

AssertThingExists()

ValidateThingExists()

FixThing()

P.S. I understand the SOP concern here, please note this is just an example.

closed as primarily opinion-based by gnat, Robbie Dee, Doc Brown, Jörg W Mittag, Bart van Ingen Schenau Mar 3 '17 at 7:55

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Have you considered splitting out these into two different functions/methods? On one hand you have an operation which should probably be a "pure" function (checking that 'thing' exists); on the other hand you have a modifying operation to insert into your data structure. The fact that these are complex/nontrivial operations really suggests to me that they should be separate. – Ben Cottrell Mar 2 '17 at 20:12
  • @BenCottrell But what do you call THE function calling the other two then, Ben? – Andy Mar 2 '17 at 20:26
  • ImportThing() – radarbob Mar 2 '17 at 20:28
  • @DavidPacker That's a fair point. A name I've seen used in the .NET framework is GetOrAdd - for example, from the MSDN docs for ConcurrentDictionary - msdn.microsoft.com/en-us/library/ee378677(v=vs.110).aspx – Ben Cottrell Mar 2 '17 at 20:29
  • 2
    Before you create functions that initialize missing things, look in your particular language for language features that do this automatically. Once those are identified, what prevents you from simply saying new Thing() for those variables that are still null? – Robert Harvey Mar 2 '17 at 20:35
4
SetDefault()

In the case of a language like C#, you could actually use Reflection to find those fields that are null and instantiate them, making this a generic SetDefaults<T>() method.

Example, for an array:

public static T[] Populate<T>(this T[] array, Func<T> provider)
{
    for (int i = 0; i < array.Length; i++)
    {
        array[i] = provider();
    }
    return array;
}
1

I would name it

initThing() or initializeThing()

With this name, you know that your method returns a object filled with all data.

0

I'd probably separate it into a different file, call it a Filler and call Filler.fillThing(). Cause that's what it does, it fills.

It's also better designed if you need to feel otherThing as you'd only need to add a new method Filler.fillOtherThing().

SPR might help you here.

  • 2
    Not sure what SPR [sic] has to do with this. – Robert Harvey Mar 2 '17 at 20:36
  • @RobertHarvey I just realized his example code might be the implementation of the method he's describing, instead of the method itself, I'll clarify and if it's like that I'll delete this answer – Christopher Francisco Mar 2 '17 at 20:37
0

That would be "Thingify()".

Your design feels horrible though. Before getting into that you should at least consider making it two methods: HasThing() and AddThing(). A method that does more than one thing is going to be a pain in the ass at some point.

Now, why do I feel you have more work to do? You apparently have a state that is uncertain, possibly not right and you have code to "fix" that. It is probably because of the same mistake that led to the method that did more than one thing. You have multiple steps and/or logical paths and you wrapped them up in one handy-dandy good-for-all procedure. Then you have no idea what that procedure exactly yields and you try to find out what happened afterwards.

Start splitting up and isolating basic steps and have an agent call the steps and evaluate the unambiguous results before deciding what to do next. That will be more steps in a more controlled manner, giving you more flexible code that will be easier to maintain and adapt.

0

Ideally a combination of two of the answers I've seen: Call it "InitializeDefaultValues()" (instead of setDefault, or Initialize, although both are fine).

HOWEVER, if this is a class, and these members need to be set, this really should be done on construction, and then there is no worry about checking that they exist and then setting them to some default value.

0

Your question pretends there is a "general best way" to name such a method. IMHO that is a wrong assumption, this is case dependend. I recommend to look at the real case, look what the data structure actually represents and how it is really named, and then decide by-case about the naming of the method.

Some factors which should influence the naming: how other methods of the classes/objects of the related context are named, if you need such a method just once or for lots of similar data structures, if your data structure has just one of such methods or many in different flavors, if "filling the blanks" means to providing meaningfull defaults for 70% of the structures fields, or if it means to "repair" the data structure for the exceptional case it is inconsistent, and so on. And to some degree, this is a matter of taste and personal style - english language allows you often to express the same matter using different words or idioms, and the same is true for the naming of methods.

  • Yep of course I am always looking at the code and stuff. My question is looking for a convention that is commonly understood. – John Wu Mar 2 '17 at 22:13
  • @JohnWu: sure you are looking for that - and I am telling you there is none. – Doc Brown Mar 2 '17 at 22:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.