Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Not a programming question, per se , but also seemed too close to programming to go in the lounge.

Our code is littered with methods like this:

C#
public int fetchStuff(int param1, int param2, out int returnA, out string returnB...)


Where this method performs some kind of computation based on params 1 & 2 (typically a db inquiry, but doesn't have to be).

This isn't about the camel casing of the method name. While that is wrong (and also indicative of the quality of code produced by people who do this--they can't get the little stuff right, how can they get the big stuff right?), I'm questioning the use of out variables. It seems so non-OO. Take some inputs and produce a result or throw an exception.

Also, the use of out variables means that the signature has to change when new data are added to the results. Sure, we can use overloads to have the old signature call the new method and throw away the new data to reduce the affect on consumers who don't care about new data, but that seems clumsy. (As opposed to returning an object, where we can add new information and consumers can use it or not, at their discretion, but signatures don't change).

OTOH, Microsoft themselves use out variables in the TryParse-type methods, so maybe it's something that really bothers me, but shouldn't.

What I have tried:

Depending on the situation and time constraints, I'll usually refactor these to return a StuffContainer object, then refactor the callers to reference that. Just not sure how necessary that is.

(Necessary not the right word; obviously there's no need to do this. I can't think of the right word though--not desirable, as it clearly is that.)
Posted
Updated 26-Feb-20 3:59am
Comments
F-ES Sitecore 26-Feb-20 10:01am    
If the function returns complex data then it should return a dedicated object with the relevant properties.
agolddog 26-Feb-20 10:18am    
Wow, thanks for the quick responses. But, as I said, not really a programming question; sure, we could use Tuples, or a dedicated object as F_ES Sitecore mentions.

The latter is the refactoring I tend toward, as it gives better context for the pieces of data than a Tuple solution.

The real question is, how much should the use of out variables bother me. It really grinds my gears, but I'm not sure if I'm overblowing the situation.

If out variables bother you, you can use Tuples instead.
See this CodeProject article: C# 4 - Tuples[^]
 
Share this answer
 
Well, you could return a triple tuple (The MS code was written before tuples were added to the C# specification) but that's fairly heavy, and may require additional code each time you call the method:
C#
private (int first, int second, string str) FetchStuff(int param1, int param2)
    {
    ...
    return (1, 2, "3" );
    }

C#
int intResult;
string stringResult;
...
var result = FetchStuff(1, 2);
intResult = result.first;
stringResult = result.str;
It's a bit more obvious what is happening though, when you get used to it.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900