Jump to content
Xtreme .Net Talk

Recommended Posts

  • *Experts*
Posted

I love watching people get fired up about things that aren't 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?

 

I've 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 it's 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 want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

I often use

int MyFunction(ref object aObject)

if MyFunction needs to manipulate aObject properties. . . the return is an error code depending on success

or

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

 

object MyFactoryFunction()

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

I never modify an object inside a function unless it's 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 ? You'll have to search for hours inside if it's this function who set an null somewhere.

 

So.... I'm going for the first option.

"If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown

"Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me

"A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend.

C# TO VB TRANSLATOR

Posted
I never modify an object inside a function unless it's 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 ? You'll have to search for hours inside if it's this function who set an null somewhere.

 

So.... I'm going for the first option.

Personally I do returns mostly.

 

Nothing against byRef, I just do returns. Chances are if I'm 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 you'd notice? I think byRef might be cleaner if you're super organized, which I'm not always :)

  • Administrators
Posted

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 I'm 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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

You know I thought about this, PD is right. I don't 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 don't really consider these things when programming - it just comes out that way.

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

I rarely use Ref.

A function like:

Add2Numbers(a,b,Result)

wouldn't make sense

 

but something like

 

Result = Add2Numbers(a,b) 'just an example :), obviously you wouldn't 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 don't feel like typing that in

 

Using a return type, you'd have to specify

Add2Numbers(1,2, Result) something like that, and oyu'd 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 don't want the return type (thus using it as a Sub)

 

The Pentium Guy

My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!)

vbprogramming.8k.com

My Project (Need VB.NET Programmers)

http://workspaces.gotdotnet.com/ResolutionRPG

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...