Language: EN

programacion-que-es-una-constante

What is a constant

Constants in programming are similar to variables, with the (enormous) 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 declaration
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 “fairly 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 creating 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 the important thing 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 the definition of a variable. 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 made at the time of declaration.

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

const int MAX_WHEELS = 4

While in JavaScript (dynamically typed), it would not be necessary to indicate the type, and it would be as follows.

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 good is it that we cannot 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, that it will not change.

Finally, it can also improve the performance of the program, since 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.

This last one depends on the compiler we use. Most modern compilers are able to identify 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 not immutable). Sorry if anyone’s head has exploded 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 the box away and give you another, 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 is a complicated issue, which requires talking about references before, and we will talk about it in its own entry at the right time.