programacion-que-es-una-constante

What is a constant

  • 4 min

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
Copied!

These are “pretty absolute” constants. They are like that because they are part of our reality. But they are not the only ones we are going to 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
Copied!

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 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 made at the moment of declaration.

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

const int MAX_WHEELS = 4
Copied!

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

const MAX_WHEELS = 4
Copied!

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 use 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 by mistake. 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 code readability. When you mark a variable as constant, you are telling the next programmer not to worry too much about that value, it’s not going to change.

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

This last point depends on the compiler we use. Most modern compilers are capable of identifying constants, even if we don’t 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 constant does not mean that its value cannot change (that it is immutable). Sorry if someone’s head just exploded, after saying 20 times throughout this post that constant means 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 I can’t take the box away from you and give you another one, but no one tells me I can’t change what’s 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, which requires talking about references first, and we will talk about it in due time in its own post.