Bug Vanquisher

13 January 2008

To Ref or Not To Ref

Filed under: Dev inside! — Tanveer Badar @ 8:22 PM

Shall I write void func( ref System.Uri ) or void func( System.Uri )?

Should

class something
{
     std::string* ptr;
};

or be done like this

class something
{
     boost::smart_ptr< std::string > ptr;
};

That, people, is the question!

Before answering this question, let us clarify some terms.

1- value semantics: upon assigning an object to another, its bit representation is copied to the assigned object, unless something fancy like copy constructor intervenes.

2- reference semantics: no object really contains what it says but has only a pointer to the actual bit representation, upon assignment this pointer is updated and both objects point to the same location in memory.

3- pass-by-value: parameter initialization obeys value semantics, newly initialized object contains an exact copy of initializer object, unless something fancy like copy constructor intervenes.

4- pass-by-reference: parameter points to the same object as the initializing object and remember the joke in java, I don’t know the current status

void swap( int a , int b )
{
    int temp = a;
    a = b;
    b = temp;
}

5- value types: value type obey value semantics by default but can be made to obey reference semantics by using explicit references or pass-by-reference across function calls.

6- reference types: by default they obey reference semantics and there is no known way to make them obey value semantics without some really fancy coding. This fancy coding typically requires that a reference type may exist in two forms, one mutable and other frozen. When value semantics are required you freeze an object and then, assignments make copies of it instead of setting the underlying pointer. But this requires fancy coding by someone.

The battle between value types and reference types is very much like "Men are from Mars and Women are from Venus’". Each has its merits and demerits. A comparison follows with examples from C++, C# and Java.

Value Types Reference Types

1- Default in C++.

1-Default in almost every other language.

2- Derived objects suffer slicing.

2- References are default.

3- Unnecessary copying, hidden costs.

3- No copying, unless explicitly asked.

4- Copy constructor often necessary, along with assignment and destructor.

4- Copy constructor1 semantically not present.

5- Explicit sharing of objects, no reference counting headaches2.

5- Multi-threading nightmare.

6- Helps in proper resource ownership3.

6- Mad COW4!

7- Function calls have pass-by-value as default.

7- Simulated pass-by-value can be a counter-intuitive.

8- References are step-child5.

8- Values types suffer6 from performance issue.

9- Destructors often have proper semantics.

9- Destructors entirely missing from picture, no deterministic clean-up.

10- Memory management is explicit (C++) or absent (CLR)7.

10- Typically, a garbage collection runs behind the scenes.

11-Nullable values types are often required when interfacing with cross-domain functionality.

11-Not nullable reference types are under active research.

[Nitpicker’s corner]
1- Memberwise clone and ICloneable do not have clearly defined semantics, they are even sort of deprecated.
2- I am not talking about COM here, it is only a core language level comparison.
3- RAII idiom.
4- I recommend you read that entire series.
5- I can’t seem to find a reference (no pun) at the moment, but it is impossible to write reference types in C++ as they are implemented in C# or Java.
6- The costs listed on that page may vanish in future, but I have plenty more reasons up my sleeve.
7- Please don’t even get me started on this!

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: