Developer Fundamentals: Variables

A contextual aside for self taught coders.

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


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

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:

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 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:

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
console.log(fortyTwo)
// prints the value of fortyTwo which would now be 84

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

$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 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.


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-09-07T00:04:38+00:00) Developer Fundamentals: Variables. Retrieved from https://www.scien.cx/2022/09/07/developer-fundamentals-variables/

MLA
" » Developer Fundamentals: Variables." Margaret Staples | Sciencx - Wednesday September 7, 2022, https://www.scien.cx/2022/09/07/developer-fundamentals-variables/
HARVARD
Margaret Staples | Sciencx Wednesday September 7, 2022 » Developer Fundamentals: Variables., viewed ,<https://www.scien.cx/2022/09/07/developer-fundamentals-variables/>
VANCOUVER
Margaret Staples | Sciencx - » Developer Fundamentals: Variables. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/07/developer-fundamentals-variables/
CHICAGO
" » Developer Fundamentals: Variables." Margaret Staples | Sciencx - Accessed . https://www.scien.cx/2022/09/07/developer-fundamentals-variables/
IEEE
" » Developer Fundamentals: Variables." Margaret Staples | Sciencx [Online]. Available: https://www.scien.cx/2022/09/07/developer-fundamentals-variables/. [Accessed: ]
rf:citation
» Developer Fundamentals: Variables | Margaret Staples | Sciencx | https://www.scien.cx/2022/09/07/developer-fundamentals-variables/ |

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.