C# Optional Parameters?
-
rastaVnuce wrote:
you can find all references with just two clicks.
I'm guessing you don't share code between different projects, then?
That's really not the point. If the parameter is critical you don't set a default value. If it's not, it means that it makes difference for a finite number of cases which you're aware about. So, you set a default value which would be properly handled for the rest of the cases. I really don't see a place for confusion. Over a decade of experience in working with a ton of different languages and technologies, I've never had a single issue with the default parameter. On the contrary, I've missed it a lot where not available.
We are using Linux daily to UP our productivity - so UP yours!
-
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.
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, 2001 -
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, 2001 -
John Simmons / outlaw programmer wrote:
only ex-VB programmers seem to be excited about optional and named parameters
Not really excited, more surprised. But, yes guilty as charged. ;)
-
about 20 overloads for MessageBox.Show[^] The cases where it subtly fails are much less than where it helps. A less subtle bug would be changing
public static void DisplayName (string firstName, string lastName = null)
topublic static void DisplayName (string firstName, string middleName = null, string lastName = null)
ouch! But hey, we can make it, we can break it.Agh! Reality! My Archnemesis![^]
| FoldWithUs! | sighist | µLaunch - program launcher for server core and hyper-v server.peterchen wrote:
public static void DisplayName (string firstName, string lastName = null) ==> public static void DisplayName (string firstName, string middleName = null, string lastName = null)
That does not compile, due to ambiguity:
DisplayName( "Peter", "Not Chen" ); // Cannot be resolved to one of the two overloads
..................... Life is too shor
-
peterchen wrote:
public static void DisplayName (string firstName, string lastName = null) ==> public static void DisplayName (string firstName, string middleName = null, string lastName = null)
That does not compile, due to ambiguity:
DisplayName( "Peter", "Not Chen" ); // Cannot be resolved to one of the two overloads
..................... Life is too shor
I meant changing the prototype from one to another, the second argument suddenly becoming the middle name
megaadam wrote:
DisplayName( "Peter", "Not Chen" );
Who's that?
Agh! Reality! My Archnemesis![^]
| FoldWithUs! | sighist | µLaunch - program launcher for server core and hyper-v server. -
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