Language: EN

programacion-que-es-una-constante

What is a constant

Constants in programming are similar to variables, with the (huge) difference that once a value is assigned, it cannot be modified.

During the execution of our program, there are certain values that will not change. The most typical example is the value of PI, which will not change throughout the execution of your program. But there are many more.

// Constant declarations
const PI = 3.14159
const DAYS_IN_WEEK = 7
const SECONDS_IN_MINUTE = 60
const MONTHS_IN_YEAR = 12
const SPEED_OF_LIGHT = 299792458  // in meters per second

These are “quite absolute” constants. They are like this because they are part of our reality. But they are not the only ones we will have.

For example, imagine you are making a program that manages vehicles. For you, in the model you have created, a vehicle can only have a maximum of four wheels. So you would have

const MAX_WHEELS = 4

What would happen the day you have to manage a truck with 8 wheels? Well, basically, you have a problem and you will have to review your code 😞.

But what is important right now is to clearly see that what is constant, and what is not constant, depends on your program and the solution you have planned to give it.

How is a constant defined?

Most languages have a reserved keyword to indicate defining a constant. In most languages, the word is const (although not in all).

Since a constant cannot be reassigned another value, in general, the assignment must be done at the time of declaration.

So, for example, in C++, Java or C# (strictly typed) a constant is declared like this

const int MAX_WHEELS = 4

While in JavaScript (dynamically typed) it would not be necessary to indicate the type, and it would look like this.

const MAX_WHEELS = 4

However, not all languages have constants. For example, Python does not have the concept of a constant. Therefore, normal variables must be used.

What are constants for?

We have seen that constants behave very similarly to a variable (in the sense that they are containers where we can store data).

So, what is the usefulness of not being able to reassign the content? Well, they have a couple of advantages.

The main one is that they increase the security and cleanliness of the code. They prevent a value from being accidentally changed. If I try to reassign a constant, the compiler or interpreter will tell me “hey, buddy!, you can’t do that!“.

On the other hand, it improves the readability of the code. When you mark a variable as constant, you are telling the next programmer not to worry too much about that value, as it will not change.

Finally, it can also improve the performance of the program, as compilers can optimize the code that uses constants. For example, they can decide to store them in another memory area, or even completely replace it with its value.

The latter depends on the compiler we use. Most modern compilers are capable of identifying constants, even if we do not mark them as such. But, in general, if it has any effect on performance, it will be a small improvement.

Constant vs immutable Advanced

That a variable is defined as a constant does not mean that its value cannot change (that it is immutable). Sorry if I blew someone’s mind after saying 20 times throughout the entry that constant means it cannot change.

It’s a bit complex. But, in summary, we have said that a variable or a constant are containers or “boxes” where we store data. Being constant means that I cannot take your box away and give you another one, but no one tells me that I cannot change what is inside the box 😅.

Both things are true. A constant cannot change, but no one guarantees me that its content cannot change. It’s a complicated topic that requires discussing references first, and we will talk about it in its own entry at the right time.