Let’s talk about variables

August 04, 2021

variable type

Programming languages are tools for communication between humans and machines. They provide various abstractions for executing operations without the need to understand the underlying technical details. One of the vital concepts that exist in many programming languages is remembering values and operating upon them. The feature which realizes this idea is known as a variable.

What is a variable?

In short, a variable is a relation between an identifier and a value. On a simple level, we create a specific name, which identifies our variable, and we use it to save and read particular values. The abstraction hides the underlying mechanism, which performs operations over memory that stores data.

To fully understand the idea of variables, we need to recognize the middle entity that exists between the identifier and the value. This element is an addressable storage location. Having that, the relation we describe becomes much more interesting. It is responsible for recognizing the symbolic name that identifies the storage location’s address and uses it to reference the physical memory where we can keep the value itself.
That story is close enough to the actual implementation of variables’ logic.

Again, the most critical job for a variable is to assign a value and then read it. Nevertheless, an exciting thing to notice is how the variable behaves when used with the assignment operator.
On the one hand, when we place our variable on the left side of the operator, it behaves as an address of the storage location. It means that it is supposed to save the value to the proper place.
On the other hand, when we place our variable on the right side of the operator, it behaves like an actual value. Thus, we may use it in calculations with other operators and assign its content to another variable.

What is a type?

The type of a variable specifies the span of values entitled to be assigned to it. Every variable acts accordingly to its type, which defines how to operate on the value. Furthermore, the type is strongly related to the performance of allowed operators. That is because we need a specific behavior when adding two numbers and a different one when adding two strings.

The first thing to note about type systems is the approach to the variable declaration. The type may be explicitly manifested alongside the name or implicitly inferred from the first usage.
An explicit declaration combines the variable with a type at the time of its creation. As a result, we can only assign values of that exact type. On the other hand, an implicit declaration merely reserves the name because, at this point, the type is unknown. That means we can later assign any value we want.

The above division is usually related to two distinct typing approaches: a static one and a dynamic one.
Programming languages with a static-type system allow for code analysis during compilation time, producing optimized code which does not need to utilize further checks, and validations in run-time. This technique eliminates a large category of potential errors and allows for various code optimizations to be applied automatically. Languages like C/C++, Pascal, Java, and Haskell are in the statically-typed category.
The dynamically-typed languages are usually interpreted. This is because the potential compilation step does not have enough information about variables and their content ahead of the execution phase. Furthermore, the dynamic approach is considered to be more flexible and easier to start working with. Moreover, the very idea of an interpreted language allows loading and executing additional code at any point. The obvious downside is the run-time type validation, which may be expensive. Variables have to remember their type alongside their value, allowing each operator to figure out the proper behavior. Languages like JavaScript, Python, and PHP are in the dynamically-typed category.

Conclusion

Variables lay at the root of programming languages and provide the necessary abstraction to modify and read stored content. One of the main properties of a variable is its type which, thanks to the language’s type-system, specifies the framework for executing and validating all operations using variables.

The choice between statically-typed and dynamically-typed languages presents a tradeoff between a safer, well-defined, compiled code and a flexible, boilerplate-free, interpreted implementation. The choice depends on the priorities.

Next time, we will further discuss other factors of variables. See you!