Discussion on:

14
Comments

Join the conversation!

Follow via:
RSS
Email Alert
0 Votes
+ -
Why do you need optional parameters when you can just overload methods? Pointless..

public void DoSomething(string Name) {
}

public void DoSomething(string Name, int Age) {
}

These two methods accomplish the same work, but with different signatures. There is NO NEED for optional parameters when you can overload methods.
When overloading you expect different implementations for different parameters. When using optional parameters you expect almost the same implementation...
0 Votes
+ -
More specifically:

public void DoSomething(string Name, int Age) {
}

public void DoSomething(string Name) {
int DefaultAge = ;

DoSomething(Name, DefaultAge) ;
}

Any additional code is minimized (realize, too, that you need something to declare optional parameters where they're supported).

To me, the explicit overloading is much easier to understand, too (but that's just personal preference). So why not give the maintenance programmers a break?
DON'T EVER ADD optional parameters. The problem with them is that they do not generally require any kind of rules on the parameters. I maintain millions of lines of code and believe me it's a nightmare to figure out which paramters are required and which are not when some fool 10 years ago wrote a sub with 7 optional parameters. They are often abused like this!

public sub DoSOmething(opt1, opt2, opt3, opt4, opt5, opt6, opt7)

Ok... so can I pass 1 and 5? 7 and 3? 2, 3, 4? NO! Guess what I have to go look at the stupid function and read through it for half an hour to figure it out. Optional parameters SUCK. Method overloading is the right answer.

public Sub DoSomething(1, 2)
public Sub DoSometing(1,2,3)
public Sub DoSomething(1,2,3,4,5,6,7)

Now you can see there are 3 valid ways to call this sub, NOT 7! (seven factorial) different ways.

I know this is an old thread but if anybody else finds this they need to think about this fact. It's a nightmare to maintain! There are probably times when the optional parameter approach makes sense, is elegant, and works. But like everything think about what you are doing. If you run into the perfect scenario then by all means do it. But don't create ambiguous code like the example. Even if you are using VB.NET or something else that will support this kind of mess, if the parameters DO have rules that govern which are required at what times and in what groups, method overloading is by far the more clear and more maintainable solution. And it's by far the most common scenario in our millions of lines of code. happy coding!
Hey man... programming can get complicated sometimes. Don't complain about that - it's your job to figure that stuff out. There's two sides to this - calling code can get really ugly without optional parameters - it is a valuable feature.

I don't find it particularly hard to maintain actually, and there's many cases where I would rather see optional parameters instead of 5040 overloads (7-factorial). Think about that possibility, where we really do need to have all seven parameters be truly optional. Would you rather maintain five thousand overloads?

That is the exact reason I'm here reading this article. Not because I want to abuse future programmers, but because I would like to create some clean, compact, readable code, but I need to handle a possible 480 million options for parameters that could be there or not.

That said - I will be solving this issue by passing an object, which is what I would usually do, but today I had some time to look into other options. There really aren't any in C# - but they are there in other languages, and they are good - not always, but usually.
0 Votes
+ -
Why?
TerryBailey 25th Sep 2002
Why are optional parameters needed? If you do as this article described, you are following poor OOP practice and introducing all sorts of vulnerability. Now you have to add all the error checking for data types etc..., not to mention adding overhead to the running app slowing it down because these choices are made at runtime.

If you use the overloading provided, the posiibility of runtime errors is greatly deminished, and since the choices are made by the compiler, the running app will be faster and les vulnerable.

So again I ask, Why try to force optional params?
If you haven't passed xml as a parameter, try it. You've got all the parameters you'll ever want, or not.
0 Votes
+ -
Opt on l Par me ers are a sl ppy and not v ry eff cient. You alw ys pay for sh rtc ts. The additional code for type checking, counting defaulting alone make overloads in C# worth the effort. VB is dead long live C#
0 Votes
+ -
You forgot about this one
Halotron Updated - 2nd Feb 2009
VB's usage of optional parameters goes beyond function overloading, because it allows you to completely skip a parameter.

Ex:

MyFunction(ByVal x As String, Optional ByVal y As String = "", Optional ByVal z As String)

Allows you to do this:

MyFunction("a", , "c")

(Note that the second parameter was not supplied, but the third one was.)

Creating overloaded functions in C# won't handle this.

And seriously, how is it that C# programmers are so excited about the ability to reduce code with lambda expressions, but come down on VB programmers about reducing code by creating optional parameters instead of explicitly creating overloaded functions??

Optional parameters aren't even a VB thing anyways, they're in C++!
And on top of that, the next version of C# is going to support optional parameters.

Anyways, you actually can put in optional parameters in C# functions if they are being called
by other languages (C++, VB) that support optional parameters.

Ex:
public MyFunction(string arg1, [Optional, DefaultParameterValue(0)] int arg2)
Make sensible use of nulls.
You could use nullable types as your parameters e.g.

public static void Test(int? ToReturn)
{
if(ToReturn.HasValue)
{
//do something with ToReturn
}
else
{
//process normally
}
}

Call Test like this:

int i = 0;
Test(i);

or

int? j = null;
Test(j);
Starting from Visual Studio 2010 - C# 4.0 will support optional parameters. Documented in the C# Programming Guide: Named and Optional Arguments: http://msdn.microsoft.com/en-us/library/dd264739(VS.100).aspx.
This MSDN page, "Simulating Default Parameters" example demonstrates the use of method overloading to simulate default parameters: http://msdn.microsoft.com/en-us/library/aa287762(VS.71).aspx
Keyboard Shortcuts:
Prev
Next
Toggle
Join the conversation
Formatting +
BB Codes - Note: HTML is not supported in forums
  • [b] Bold [/b]
  • [i] Italic [/i]
  • [u] Underline [/u]
  • [s] Strikethrough [/s]
  • [q] "Quote" [/q]
  • [ol][*] 1. Ordered List [/ol]
  • [ul][*] · Unordered List [/ul]
  • [pre] Preformat [/pre]
  • [quote] "Blockquote" [/quote]

Join the TechRepublic Community and join the conversation! Signing-up is free and quick, Do it now, we want to hear your opinion.