469,323 Members | 1,575 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

Optional out parameters?

This is just a suggestion.

When a method has an out parameter, the calling code has to supply a
target variable. I would like to be able to pass null instead,
indicating that I have no use for the out parameter.

For example, if I have a method ...
bool IsValidWeight(float weight, out string error)
.... I might want to check whether a weight is valid without caring
about the specific error ("value is negative", "value is too high",
etc.), in which case declaring and passing the unused variable seems
to complicate the code unnecessarily.

What do you think?

P.
Nov 16 '05 #1
12 17195
Paul,

I am not too sure I like it, only because it looks odd when the
signature is a value type, like this:

bool InvalidWeight(float weight, out int error);

Then the use of null is non-intuitive, and would be a detriment to the
language, IMO.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Paul E Collins" <fi******************@CL4.org> wrote in message
news:ct**********@hercules.btinternet.com...
This is just a suggestion.

When a method has an out parameter, the calling code has to supply a
target variable. I would like to be able to pass null instead, indicating
that I have no use for the out parameter.

For example, if I have a method ...
bool IsValidWeight(float weight, out string error)
... I might want to check whether a weight is valid without caring about
the specific error ("value is negative", "value is too high", etc.), in
which case declaring and passing the unused variable seems to complicate
the code unnecessarily.

What do you think?

P.

Nov 16 '05 #2
In C# you achieve this by providing an alternate method signature,
without the offending out argument.

The alternate implementation can then just declare the out argument,
pass it to the full method implementation, and simply not return that
argument.

Nov 16 '05 #3
Paul E Collins <fi******************@CL4.org> wrote:
This is just a suggestion.

When a method has an out parameter, the calling code has to supply a
target variable. I would like to be able to pass null instead,
indicating that I have no use for the out parameter.

For example, if I have a method ...
bool IsValidWeight(float weight, out string error)
... I might want to check whether a weight is valid without caring
about the specific error ("value is negative", "value is too high",
etc.), in which case declaring and passing the unused variable seems
to complicate the code unnecessarily.

What do you think?


Probably the easiest way of doing this is overloading the method, so
you also provide:

bool IsValidWeight (float weight)
{
string error;
return IsValidWeight (weight, out error);
}

