Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.
public int foo(int x)
{
    int defaultValue = 0;
    return foo(x, defaultValue);
}

public int foo(int x, int y)
{
    return x + y;
}
share|improve this question
2  
If this is a pattern then you could as well call "i++" the "index incrementation pattern". –  Kim Feb 10 '11 at 21:21
1  
@Kim touche.... –  Steve Feb 10 '11 at 21:42
add comment

5 Answers

up vote 8 down vote accepted

It's called emulating default argument through method overloading.

And where does it use polymorphism?

share|improve this answer
    
@delnan overloading = polymorphism –  Steve Feb 10 '11 at 20:52
    
@Steve: No. Unless you mean "overloading on this", which would be a strange definition, would only work if overloads weren't decided statically but dynamically and doesn't apply here anyway. –  delnan Feb 10 '11 at 20:59
1  
Specifically method overloading - having multiple methods with the same name but different signatures - is (sometimes) referred to as ad hoc polymorphism. –  Frank Shearar Feb 10 '11 at 21:00
3  
@Frank: Ah, yes. But that's not what polymorphism usually describes in OOP context. –  delnan Feb 10 '11 at 21:01
    
@Frank: "ad-hoc polymorphism" describes overriding, not overloading. "ad-hoc" is used to disambiguate between type polymorphism (what people call "generics" in OOP languages) and overriding, not between overriding and overloading. Static overloading just isn't polymorphism at all. –  munificent Feb 10 '11 at 23:50
show 2 more comments

Overloading is not polymorphism.

I'm not really sure of a pattern, except that it's a way to create overloaded constructors with default parameters.

share|improve this answer
    
I was under the assumption that static polymorphism is method overloading. Anyway we digress. –  Steve Feb 10 '11 at 21:11
add comment

In .NET 4 this type of coding is not necessary. Instead, use optional parameters with default values. More info.

share|improve this answer
3  
A bite of missing-the-question anyone? ;) –  delnan Feb 10 '11 at 21:32
    
I wouldn't call the code in the OP a design pattern (not to be confused with a design principle). Patterns are proven techniques that can be applied in a set of scenerios. The code is recognizeable as an obsolete technique for new development, if anything that makes it an anti-pattern. Thus, the answer. –  P.Brian.Mackey Feb 10 '11 at 22:17
    
It's hardly obsolete, and optional parameters are a tradeoff, not a clear win. But the word you're probably looking for is "idiom". –  Frank Shearar Feb 11 '11 at 8:49
    
@Frank shearar - point taken. Good info on this subject stackoverflow.com/questions/3316402/… –  P.Brian.Mackey Feb 12 '11 at 0:29
add comment

"DRY Overloading" is a better description; polymorphism refers to inheritance hierarchies. I don't think you could call it a design or architecture pattern, either, since details at this level are details of implementation, not design.

share|improve this answer
add comment

The code you show is an example of how to implement default parameter values using a language that does not permit default parameter values.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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