Debate: ref vs. return

Nerseus

Danner
Joined
Oct 22, 2002
Messages
2,547
Location
Arizona, USA
User Rank
*Expert*
I love watching people get fired up about things that arent necessarily important, but still fun to have opinions about... with that in mind:

Do you, or does your company, have a "standard" for using ref/ByRef vs ONLY allowing methods to return values?

Ive seen 3 typical scenarios:
1. [ref/ByRef is a strict no-no] A method should ONLY return a single value (the return type), ref/ByRef parameters used to return values is a no-no (maybe Ok to pass strings, for performance). If multiple returns are needed, us a custom object, hashtable, etc.

2. [ref/ByRef is perfectly acceptable] A method should return values using ref/ByRef whenever needed.

3. [ref/ByRef is mostly a no-no, but occasionally Ok] A method should almost never use ref/ByRef but its Ok in certain, VERY limited situations.

-ner

PS This comes to mind as a previous boss of mine is now looking to come work for my company as an "underling". He was/is extremely opinionated and once tied up a developer meeting for *hours* trying to get everyone convinced of something that he later flip-flopped on and then flip-flopped again a few months after that.
 
I often use
PHP:
int MyFunction(ref object aObject)
if MyFunction needs to manipulate aObject properties. . . the return is an error code depending on success
or
PHP:
int MyFunction(out object aObject)
if aObject is null, int is the error code

the later usually implies a Class Factory method though which would be

PHP:
object MyFactoryFunction()
 
I never modify an object inside a function unless its sent by value. I always return 1 data. And if I need to return more data.... well.. build a struct, make a hash table....

For me... data should be returned. Never modified. What if your data is set to null ? Youll have to search for hours inside if its this function who set an null somewhere.

So.... Im going for the first option.
 
Arch4ngel said:
I never modify an object inside a function unless its sent by value. I always return 1 data. And if I need to return more data.... well.. build a struct, make a hash table....

For me... data should be returned. Never modified. What if your data is set to null ? Youll have to search for hours inside if its this function who set an null somewhere.

So.... Im going for the first option.
Personally I do returns mostly.

Nothing against byRef, I just do returns. Chances are if Im using a group of data I create a struct to hold all of the data. It just seems to make sense and is easier to handle.

Is there a performance gain for byRef over structs that youd notice? I think byRef might be cleaner if youre super organized, which Im not always :)
 
If you are returning a null or setting a parameter to null you will still have a null value being passed around.

I personally tend towards option 3 - I avoid modifying refernces in a function an prefer to return a new object. I often find that in cases where Im passing by reference and modifying the object the code can be refactored into a method of the original class.
The cases where I do use references are often in either static methods, when their use makes the code cleaner or for the odd performance issue where creating and returning a temporary object may be undesirable.
 
You know I thought about this, PD is right. I dont often use the ref keyword on passed parameters, as most of the time the properties are being changed on a reference type, which means they are mutable. i never change the object reference itself in a method.

I change the props and return an error code.

There are certain times that I use the out modifier, but looking at my code it is always on a value type.

Its funny, but I dont really consider these things when programming - it just comes out that way.
 
I rarely use Ref.
A function like:
Add2Numbers(a,b,Result)
wouldnt make sense

but something like

Result = Add2Numbers(a,b) just an example :), obviously you wouldnt create a function for that.

Functions are more useful as well. For example if in the function you decide to... i dunno, instantiate 200 classes (just.. some random example),
and you dont feel like typing that in

Using a return type, youd have to specify
Add2Numbers(1,2, Result) something like that, and oyud have to declare Result.

Using functions:
Add2Numbers(1,2).. there you go, you dont even have to have somethign to the left of the equal sign, and now your function instantiates the 200 classes <REALLY bad example, but i hope you know what i mean :)>

So using functions, its not necessary declare the extra variable if you dont want the return type (thus using it as a Sub)

The Pentium Guy
 
Back
Top