Developer Fundamentals: Variables
Time to read: 4 minutes
A variable is a super broad concept, so today you’ll be looking at them with the specific goal of finding what practices and concepts will be useful to you as you learn more about coding. Variables are going to be a central part of your code whether you’re learning to code anything from games, to apps, websites, or robots.
In general, 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 that is using them, but aren't actively passed in like a parameter or argument. Environment variables are set and stored in the operating system in which the program is running.
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. If you set an environment variable and it seems to have vanished, there are a few steps to check in your troubleshooting:
- Did you set the environment variable on a different machine from where the program is currently running?
- Did you set the environment variable for a different user that the program is currently being executed by?
- Did you 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 and cannot be changed. 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.
Variables tend to be used more often than constants because they’re more flexible, allowing their value to be overwritten any number of times.
An example in JavaScript:
You can't reassign a constant.
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 understand 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. This creates an unfortunate energy tax for readers with differing code structure and syntax.
Identical examples in PHP:
Style #1
Style #2
Some programming languages start all variables with a special character or key word but some don’t. Some programming languages make you declare what type of value a variable will hold when you create it and some 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 in a nondescript manner (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 then some possible 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. A great starting point to get used to naming conventions is descriptive naming. In your own code, try to come up with names for variables that will tell future coders what the variable is for as clearly as possible.
Because of all these possible differences in coding syntax amongst coders, organizations will often develop style guides they ask their 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 to help develop 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. When reading others’ code, try reading it as if the programmer who wrote it named their variables descriptively – as if they were trying to convey to the reader what the variable is for as clearly as possible.
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 to your computer. Rewrite the examples with your own names and see if you can make sense of the code around the variables once the distractions of naming conventions are 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.