Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
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
If this is a pattern then you could as well call "i++" the "index incrementation pattern". – Kim Feb 10 '11 at 21:21
@Kim touche.... – Steve Feb 10 '11 at 21:42
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
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
@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

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

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

share|improve this answer
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… – P.Brian.Mackey Feb 12 '11 at 0:29
@P.Brian.Mackey: Even in .NET 4, that style of overloading is still necessary in cases where the default value for a parameter cannot be expressed as a compile-time constant. – supercat May 8 '14 at 17:12

"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

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

Your Answer


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.