Action by reference

The son hits his finger and the father feels the pain, regardless of where the father is (or possibly when the father was/is… uh, what?).

But that’s the image I get from the source code I’ve been reading lately. And I’ve been reading a lot of source code (it’s for research). Here’s an example:

TheFather father = new TheFather();
TheSon son = father.Son;

father.FeelsPain = false;

son.HitFinger();

// I bet you nuggets to donuts that father.FeelsPain is now true
if (father.FeelsPain) Console.WriteLine("It hurts!");
else Console.WriteLine("Nahh, no biggie!");

Is there some coding paradigm I don’t know about?

Basically you have a parent class, and you get some child property/class from it. Then you manipulate the child property/class, changing some properties perhaps, or calling some of its functions.

And then the parent class automagically has the changes from its child property/class updated.

That’s the equivalent of NASA sending Curiosity to Mars and NASA automagically gets real-time footage from its rover. The rover doesn’t need to send the updates back home at all.

In the programming realm, this is similar to passing variables by reference instead of by value. Done within known and well-defined scope, this can be useful. Otherwise, they’re equivalent to global variables.

So what’s the background?

Reading the source code of other open source spreadsheet libraries (ahem), and using these libraries, I found this to be common:

var worksheet = workbook.Worksheets["Sheet1"];
// do something with worksheet

Then the workbook automagically have the changes on the worksheet updated into itself.

Then I went to read the example source code of commercial spreadsheet and word processing libraries (ahem), and this behaviour still held true.

Perhaps this makes sense to you. Personally, I find it confusing. I sent my son to get donuts. He seemed to have bought the donuts. But he didn’t report back. Should I call him? Shouldn’t he be home by now? Where are the donuts? What’s going on?

Now I’m hungry…