(Note that exceptions would usually be a better way of indicating
errors.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
AFAIK there is no other way than providing another overload for
IsValidWeight without the out parameter.

Nov 16 '05 #5
AFAIK, there is no way except providing another overload for
IsValidWeight that doesn't have the out parameter.

Nov 16 '05 #6
I don't agree here. I think that for errors in what is classified as
"business logic", exceptions are not appropriate. Exceptions should be used
to handle the exceptional case, IMO. This kind of thing is not exceptional,
and has to be guarded against, but your app doesn't have to be blown out of
the water as a result.

In other words, exceptions shouldn't be used for recoverable operations,
IMO.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Paul E Collins <fi******************@CL4.org> wrote:
This is just a suggestion.

When a method has an out parameter, the calling code has to supply a
target variable. I would like to be able to pass null instead,
indicating that I have no use for the out parameter.

For example, if I have a method ...
bool IsValidWeight(float weight, out string error)
... I might want to check whether a weight is valid without caring
about the specific error ("value is negative", "value is too high",
etc.), in which case declaring and passing the unused variable seems
to complicate the code unnecessarily.

What do you think?


Probably the easiest way of doing this is overloading the method, so
you also provide:

bool IsValidWeight (float weight)
{
string error;
return IsValidWeight (weight, out error);
}

(Note that exceptions would usually be a better way of indicating
errors.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #7
"Nicholas Paldino [.NET/C# MVP]" wrote:
Paul,

I am not too sure I like it, only because it looks odd when the
signature is a value type, like this:

bool InvalidWeight(float weight, out int error);
if you think of "out int" or "ref int" as int*, then it's not THAT weird.

something like this would be somewhat useful when you do p/invoke. I've
seen api where pass in null to int* to indicate ignore output.

Then the use of null is non-intuitive, and would be a detriment to the
language, IMO.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Paul E Collins" <fi******************@CL4.org> wrote in message
news:ct**********@hercules.btinternet.com...
This is just a suggestion.

When a method has an out parameter, the calling code has to supply a
target variable. I would like to be able to pass null instead, indicating
that I have no use for the out parameter.

For example, if I have a method ...
bool IsValidWeight(float weight, out string error)
... I might want to check whether a weight is valid without caring about
the specific error ("value is negative", "value is too high", etc.), in
which case declaring and passing the unused variable seems to complicate
the code unnecessarily.

What do you think?

P.


Nov 16 '05 #8
Daniel,

There it would be helpful, but that hopefully will be resolved with the
introduction of nullable types. Hopefully we will see the P/Invoke layer
and COM interop use them for situations like this.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Daniel Jin" <Da*******@discussions.microsoft.com> wrote in message
news:57**********************************@microsof t.com...
"Nicholas Paldino [.NET/C# MVP]" wrote:
Paul,

I am not too sure I like it, only because it looks odd when the
signature is a value type, like this:

bool InvalidWeight(float weight, out int error);


if you think of "out int" or "ref int" as int*, then it's not THAT weird.

something like this would be somewhat useful when you do p/invoke. I've
seen api where pass in null to int* to indicate ignore output.

Then the use of null is non-intuitive, and would be a detriment to
the
language, IMO.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Paul E Collins" <fi******************@CL4.org> wrote in message
news:ct**********@hercules.btinternet.com...
> This is just a suggestion.
>
> When a method has an out parameter, the calling code has to supply a
> target variable. I would like to be able to pass null instead,
> indicating
> that I have no use for the out parameter.
>
> For example, if I have a method ...
> bool IsValidWeight(float weight, out string error)
> ... I might want to check whether a weight is valid without caring
> about
> the specific error ("value is negative", "value is too high", etc.), in
> which case declaring and passing the unused variable seems to
> complicate
> the code unnecessarily.
>
> What do you think?
>
> P.
>
>


Nov 16 '05 #9
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
I don't agree here. I think that for errors in what is classified as
"business logic", exceptions are not appropriate. Exceptions should be used
to handle the exceptional case, IMO. This kind of thing is not exceptional,
and has to be guarded against, but your app doesn't have to be blown out of
the water as a result.

In other words, exceptions shouldn't be used for recoverable operations,
IMO.


It's one of those things which very much varies by taste. One of the
things I like about exceptions is that I can't just ignore them absent-
mindedly. It's very easy to forget to check an out parameter or a
return value - but forgetting to handle an exception is harder, so you
don't tend to proceed with something without getting the prerequisites
right. (Your program may crash, but it won't overwrite good data with
bad, for instance.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10
Nicholis... I really like an explicit version of Henry Sutter's
approach. If the
method explicitly documents a precondition, then, if possible, the
method
throws an exception if the pre-condition is violated. This is
independent of
the presence of a recoverable operation. Thus two identical methods can
have
different behaviors, based on the explicity documented pre-conditions.
If a
pre-condition is declared, then the caller must validate the
pre-condition. So
if a a file must exist as a precondition, then the caller calls if
(File.Exist(myFile) or the method may throw an exception. If the the
method
does not declare that the file must exist as a precondition, then if the
file
dose not exist, the method does _not_ throw an exception. Instead it may
return say null.

Regards,
Jeff
In other words, exceptions shouldn't be used for recoverable

operations,
IMO.<
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #11
On Tue, 1 Feb 2005 15:30:02 -0500, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.com> wrote:
In other words, exceptions shouldn't be used for recoverable operations,
IMO.


I believe the C# design disagrees with you here, since the "catch"
keyword seems pretty much designed to deal with recoverable errors. :)
--
http://www.kynosarges.de
Nov 16 '05 #12
On Tue, 1 Feb 2005 15:30:02 -0500, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.com> wrote:
In other words, exceptions shouldn't be used for recoverable operations,
IMO.


I believe the C# design disagrees with you here, since the "catch"
keyword seems pretty much designed to deal with recoverable errors. :)
--
http://www.kynosarges.de
Nov 16 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by William Ryan | last post: by
12 posts views Thread by Nick Hounsome | last post: by
7 posts views Thread by Sam Shrefler | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.