Passing by reference twice

J

Jon Skeet [C# MVP]

Mike said:
I think a success/failure or status return is quite handy:

if ( DoSomething( a, b, ref out c, ref out d))
// we're golden

This was especially true before nullable values, where it may be hard to
determine from the out params whether the function even suceeded if results
are allowed to be null.
As most functional languages have an implcit notion of success/failure
(where failure sometimes results in backtracking if the language supports
it) even if programming c# in a functional style, I don't think returning
true/false is so bad.

That's okay *sometimes* - but only sometimes. Unless you think people
are likely to call the method a significant amount in a "failure" case
(as per Double.TryParse etc) I'd favour an exception instead. It's far
more informative than a return value, and is a lot harder to
accidentally ignore.
 
M

Michael C

Mike said:
This was especially true before nullable values, where it may be hard to
determine from the out params whether the function even suceeded if
results are allowed to be null.
As most functional languages have an implcit notion of success/failure
(where failure sometimes results in backtracking if the language supports
it) even if programming c# in a functional style, I don't think returning
true/false is so bad.

It is if it's in place of an exception. So often I see code like this:

bool SetSomeValueInSomeList(int index, object value)
{
if(index < 0 || index >= mylist.Count) return false;
mylist[index] = value;
return true;
}

Michael
 
M

Michael C

Jon Skeet said:
That's okay *sometimes* - but only sometimes. Unless you think people
are likely to call the method a significant amount in a "failure" case
(as per Double.TryParse etc) I'd favour an exception instead. It's far
more informative than a return value, and is a lot harder to
accidentally ignore.

Plus a lot more consistant. :)

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top