Be Comfortable with Variables — Patrick Fluke

Image for post
Image for post
Photo by Jorge Jesus from Pexels

I’ll never forget a walk I took back to my car from an immunology class I took in university. The parking lot was down this long sloping pathway down to what was affectionately known as the pit. Along the way, you would be walking with a variety of other students who happen to be done for the day, and every once in a while you’d encounter a particularly excitable group. On this particular day, I remember one of the guys loudly expressing his distaste for variables. I believe they had just left the linear algebra class in a nearby building, and he was livid about variables, and how it made no sense that X means this, or maybe that.

Variables are Not Scary

If you are working on how to be comfortable with variables, my story should let you know, you’re not alone. The substitution of x, y, or maybe apple into what is otherwise simple math can make everything seem so much more complicated. If you’ve been struggling with variables, I’m here today to try to help you become acclimated to seeing them in everyday use.

Variables are part of advanced math, and programming because without variables you would not be able to create a dynamic program. Take for example the simple equation for the y-intercept:

y = mx + b

You see here a formula that has nothing but variables. Take a closer look though. X and Y are both variables, but they represent positions on a graph, or the X and Y coordinate respectively. As a result, even though they are variables, they represent something real. They represent a real point on the graph. If we want to know the Y coordinate at the y-axis, then we know that X is equal to zero. The formula then becomes:

y = b

Therefore, the Y coordinate is equal to b! Well, this is probably where you’d ask well what the hell is b? You wouldn’t be wrong to wonder this, but then this brings us to the reason variables exist in the first place. If b was replaced with 2. We could solve for x or y for any point on the line using this formula. This line, however, would never change, and the formula will always represent the same line. The same goes for programming, you want your program to be responsive to the user, but if you don’t use any variables, your programs will never be able to respond to the changes in the environment in which it’s running.

Make Sense of Variables

Perhaps you’ve been scared away from variables because you haven’t really considered any real-life examples. Sure the X and Y coordinates of a line are a real-life example, but outside the world of math or maybe maps, what does that really even mean?

I propose a few new examples to try to help you see variables in a new light. Dinner. It’s a meal that you most likely have every single day. It’s dynamic. The meal likely changes day after day and is an excellent example of a variable. Every single day you may choose a new meal, and that meal is stored in the variable for your dinner for that day.

As an added bonus, a constant for any given day could be the medication you’re required to take. Different from a variable in that it will be the same day after day, rather than changing depending on whether you want Mexican food tonight!

To Be Comfortable with Variables, Name them Well

Consider the following code (written in JavaScript):

Image for post
Image for post

Without the variable definitions, you may have a difficult time seeing what this code is attempting to accomplish. It may help to have the declaration lines for someone familiar with how programming works, but for the uninitiated, this can be off-putting. However, take a look at the same code with descriptive variable names. I’ll include the declaration lines below, but even without them, you’ll be able to read the code much more clearly.

Image for post
Image for post

So now you can see the code is searching the cupboard, and all the items in it, for the Lego. This is an overly simplified example, but I feel like it demonstrates my point quite well. If you’re descriptive in your variables, and you learn to be comfortable with the dynamic nature of programming, variables aren’t scary, but actually one of the most fascinating building blocks of this amazing field.

Originally published at https://patrickfluke.com on February 12, 2020.

Written by

Freelance Writer and Programmer. https://PatrickFluke.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store