Variables

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 ce…


This content originally appeared on Twilio Blog and was authored by Margaret Staples

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:

fortyTwo = 7 * 6
#declares a variable named fortyTwo and sets it equal to the product of 7 * 6
print(fortyTwo)
#prints the value of the fortyTwo variable which in this example would be 42

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:

var fortyTwo = 7 * 6;
// declares a variable named fortyTwo and sets it equal to the product of 7 * 6
const FORTY_TWO = fortyTwo;
// declares a constant named FORTY_TWO and sets it equal to the value of fortyTwo

fortyTwo = FORTY_TWO * 2;
// redefines the value of the variable fortyTwo to be equal to the product of FORTY_TWO * 2
print(fortyTwo)
// prints the value of fortyTwo which would now be 84

FORTY_TWO = fortyTwo;
// attempts to set the constant FORTY_TWO equal to the variable fortyTwo
print(FORTY_TWO);
// prints the value of FORTY_TWO which would still be 42 because you can't reassign the value of 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 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

$sid = getenv("TWILIO_ACCOUNT_SID"); 
$token = getenv("TWILIO_AUTH_TOKEN"); 
$twilio = new Client($sid, $token); 

$message = $twilio->messages ->create("+15558675310", 
  [
    "from" => "+15017122661", 
    "body" => "Hi there"
  ] );

Style #2

$x = getenv("env_var_pie"); 
$y = getenv("env_var_cake"); 
$client = new Client($x, $y); 

$initiate_api_action = $client->messages ->create("+15558675310", [ "from" => "+15017122661", "body" => "Hi there"] );

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.


This content originally appeared on Twilio Blog and was authored by Margaret Staples


Print Share Comment Cite Upload Translate Updates
APA

Margaret Staples | Sciencx (2022-06-30T23:07:36+00:00) Variables. Retrieved from https://www.scien.cx/2022/06/30/variables-2/

MLA
" » Variables." Margaret Staples | Sciencx - Thursday June 30, 2022, https://www.scien.cx/2022/06/30/variables-2/
HARVARD
Margaret Staples | Sciencx Thursday June 30, 2022 » Variables., viewed ,<https://www.scien.cx/2022/06/30/variables-2/>
VANCOUVER
Margaret Staples | Sciencx - » Variables. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/06/30/variables-2/
CHICAGO
" » Variables." Margaret Staples | Sciencx - Accessed . https://www.scien.cx/2022/06/30/variables-2/
IEEE
" » Variables." Margaret Staples | Sciencx [Online]. Available: https://www.scien.cx/2022/06/30/variables-2/. [Accessed: ]
rf:citation
» Variables | Margaret Staples | Sciencx | https://www.scien.cx/2022/06/30/variables-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.