473,387 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 17671
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: (Pete Cresswell) | last post by:
I've dabbled in "Optional" over the last few days and think I'm coming down against using it. Seems to me like it makes the code harder to read and more complicated. Instead of using Optional,...
13
by: William Ryan | last post by:
I just picked up a copy of John Robbins' debugging book and started to look at disassembled code. Anyway, I hate optional Parameters in VB, but I was checking them out to see what IL is created. ...
21
by: Marc DVer | last post by:
I am trying to create a query that can be loaded as a querydef object but not having to assign values to the parameters if I don't want to. Normally when using a parameter query in VBA my code...
16
by: ad | last post by:
Does C#2.0 support optional parameters like VB.NET: Function MyFunction(Optional ByVal isCenter As Boolean = False)
12
by: Nick Hounsome | last post by:
Can anyone tell me what the rational is for not supporting optional arguments. It is obviously a trivial thing to implement and, since C++ has them, I would not expect them to be omitted without...
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
12
by: pamelafluente | last post by:
Hi guys, In the past I have used several time optional parameters in my function. But Now I am more inclined to think that they are more dangerous than useful, and probably better to be...
7
by: Sam Shrefler | last post by:
I'm working on creating a WebService / WebMethod to receive a record in real time from another system. The record contains about 20 fields. 10 of which aren't required. I was planning on just...
1
by: peridian | last post by:
This is more of a general question, but I didn't know where to post it. Since Java is an example of a language which does this, I thought here would work. Coming from a C++ background, having...
7
by: jamesclose | last post by:
My problem is this (apologies if this is a little long ... hang in there): I can define a function in VB.NET with optional parameters that wraps a SQL procedure: Sub Test(Optional ByVal Arg1...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.