Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with output parameters?

Tags:

c#

sql

vba

vb6

Both in SQL and C#, I've never really liked output parameters. I never passed parameters ByRef in VB6, either. Something about counting on side effects to get something done just bothers me.

I know they're a way around not being able to return multiple results from a function, but a rowset in SQL or a complex datatype in C# and VB work just as well, and seem more self-documenting to me.

Is there something wrong with my thinking, or are there resources from authoritative sources that back me up? What's your personal take on this and why? What can I say to colleagues that want to design with output parameters that might convince them to use different structures?

EDIT: interesting turn- the output parameter I was asking this question about was used in place of a return value. When the return value is "ERROR", the caller is supposed to handle it as an exception. I was doing that but not pleased with the idea. A coworker wasn't informed of the need to handle this condition and as a result, a great deal of money was lost as the procedure failed silently!

like image 811
Chris McCall Avatar asked Sep 16 '09 17:09

Chris McCall


People also ask

What are output parameters?

Output parameters are the parameters that are fetched from the response of a service call. These are formatted according to the attributes you configure for the output before displaying on the device. The service parameters have a scope and data type attached to them.

Are out parameters a code smell?

Output parameters are the kind of code smell that is sometimes difficult to see, but when they crop up they can cause a moment of confusion, as they obscure the purpose of a function call, and therefore make our code less readable.

Where are output parameters used?

The OUTPUT parameter is used when you want to return some value from the stored procedure. The calling program must also use the OUTPUT keyword while executing the procedure. The following stored procedure contains INPUT and OUTPUT parameters.

Can function have output parameters?

You can't use OUTPUT parameters with a user defined function (UDF). By definition a scalar function just returns one scalar value. You have two options: 1 - Make this a stored procedure using OUTPUT parameters.


2 Answers

Output parameters can be a code smell indicating that your method is doing too much. If you need to return more than one value, the method is likely doing more than one thing. If the data is tightly related, then it would probably benefit from a class that holds both values.

Of course, this is not ALWAYS the case, but I have found that it is usually the case.

In other words, I think you are right to avoid them.

like image 185
Brian Genisio Avatar answered Sep 19 '22 08:09

Brian Genisio


They have their place. Int32.TryParse method is a good example of an effective use of an out parameter.

bool result = Int32.TryParse(value, out number);
if (result)
{
    Console.WriteLine("Converted '{0}' to {1}.", value, number);         
}
like image 43
Andrew Cowenhoven Avatar answered Sep 18 '22 08:09

Andrew Cowenhoven