1

Is there a name for this pattern where an API offers several variations of a method (differing in number of parameters) and internally forwards them with default parameters like this:

public void doSomething(Object one) {
    doSomething(one, null);
}

public void doSomething(Object one, Object two) {
    doSomething(one, two, null);
}

public void doSomething(Object one, Object two, Object three) {
        // real implementation here
}
  • 1
    It's called ugly Java boilerplate that imitate default argument values :) – Maciej Chałapuk Nov 2 '15 at 23:33
  • 2
    I assume these are not constructors, but if it was then the term might be "telescoping". – user156792 Nov 2 '15 at 23:38
  • A brief note on terminology: low level and highly language-specific techniques like this tend to be called "idioms" rather than "patterns". – Jules Nov 3 '15 at 2:36
4

If the delegation (and the default parameters filled in) are part of the function's contract, for example, if the DocString says

doSomething(one, two) is equivalent to doSomething(one, two, null).

then I refer to these as convenience overloads or convenience methods as they simply save the user some typing but don't do anything the user couldn't have done otherwise.

If the fact that the function simply delegates is not made public, then it is just an arbitrary implementation detail and probably does not deserve a special name.

I've picked this term up from the official Java documentation where it is used quite often.

A related idiom is to have a bunch of overloads for some public method doSomething that do nothing more than determining some parameters to then delegate to a private method doSomethingImpl. This is something different as the user is not aware of the existence of the doSomethingImpl method (and couldn't possibly call it). I have heard the doSomethingImpl be referred to as “worker method” or “workhorse” but those are probably very colloquial terms.

  • I've only heard "worker" refer to methods in separate threads that do some work for the main thread. – user156792 Nov 2 '15 at 23:37
  • @jdv “worker method” would probably be more precise. (updated) – 5gon12eder Nov 2 '15 at 23:41
  • These colloquialisms are as valid as anything else! I think my peers only ever used worker for threaded code. But your reference to "impl" methods also fits my experience, so there you go. – user156792 Nov 2 '15 at 23:45
4

As said the methods with different parameter lists is simple overloading.

The fact they call each other is simply a refactor to reduce duplication in the code. You could copy and paste the real implementation into each method body, but that would create problems when the common code changed (you would need to update the code in each method) so having the methods call each other is an obvious refactor.

In C# 4.0 and after you could use optional parameters to do the same

public void doSomething(Object one, Object two = null, Object three = null) 
{
        // real implementation here
}
  • Microsoft recommends in warning CA1026 not to use default parameters because they can break when unmanaged code calls managed code. Apparently, the default values aren't used in this case. – mgw854 Nov 3 '15 at 1:46
4

It's one use of overloading

In some programming languages, function overloading or method overloading is the ability to create multiple methods of the same name with different implementations. Calls to an overloaded function will run a specific implementation of that function appropriate to the context of the call, allowing one function call to perform different tasks depending on context.

If you are asking about this specific use case, then I don't know of a particular term, although it can be used as a substitute for optional parameters.

  • It seems to me the question is about the forward to the next-higher version, not the overloading itself (1 param => 2 param => 3 param). – Wilbert Nov 2 '15 at 17:33
  • @Wilbert Exactly. The definition of overloading does not require the forwarding, but simply two or methods of the same name with a different set of parameters. – asco Nov 2 '15 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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