Developer Fundamentals: Variables
Time to read: 4 minutes
A contextual aside for self taught developers.
A variable is a super broad concept, so we’re going to look at it with the more narrow and specific goal of what will be useful context for learning materials about coding. Variables are going to be a central part of coding whether you’re learning to code for anything from games, apps, websites, or robots.
Variables in code are named placeholders for values the computer will assign when the program is run.
An example in Python:
Environment Variables
Environment variables are variables whose values are set outside of the program context which is using them, but which aren't actively passed in like a parameter / argument. Environment variables are set and stored in the operating system in which the program is being run.
Environment variables can be tricky because, just like running programs, they tend to be tied to a specific user and can additionally be tied to an individual session. So if you set an environment variable and it seems to have vanished, there are a few steps to check in your troubleshooting:
- Did I set the environment variable on a different machine from where the program is currently running?
- Did I set the environment variable for a different user that the program is currently being executed by?
- Did I set the environment variable for a session that has since expired?
Environment variables tend to be used for things that don't make sense to include in a code repository either for security reasons, like account credentials, or because they are implementation-specific, like operating system information.
Constants
Constants are similar to variables, but they can only be assigned once. Variables tend to be used more often than constants, because they’re more flexible, allowing their value to be overwritten any number of times. Constants are great to use in situations where you definitely don't want to accidentally change a value once it's been set.
For example:
- You're writing a program that does a bunch of math using pi to 5 decimal places.
- You could use a math function that calculates pi and tell it to truncate it to 5 decimal places every time, but if you instead do that once and assign the output to a constant, then if you change your mind about how many decimal places you want your pi calculations to use, you can change it in just one place.
- If you did the same thing, but assigned your pi calculation to a variable, then you might have situations where the pi variable is modified causing some of your pi calculations to be different than you'd expect. To troubleshoot those differences you'd have to look at every piece of code that interacts with the pi variable to find where it was modified.
An example in JavaScript:
Naming
Was that last example confusing? Naming variables can be tricky! Writing a program that a computer can read and run successfully is important, but writing a program that humans can read and reason about is just as important!
It takes energy to switch between different contexts. Because so much of code is composed of variables, differences in how individuals and organizations name variables can make the same coded action look dramatically different, creating an unfortunate energy tax for readers not skilled in separating variable naming schemes from code structure and syntax.
Identical examples in PHP:
Style #1
Style #2
Some programming languages start all variables with a special character but most don’t. Some programming languages make you declare what type of value a variable will hold when you create it and many don’t. Some programming languages let you declare new variables anywhere in a program, but some have special rules about it. Most learning resources will assume you know those language specific rules for whatever programming language you’re using.
It can be helpful to remember that as far as the computer running the program is concerned, the name of a variable is completely meaningless.
The reason to not name your variables a, b, c, d, e, f, g, etc is because humans also need to be able to read code for it to be useful as more than a learning exercise. If you see example code where all the variables are named with single letters the most likely reasons are:
- The writer is signaling that the variables are not important so you’ll focus on the surrounding code.
- The writer is grounded in algebra, geometry, physics, or another specialty where single letter variable names are a cultural norm.
- The writer, or the organization they’re writing for, subscribes to a style guide that values concise naming over all other naming concerns.
Even if the only human who will ever read, run, or otherwise interact with a piece of code is you, how you name your variables will impact your ability to read, run, and modify that code in the future. Because of this, organizations will often develop style guides they ask coders to stick to voluntarily.
Style
Sometimes programmers will codify a set of style recommendations (style guide examples: php, JavaScript, go) into a tool called a code linter (linter examples: php, JavaScript, go). A code linter can be run against a program to identify any ways that it differs from whatever set of style recommendations it’s been designed for. These can be great tools developing consistent coding habits that will make collaborating with others (including future you) easier.
In addition to these more broadly accepted and encoded style guidelines, many organizations create their own set of rules and recommendations that impact how variables get named in production code, example code, and in learning resources.
As an individual, a great starting place is descriptive naming. In your own code, try to come up with names for variables that will tell future you as clearly as possible what the variable is for. When reading learning resources and example code, try reading it as if the programmer who wrote it named their variables the same way – trying to convey to the reader as clearly as possible what the variable is for.
If you’re working with a bunch of resources from the same organization, and their variable naming is difficult to understand, see if there’s a style guide you can reference to help you read and write code that matches what they expect.
If you’re working with resources from a variety of individuals and organizations, and the diversity of variable naming is difficult to understand, remind yourself that the specific names don’t matter, rewrite the examples with your own names (or even a, b, c, d, e), and see if you can make sense of the code around the variables once the distraction of how-things-are-named is removed.
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.