A concept common in early computer science courses is aliasing. Alarmingly, however, there seems to be very few resources on the topic online. As a result, I figured I’d take a quit stab at it!
Table of Contents
What Is Aliasing?
Aliasing refers to a scenario when two or more variables refer to the same value in memory. As a result, aliasing can only occur—to my knowledge—in programming languages that support references.
References, for the uninitiated, are variables that store the location of some data. For folks who have worked with lower level programming languages, you can think of a reference as a slightly more opaque pointer. In other words, a reference doesn’t store a value directly, but instead directs you to where that value is stored.
Because references are one step removed from the actual value, situations can arise where multiple variables reference the same value. Therefore, anything that happens to the value will be reflected in every variable.
If it helps you think about aliasing, imagine giving your address to a painter and your friend. Tomorrow, your friend comes over to hang out. Then, a day later, the painter comes and changes your house color from red to blue. When your friend comes back to visit a week later, they’ll see the changes.
To extend the example above, any number of folks could have a reference to your house. Therefore, any changes to your house (e.g., a new couch, a bit of remodeling, etc.) could be seen by these folks. Consequently, any of these folks could make a change to your house. In some cases, this situation is desirable. For example, you probably want the painter to paint your house. In other cases, it’s not quite as desirable. For instance, you probably don’t want your house egged.
Ultimately, the house example doesn’t perfectly model how references work because there are definitely unexpected things that can happen in code. For example, you might want to make a change to your house that you don’t want others to see. In code, you would make a copy of that house. In real life, that’s probably not so practical.
Why Does Aliasing Happen?
In programming, aliasing is generally unintentional. It occurs all the time, and you might not even notice it. For example, every time you pass a reference to a function, that reference is copied to the function parameter. As a result, you end up with two variables pointing to the same value.
Other times, aliasing is a bit more intentional. For example, you might have a set of objects that model a real world family. In a family, items are often shared by the collective. If you were modeling that in code, you might have items like silverware that everyone has a reference to (e.g., everyone knows where the spoons are). It would be impractical to withhold that knowledge from your family and only provide a spoon anytime someone asked for it (though, this example doesn’t really map to code very well).
Finally, there is the form of aliasing that our field tends to hate: aliasing as a result of assignment. In many programming languages, the assignment statement does not perform a copy. Instead, it creates an alias. As a result, while the new variable may appear to hold a copy, it actually holds a reference to the original value. Therefore, any changes to one variable will be seen by the other.
Is Aliasing Bad?
Short answer: maybe.
In general, aliasing is a necessary evil. For example, in functions, copying the reference allows us to make changes to its underlying value. Without that ability, functions could never make changes to a value. Instead, a new value would have to be returned and reassigned to the original variable. You might already be familiar with this kind of behavior with immutable reference types (e.g., strings).
However, there is a particular brand of aliasing that can cause a lot of pain: aliasing by assignment. When variables are unintentionally aliased through assignment, the outcome is most likely going to be bizarre bugs. After all, the intent of using assignment is probably to create a copy, and that is just not how assignment works in many languages.
Therefore, if you want to avoid aliasing, you need to learn how to make copies. Here are a couple resources that cover that subject for my Python and Java users:
- Be Careful When Copying Mutable Data Types
- How to Copy a List in Python: Slice, Copy, and More
- How to Swap Java Reference Types in a Method
Overall, I was a bit surprised by the lack of writing on this subject on the web, so I don’t have any external resources or takes. Though, I’ve seen quite a bit about the issues that aliasing can have on program performance. At any rate, that’s all I wanted to cover today. If you found this article helpful, feel free to show your support by giving it a share and heading over to my list of ways to grow the site. Otherwise, take care!
Recent Posts
Python has a cool feature that allows you to overload the operators. Let's talk about what that means and how you might use it!
This week, we're hitting another beginner topic: the assignment operator. While the idea is simple, the concept is rich in related ideas like scope, iterable unpacking, and augmented assignment.