Welcome to U E T - C S . org Sign in | Join | Help

Osama Abbas

the Answers to Life, the Universe, and Everything
VB.net Optional Parameters

When I was young (about a year or so ago ... ) I used to look at Optional parameters and say : wow .. this is so cool ... instead of writing 4 overloads for this function, I can simply write one master function with optional parameters. Convenient !

Later on in life, while going through a mid-life crisis, fixing VB.net code written by another programmer years ago, I came across a function that not only used optional parameters, but was also overloaded, plus had multiple versions with slightly different names. Refactoring this function made me go insane as the number of permutations was simply overwhelming ! That was the point where all love for O.Ps was gone.

During my relatively wiser years, I wondered, how does this feature work exactly ?
The first thing that came to mind was that the compiler automatically generates overloaded functions in IL at compile time.

I WAS WRONG

Passing the assembly through ILDASM revealed that [opt] is a supported feature of the runtime and C# does not support it (this is probably more of a design decision than a technical limitation).

OK. So optional parameters are an A class feature and not an LSD laced candy VB.net hack* .... I still think they're bad. There are a lot of reasons behind this but basically I feel it breaks the element of good design in your code, creates ambiguities and makes code harder to refactor or convert into other languages.

In my senile years, I'll probably shake my stick at all these new fangled language features and curse at them but for now ... lets all please just avoid them.

* is LSD addictive ? 

Posted: Wednesday, February 28, 2007 10:15 PM by osama.abbas
Filed under:

Comments

fahad said:

Yup, LSD is addictive :P

# March 2, 2007 3:11 AM
Anonymous comments are disabled