3.6. Creating and Using Constants

I've been focused on variables but not all data is actually variable. You might want some data in your program which doesn't vary. It just doesn't change. For example, you may define a few strings to contain messages and expect to use them later in your source code, but you don't expect to change their values.

For example, if you create a floating-point variable to hold the value of π (Pi) which is approximately 3.14159, or perhaps the maximum players that your game supports, you're never going to want to change these. There's no reason to change π to 4 or 9 or -700, so it's very common to need a piece of data in your program which just doesn't need to change.

Image

Fig 3.6.1: Comparing variables and constants

Now it's completely true that we could just create a variable, set its value and then just not change it. But many languages support the idea of a constant, a constant is very similar, almost identical to a variable. It has a name, a value and a specific type. The one difference is when our program runs, we can only assign a value to it once and then that piece of data stays fixed, locked and constant at that value.

Any attempt to change it beyond that first assignment will cause an error. There are a couple of benefits to this. One is if you can lock down a piece of data, it prevents either accidental changes or any misunderstandings about what that data means, particularly from multiple developers all working and writing code on the same project.

Another benefit is that in some languages, data stored as constant is actually more efficient than the same data stored as variables because the language can optimize the memory differently if it knows that data is never going to change.

How do we do this? Here’s a quick example or two of syntax. In some languages you use an additional keyword whereas in some other languages you use a different keyword. Here's what I mean.

In the language C#, to declare a string variable, I'd write it this way:

string message = “Thanks for Playing!”

That is, I give it a string literal to set the value. To make this a constant, I add an additional keyword “const” at the beginning:

const string message = “Thanks for Playing!”

That's it. This is now a fixed value and it cannot be changed to a different value somewhere else in my source code.

In the Swift language we use a different keyword. The normal way to create a variable is using var. For example,

var message: String = “Thanks for Playing!”

But if I want a constant, I just swap out that keyword with a new one, let, and everything else stays the same:

let message: String = “Thanks for Playing!”

That's it.

In some languages like Python, you just don't make a constant. There is no formal way to make a constant. You just make a variable and try not to change it.

One final word on constants is that in some languages it is common to see the name of constants written in all upper case as opposed to camel case or lowercase.

Image

Fig 3.6.2: Examples of how to create constants in different languages

Now this is another naming style. It's not a rule, but it can make things easier when you're reading a lot of code to visually distinguish between what has been defined as variables and what have been defined as constants.