Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Syntax - Your preferred practice for getting 2 or 3 answers from a method

I'm just wondering how other developers tackle this issue of getting 2 or 3 answers from a method.

1) return a object[]
2) return a custom class
3) use an out or ref keyword on multiple variables
4) write or borrow (F#) a simple Tuple<> generic class
http://slideguitarist.blogspot.com/2008/02/whats-f-tuple.html

I'm working on some code now that does data refreshes. From the method that does the refresh I would like to pass back (1) Refresh Start Time and (2) Refresh End Time.
At a later date I may want to pass back a third value.

Thoughts? Any good practices from open source .NET projects on this topic?

like image 896
BuddyJoe Avatar asked Dec 03 '08 17:12

BuddyJoe


6 Answers

It entirely depends on what the results are. If they are related to one another, I'd usually create a custom class.

If they're not really related, I'd either use an out parameter or split the method up. If a method wants to return three unrelated items, it's probably doing too much. The exception to this is when you're talking across a web-service boundary or something else where a "purer" API may be too chatty.

like image 195
Jon Skeet Avatar answered Oct 17 '22 17:10

Jon Skeet


For two, usually 4)

More than that, 2)

like image 28
James Curran Avatar answered Oct 17 '22 17:10

James Curran


Your question points to the possibility that you'll be returning more data in the future, so I would recommend implementing your own class to contain the data.

What this means is that your method signature will remain the same even if the inner representation of the object you're passing around changes to accommodate more data. It's also good practice for readability and encapsulation reasons.

like image 25
Dave R. Avatar answered Oct 17 '22 16:10

Dave R.


Code Architeture wise i'd always go with a Custom Class when needing somewhat a specific amount of variables changed. Why? Simply because a Class is actually a "blueprint" of an often used data type, creating your own data type, which it in this case is, will help you getting a good structure and helping others programme for your interface.

like image 37
Filip Ekberg Avatar answered Oct 17 '22 16:10

Filip Ekberg


Personally, I hate out/ref params, so I'd rather not use that approach. Also, most of the time, if you need to return more than one result, you are probably doing something wrong.

If it really is unavoidable, you will probably be happiest in the long run writing a custom class. Returning an array is tempting as it is easy and effective in the short teerm, but using a class gives you the option of changing the return type in the future without having to worry to much about causing problems down stream. Imagine the potential for a debugging nightmare if someone swaps the order of two elements in the array that is returned....

like image 2
gillonba Avatar answered Oct 17 '22 17:10

gillonba


I use out if it's only 1 or 2 additional variables (for example, a function returns a bool that is the actual important result, but also a long as an out parameter to return how long the function ran, for logging purposes).

For anything more complicated, i usually create a custom struct/class.

like image 1
Michael Stum Avatar answered Oct 17 '22 17:10

Michael Stum