Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which is better, using a nullable or a boolean return+out parameter

Lets say I have a function that needs to return some integer value. but it can also fail, and I need to know when it does.

Which is the better way?

public int? DoSomethingWonderful()

or

public bool DoSomethingWonderful(out int parameter)

this is probably more of a style question, but I'm still curious which option people would take.

Edit: clarification, this code talks to a black box (lets call it a cloud. no, a black box. no, wait. cloud. yes). I dont care why it failed. I would just need to know if I have a valid value or not.

like image 208
Oren Mazor Avatar asked Nov 25 '09 21:11

Oren Mazor


People also ask

Is it good to use out parameter in C#?

Even Microsoft themself recommends not using out parameters. msdn.microsoft.com/en-us/library/ms182131.aspx When you boil it down, out is a way of returning multiple values instead of just one. But it's cleaner and more modular for a function to return a struct or class that has everything you need to return in it.

What is the difference between return value and out parameter?

If your method needs to return a value, then you must use return. Out is used where your method needs to return multiple values. If you use return, then the data is first written to the methods stack and then in the calling method's. While in case of out, it is directly written to the calling methods stack.

Should I use nullable?

You typically use a nullable value type when you need to represent the undefined value of an underlying value type. For example, a Boolean, or bool , variable can only be either true or false . However, in some applications a variable value can be undefined or missing.

What should I return in Boolean?

Return value from bool() It can return one of the two values. It returns True if the parameter or value passed is True. It returns False if the parameter or value passed is False.


2 Answers

I like the nullable version better, because you can use the null coalesce operator ?? on it, e.g.:

int reallyTerrible = 0;
var mightBeWonderful = DoSomethingWonderful() ?? reallyTerrible;
like image 185
Manu Avatar answered Oct 20 '22 18:10

Manu


It depends on how you think the calling code should look like. And therefore what your function is used for.

Generally, you should avoid out arguments. On the other hand, it could be nice to have code like this:

int parameter;
if (DoSomething(out paramameter))
{
  // use parameter
}

When you have a nullable int, it would look like this:

int? result = DoSomething();
if (result != null)
{
  // use result
}

This is somewhat better because you don't have an out argument, but the code that decides if the function succeeded doesn't look very obvious.

Don't forget that there is another option: use Exeptions. Only do this if the case where your function fails is really an exceptional and kind of a error-case.

try
{
  // normal case
  int result = DoSomething()
}
catch (SomethingFailedException ex)
{
  // exceptional case
}

One advantage of the exception is that you can't just ignore it. The normal case is also straight forward to implement. If the exceptional case something you could ignore, you shouldn't use exceptions.

Edit: Forgot to mention: another advantage of an Exception is that you also can provide information why the operation failed. This information is provided by the Exception type, properties of the Exception and the message text.

like image 29
Stefan Steinegger Avatar answered Oct 20 '22 18:10

Stefan Steinegger