Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   Recommended Practice for returning data from function (http://www.programmingforums.org/showthread.php?t=5427)

Arla Aug 16th, 2005 11:49 AM

Recommended Practice for returning data from function
 
Okay, so I've been thinking this through quite a bit, and have a few questions

I have some functions, that essentially return two disparate sets of data, one set of data is the error messages, which are standard across every function I write, the other is the actual data (these are database functions, so mostly return table data).

What I'm trying to work out is the best way to return this data, now it seems I have about three options.

1. Return error data from function as in (assume ErrorData is some sort of data structure)

:

public ErrorData Function(int x, int y, int z)

I like this approach at least for the ErrorData because it allows me to assign the return to a variable and immediately reference it, the other option would be to have it as a :out from the function eg

:

public void Function (int x, int y, int z, out ErrorData errors)

or even Ref (although since in my code errors start at the lowest level of the program and bubble up, I'd never have any need to pass the errors in).

Now same goes for the "return" data, the only issue being that having two sets of returns from a function isn't possible, so then I either have to create another class that contains both the error data and the return data, or I have to return one of them with out or ref parameter.

Alternatively, I can make variables inside the functions class public, and just reference them, however I don't like that as it seems to me it allows for them to change without necessarily meaning it (global variables).

So any thoughts? Currently I'm thinking maybe that passing the errors as the return from the function, and using out in the function for the return data, however a lot of things say "NEVER USE OUT parameters" but never seem to say why they are bad.

DaWei Aug 16th, 2005 12:21 PM

Obviously you've investigated your options and know what they are. The choice is yours and based on YOUR requirements and best judgement. If you feel like consulting others for their judgement, that's not an unwise thing to do. The unwise thing is to put a lot of stock in a high-priest/guru that will lay down the law (NEVER USE AN OUT PARAMETER) without telling you why.

Personally, I would avoid statics or globals; too much loss of control and widespread utility.

I probably also wouldn't require my classes to all have error data members, but I might. That depends upon the overall design and the robustness of the objectivity I had plans for.

The use of a reference (whether in the sense of C++, or in the sense of any pointer) can be confusing for follow-on maintenance if the documentation is lacking. It's perfectly functional but can be affected by multiple threads.

As you can see, there's no definitive answer in this response. If a single solution were good, and possible, you would already have found it, and thus not be posting. I just thought I'd give you some of my brief reasoning, rather than hand you a dictum, tell you to shut up, kiss my ring, and not ask questions.


All times are GMT -5. The time now is 4:20 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC