C# Optional Parameters?
-
I just read this[^] and thought "what on earth is going on here"? When I moved from VB.Net to C#.net, one of the big evangelistic arguments was around optional parameters as opposed to overloaded methods. I always liked optional parameters, but was prepared to give them up if the general feeling was that they where evil. Seems they aren't evil any more. I think Microsoft is just messing with my head and they will be removed in 5.0. I do like the named parameters though. I have been wanting those ever since I did some Office Automation stuff.
RCoate wrote:
I do like the named parameters though. I have been wanting those ever since I did some Office Automation stuff.
I've loved named parameters since I first did some Ada programming in the mid 1990s...thy'r the one thing that makes optional parameters non-evil, IMO...
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!
-
Named parameters have the same problem. How do you guarantee that you have passed in the fourth parameter everywhere you should have?
Electron Shepherd wrote:
How do you guarantee that you have passed in the fourth parameter everywhere you should have?
How do you know you called the 4 parameter overload everywhere you should have instead of the three parameter overload?
3x12=36 2x12=24 1x12=12 0x12=18
-
Electron Shepherd wrote:
How do you guarantee that you have passed in the fourth parameter everywhere you should have?
How do you know you called the 4 parameter overload everywhere you should have instead of the three parameter overload?
3x12=36 2x12=24 1x12=12 0x12=18
Becuase my point was to make the parameters non-optional. Then the compiler spots all those cases for you.
-
The trouble with optional parameters comes when you extend them
public static void DisplayName (string lastName, string firstName,
string middleName = null)If I now add a salutation:
public static void DisplayName (string lastName, string firstName,
string middleName = null, string salutation = null)all my code still compiles. And that can be a problem. I have a lot of work to do to identify all the places that I need to pass in the new, fourth, parameter (I must need it in at least one place, or else why change the function). Without optional parameters, the compiler does my impact analysis for me. In this case, the worst that happens is that the salutation is missed off a displayed name, but in some cases, you can introduce some subtle bugs.
Electron Shepherd wrote:
I must need it in at least one place
Not if it's framework code that you may not be using at all. I write a lot of methods I never use.
-
The trouble with optional parameters comes when you extend them
public static void DisplayName (string lastName, string firstName,
string middleName = null)If I now add a salutation:
public static void DisplayName (string lastName, string firstName,
string middleName = null, string salutation = null)all my code still compiles. And that can be a problem. I have a lot of work to do to identify all the places that I need to pass in the new, fourth, parameter (I must need it in at least one place, or else why change the function). Without optional parameters, the compiler does my impact analysis for me. In this case, the worst that happens is that the salutation is missed off a displayed name, but in some cases, you can introduce some subtle bugs.
You would have run into the same problem if you'd used method overloading instead of optional parameters; you'd still have to manually identify the places where the extra parameter should be used. Anyway, you could just comment out the
= null
and recompile again :)Regards Senthil _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro
-
I just read this[^] and thought "what on earth is going on here"? When I moved from VB.Net to C#.net, one of the big evangelistic arguments was around optional parameters as opposed to overloaded methods. I always liked optional parameters, but was prepared to give them up if the general feeling was that they where evil. Seems they aren't evil any more. I think Microsoft is just messing with my head and they will be removed in 5.0. I do like the named parameters though. I have been wanting those ever since I did some Office Automation stuff.
I finally tried one the other day, then refactored the need away. It's a good tool to have in the toolbox, but I don't expect to use it all that much.
-
Two words - side effects... What I've noticed is that only ex-VB programmers seem to be excited about optional and named parameters.
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001John Simmons / outlaw programmer wrote:
side effects
Did you mean dependencies between parameters?
void Method(int x, int y) {}
int z = 2;
Method(y : z, x : ++z)Regards Senthil _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro