views:

680

answers:

8

I have seen this often in code, but when I speak of it I don't know the name for such 'pattern'

I have a method with 2 arguments that calls an overloaded method that has 3 arguments and intentionally sets the 3rd one to empty string.

public void DoWork(string name, string phoneNumber)
{
    DoWork(name, phoneNumber, string.Empty)
}

private void DoWork(string name, string phoneNumber, string emailAddress)
{
    //do the work
}

The reason I'm doing this is to not duplicate code, and to allow existing callers to still call the method that has only 2 parameters.

Is this a pattern, and does it have a name?

+19  A: 

The name of that is overloading and it's not a design pattern but a OOP feature

http://en.wikipedia.org/wiki/Method_overloading

Claudio Redi
Ok, I should have put the second method private, this is the case where the overloaded method is hidden to the caller, and the overloaded methods are called by the parent. I will adjust my code, thanks, is there a name for this feature or use of overloaded method?
GenEric35
IMO, overloading could be considered a design pattern supported by OOP and other languages.
kenny
Saying that this is "just method overloading" is like saying e.g. the visitor pattern is "just dynamic dispatch". It describes the OOP mechanics perfectly, but it misses to address the whole notion of what a design pattern is.
polygenelubricants
@polygenelubricants: thanks for your feedback. The only reference to the "telescopic pattern" is the link you provided. It's just a name that one person decided to give to a specific situation that no one else in the world seems to use. The user asked if there is well known pattern for that and the WELL KNOWN name for that is overloading.
Claudio Redi
The well known name for frozen flakes of water coming down from the sky is snow. The name for the pattern of that happening 4 months out of the year is winter.
polygenelubricants
The OP already says that the method is overloaded. He's interested in the inter-method dispatch, not the overloading.
Steven Mackenzie
@Claudio: In response to your skepticism, I've added more quotes to my answer. I'll welcome further feedback.
polygenelubricants
+2  A: 

I'm guessing either your DoWork methods should be called CreateContact or your call to CreateContact should be DoWork...

But this isn't really a pattern. It's just a common use of method overloading.

Justin Niessner
Yes I could have called it Create contact but didn't want to involve constructors since in my real example those are static methods, so I just called it DoWork.I understand it is called method overloading, maybe I should have put this in the question to make it clear i know those are overloaded versions of the same method.It is the case where the caller can't call the overloaded method directly, I should have made the second one private, which i will edit now.Is there a name for this use of overloaded method where they call each other and only one of them is public?
GenEric35
As far as I'm aware, that's still just a use of overloading rather than one of the accepted patterns.
Justin Niessner
+6  A: 

No, this is not a design pattern in the gang of four sense, but it is common in many languages that do not allow default parameters.

In a language like ruby you could do something similar as follows

  def dowork(name, phoneNumber, emailAddress = '')
    # code here
  end
Daniel
+4  A: 

I'd say this is pretty much a c# < 4 work around for lack of default arguments. if you prescribe to the 'patterns are missing language features' school of thought i guess you could say its a pattern, though not one that commonly gets named.

edit: ok your update has really thrown me, i don't see what your are trying to do with a public method calling a private method. as far as the public api is concerned you could just move all the private methods code into the public method and have a local variable for the 'defaulted' value. or are both methods also called from other places in the class?

jk
+2  A: 

It is called as Function Overloading.In Function overloading name of a functions are same but they are differ in either type of parameter or number of parameter. It is also called as false polymorphism. It is not a pattern, it is a basic OOP concept.

MAS1
+21  A: 

It's actually more than just method overloading (where usually the same method name has different argument types), this specific pattern -- where overloads are basically the same method, and the shorter one invokes the longer one with default value to emulate optional parameters -- is called the telescopic/telescoping pattern, usually seen on constructors, but of course generalizable to any method.


For a more authoritative quote, here's an excerpt from Effective Java 2nd Edition, Item 2: Consider a builder pattern when faced with many constructor parameters (excerpt online)

Traditionally, programmers have used the telescoping constructor pattern, in which you provide a constructor with only the required parameters, another with a single optional parameters, a third with two optional parameters, and so on...

Again, usually the telescopic pattern is discussed within the context of constructors (where e.g. a 2-arg constructor would have one line this(arg1, arg2, ARG3_DEFAULT); to invoke the 3-arg constructor, etc), but I don't see why it can't be generalized to other methods as well.


Another authoritative quote, unfortunately with no definition of the pattern: Sun Developer Network: How to Write Doc Comments for the Javadoc Tool:

Notice the methods and constructors are in "telescoping" order, which means the "no arg" form first, then the "1 arg" form, then the "2 arg" form, and so forth.


And another random quote, with a more explicit definition of the pattern: I Am Hate Method Overloading (And So Can You!):

Telescoping Methods

You may have a function that takes some number of arguments. The last few arguments may not be all that important, and most users would be annoyed in having to figure out what to pass into them. So you create a few more methods with the same name and fewer arguments, which call through to the “master” method.

This last quote directly proposes that language support for default arguments is a much better alternative.

polygenelubricants
great answer, thanks
GenEric35
@polygenelubricants:I really appreciate your feedback, thanks! My point is, while I agree 100% with you in the general idea I’m not very convinced for this specific case. The fact that we had OOP for 40 years and design patterns for 30 years and for such a common situation you hardly found 3 references in the world calling that a “design pattern” (or at least generalizing the idea), don’t make you think that there is no accepted design pattern and probably because it’s not necessary? Do you think that it’s enough with provided data that to say: This IS the “Telescopic Pattern”?
Claudio Redi
@Claudio: _maybe_ the name hasn't spread widely yet, but it's used in _Effective Java_, which I believe is a widely read book. In any case, let's just start using the name rather than not naming it at all. We're using the pattern (good or bad), so rather than leaving it nameless, we might as well call it something. And what do you know, some people already did.
polygenelubricants
+3  A: 

It's an example of a helper method. Sheesh people, not being in the Gang of Four book doesn't stop it being a pattern. In the specific case where there helper is public and the helped method is private it's an example of encapsulation. Possibly a little too much encapsulation in this case.

Steve
that helped as well, thanks :D
GenEric35
+2  A: 

I agree with @polygenelubricants, but would like to point out that the telescopic pattern can be used apart from overloading. An example is in Objective-C, where method selectors (signatures) must be unique and can't be overloaded.

- (id)init {
    return [self initWithParam:0];
}

- (id)initWithParam:(int)param {
    // Do real initialization here!
    return self;
}
mbmcavoy