Language: EN

que-es-una-referencia

What is a Reference

Today we’re going to talk about the concept of REFERENCE in programming. It’s a very important concept (and very simple if we understand it correctly).

However, it is one of the topics that confuses programmers the most, even those who have been programming for a longer time (it’s not clickbait, I assure you it’s true. The concept of REFERENCE is terribly misunderstood).

The use of references has significant implications regarding aspects like constant / immutable, value types / reference types, stack, heap, efficiency… and countless other things. So it’s advisable that you try to understand it well.

Moreover, it is a concept that applies to all programming languages, whether you know it or not. While the more modern languages try to hide some of the difficulty in managing them, in others like C++ or Java it is more visible.

So let’s get to the point. What is a REFERENCE? Let’s see what the dictionary says:

A reference is a relationship established between an expression and what it refers to.

Okay… we’re back to where we started, thank you very much, dictionary. So let’s look at a practical case instead.

Imagine you have a book. At the end of the book, you want to add a bibliography to another book. Obviously, you do not copy the entire book you want to mention; you simply add a review. In other words, you reference other books 😉.

curso-programacion-referencia

That is to say, a reference is an element that serves as a link to direct us to other information. But it does NOT contain the information; the information is somewhere else. The reference is just the link.

Well, I managed to finish the introduction without saying that a reference is something that refers to something! Good for me! 👏

References in Computer Science

In computer science, we have a very everyday and very simple example that you use every day, and that will help us explain what a reference is: shortcuts to files and folders.

You have your favorite movie on your hard drive. The Dark Knight, the Avengers, Frozen. Or whatever, I don’t care. But it’s on your hard drive, with director’s commentary and 4k resolution, taking up its 20GB.

referencia-1

Your movie

Now you have a shortcut on your desktop. When you double-click it, the movie opens. But the shortcut is not the movie. It’s just a link that takes you to the real movie.

referencia-2

A shortcut to the movie

In fact, you can have different shortcuts that direct you to the same movie. There is only one movie, but you have different accesses through which you can reach it.

referencia-4

Multiple shortcuts to the same movie

Well, that’s a referencing mechanism. In this case, of your Avatar movie. Or Interstellar, Toy Story 3. Or whatever, it still doesn’t matter.

Now, a typical mistake of a person starting in computer science. “Luis, I sent you 218 movies in this email.” And you think… “218 movies? How can that fit in an email? And it took him 2 seconds to send it?”

Obviously, NO. Your rookie friend just sent you the shortcuts 🤦‍♂️. Shortcuts weigh very little, because they do not contain the real movie. So he can send you thousands. But they are useless to you because the information is not on your computer.

References in Programming

In programming, REFERENCES are variables that do not contain data, but contain a link to the data. Just like the shortcut did not have the data of the movie, it was just an access to the data.

Why would someone come up with such a mess? Well, as you can imagine, it wasn’t for fun. Without references, computers couldn’t function.

Mainly for two reasons:

Speed in Copying

Copying or moving REFERENCES is much faster than working with the actual data. Remember your rookie friend who only copied the shortcuts, not the movies. It’s much faster because references are very small.

If two parts of a program need to work on the same data, they can exchange references. Otherwise, each part would have to make its own copy, modify it, and return it.

References significantly improve the speed of programs. In fact, literally without references, we wouldn’t have computers.

Dynamic Memory

The other reason to use REFERENCES is the need to use dynamic memory. Your program doesn’t always know what data it needs. For example, it has to store numbers that the user enters… but we don’t know how many numbers will be entered.

Except for the simplest programs, most will need to reserve memory during their execution. To do this, they launch a process called allocation. In this process, the operating system and the Allocator are involved.

We won’t go into detail, but at the end of the process, we will have our space reserved in memory. But we have no idea where that space is; they have to tell us where it is. So they have to return a reference to us.

referencias-memoria-dinamica

Your variable is in room 0x23

It’s like when we go to book a room in a hotel. We go to the reception, ask for a room, and when they finish, they have to tell us what number it is.

What is a Pointer Advanced

Many languages have the concept of POINTER. Feared by some, loved by others, and overrated by almost everyone, the truth is that the term does not leave anyone indifferent.

So what is a pointer? A pointer is one of the simplest mechanisms of referencing. It is not the simplest, nor the most difficult, nor the most powerful. It is simply one of the simplest.

Basically, a pointer is a variable that contains a memory address, where there is a value. Pointers are called that way because they “point” to a memory address.

Think of a laser pointer, the kind you use in a presentation with a whiteboard. What is a laser pointer? Something that serves to point. Well, the same, only this is a pointer to memory, something that points to a location in memory.

As I said, POINTERS are a mechanism to make REFERENCES. All pointers are references, but not all references are pointers. There are other mechanisms, some better and some worse.

Internally, probably, all reference systems use pointers to a greater or lesser extent. Although in the end, this doesn’t say much. Yes, and all will use memory addresses and bytes and things, and what?

So I am going to demystify the concept of a pointer. What is much more important is that you understand the abstract concept of reference. Pointers are a particular case of implementing a referencing system.