Thursday, April 3, 2014

Reference/Dereference for languages

I get a bit confused on reference/dereference, etc... until I figured out an analogy that worked for my purposes.

A ref/reference to an array, hash is where it is in memory. (use \ in perl to reference)
Dereference is what the contents are.

In another environment, I've explained how, instead of automatically sending an attachment email, one should send a link to data, and not the data itself. "But why? Isn't that just another step for the end user?" Yes, but the key is ... it's cheaper for both parties to have knowledge of and access to where the attachment/data/array/hash is stored so it can be retrieved as often as necessary without having it take up space in email. "But the user has to click a link to download!" Yeah, but the user can do that when he's in a position to do that, and if you send *the link* again, the user won't have the [potentially large] attachment *again* in the inbox and the sender won't have the attachment *again* in Sent Items.

So it is with programming. If you want to pass a variable's data to/as part of another variable, it's cheaper to point to the location of the data than to pass the entire structure of the contents of the variable.

Think of it like symlinks or shortcuts to folders on another storage device. They are references to where data is stored, but you can make LOTS of symlinks, all having the same access to the same data, without duplicating the data.

No comments: