John
Wed Jun 30 20:53:04 CDT 2004
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message
>> Because, IMHO, Optional parameters end up being more complex in practice.
They pretty much hide whats actually going on: an optional parameter hides
values, meaning you have to rely on documentation to understand whats being
passed as well as to what the passed values mean as far as behavior goes.
<<
Right but the same goes for overloaded methods... you're no longer providing
the parameter, so what would it default to? In VIsual Basic, for example,
you can specify the default value as part of the method signature. As such,
it would be displayed in intellisense. That's something which is impossible
with overloading -- because the defaulting is hidden away in code that's not
visible without manual documentation.
>> Another particular problem is that optional parameters do *nothing* to
express constraints. A method with 4 parameters, of which any three can be
provided or where all 4 can be provided or only 2 can, or whatever just
doesn't work. <<
I agree here, sometimes parameters aren't just optional they're mutually
exclusive, in combinations that are impossible to represent with optional
parameters. However this is an obvious candidate for overloading... but that
doesn't mean we shouldn't have optional parameters for all other situations,
they easily complement one another.
>> Note I would consider it bad practice to check an optional parameter for
null and then assign a property a generic value based on that null(ya know,
new MyObject()). <<
Yeah of course, I don't think anyone would advocate that, another good
reason to have optional parameters, rather than letting people try to
synthesize a half-baked optional parameter implementation themselves.
>> Optional parameters also have a particular limitation in that the default
value takes away one possible state...able to differentiate between a
default value x and the literal value y that happens to be equal to x may be
relevent <<
Yeah, this can and has been solved in Visual Basic, where you have the
IsMissing keyword that indicates whether or not a parmeter was actually
specified. eg. if (isMissing(price)) price = 23;
>> I also think that methods like MyMethod(a,,,,,,4,,,83,c) are pretty
unreadable. <<
VBA actually has some nice optional parameter syntax, it allows you to
specify the name of the parameter(s) in the method call, eg. MyMethod(
category:=CitrusFruit, price:=1.45, weight:=6 );
C# could quite easily adopt this syntax, and allow it for non-optional
paramaterized methods also.
>> Beyond that, the one implementation specific issue: optional parameters
don't version well. You have to recompile client code whenever the parameter
values change as they are inserted into calling code directly. <<
I think the above method of specifying optional parameters would version
quite well, so long as a parameter names didn't change. In fact it would
probably version better than a regular method (because parameter specifying
to parameter signature binding is explicit).
--
John Wood
EMail: first name, dot, last name, at priorganize.com
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:u3jrcwvXEHA.644@TK2MSFTNGP10.phx.gbl...
>
> "John Wood" <j@ro.com> wrote in message
> news:uhEOi2qXEHA.2944@TK2MSFTNGP11.phx.gbl...
> >I still don't get why they didn't add optional parameters in C#...
> > Constructor overloads can get pretty messy, given all the permutations
you
> > have to provide.
> >
> Because, IMHO, Optional parameters end up being more complex in practice.
> They pretty much hide whats actually going on: an optional parameter hides
> values, meaning you have to rely on documentation to understand whats
being
> passed as well as to what the passed values mean as far as behavior goes.
> That isn't particularly pleasent and doesn't tend to be that easy to debug
> or maintain.
> Badly written methods that use optional parameters can be more complex
than
> a set of overloads(a few dozen if statements in one method redirecting to
> sub methods or whatever, based entirely on which parameters were
provided).
> Another particular problem is that optional parameters do *nothing* to
> express constraints. A method with 4 parameters, of which any three can be
> provided or where all 4 can be provided or only 2 can, or whatever just
> doesn't work. Overloads are the proper choice here, optional parameters
are
> not substitutes for functional permutation, they are only for situations
> where you don't wish to pass a parameter and are happy with the default
> value. Note I would consider it bad practice to check an optional
parameter
> for null and then assign a property a generic value based on that null(ya
> know, new MyObject()).
> Optional parameters also have a particular limitation in that the default
> value takes away one possible state. Its usually irrelevent, but being
able
> to differentiate between a default value x and the literal value y that
> happens to be equal to x may be relevent, especially if you are using
> reference types(strings most likely). This manifests in nullablity as
> well(as best as I remember, the runtime doesn't allow that level of
> flexibilty but I may be wrong. These are conceptual arguments not
> implementation specific).
>
> I also think that methods like MyMethod(a,,,,,,4,,,83,c) are pretty
> unreadable.
>
> Beyond that, the one implementation specific issue: optional parameters
> don't version well. You have to recompile client code whenever the
parameter
> values change as they are inserted into calling code directly.
>
> Put all that together and I argue that optional parameters should rarely,
if
> ever, be used. There are just too many problems, too many tricks, and far
to
> many oppurtunities for misuse(and, examining history, misuse is the law of
> the land).
> > --
> > John Wood
> > EMail: first name, dot, last name, at priorganize.com
> > "Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
> > news:MPG.1b4cdb754a08b4ef98ae11@msnews.microsoft.com...
> >> John Wood <j@ro.com> wrote:
> >> > > Yes you can - just as your code below, but using "this" instead of
> >> > > "base".
> >> > Yeah true you can (I admit I forgot), but I rarely use that because
you
> > have
> >> > little control over when the referenced constructor is called (it
> >> > always
> >> > runs before the constructor body).
> >>
> >> That doesn't mean it's not useful though. I use it all the time when
> >> providing constructs which effectively default various values - each
> >> constructor calls a version with more parameters, either directly going
> >> to the "full" one or going in stages. I believe this is fairly common.
> >>
> >> --
> >> Jon Skeet - <skeet@pobox.com>
> >>
http://www.pobox.com/~skeet
> >> If replying to the group, please do not mail me too
> >
> >
>
>