Variables? They are Just Labels!

Introduction

I can relate to the excitement that comes with writing your first functional program (The renowned "Hello, world!" program). After that initial milestone, the journey begins as you delve into the intricacies of the programming language you have chosen to learn. A fundamental aspect of this journey is grasping the concept of variables, their significance, and how to utilize them appropriately.

In this post, I will share insights on variables, their general concept, and their transferability across different programming languages.

Demystifying Variables

Variables are essentially labels on containers, you don't have to overthink it. You may have heard about variables x and y in Maths class, and frankly, they are not that much different from what they are in programming. So whenever you think about variables, think about labels on a container.

Let me explain why variables are commonly referred to as "labels". Their purpose is to label a container that holds some piece of information, much like a jar that holds objects. The data that is stored in memory is determined by the person who creates it, and the size of the memory. To make it easier to identify what is kept in the computer's memory, it is labeled with a name that describes the information it holds, just like a jar would have a label stating its contents. For instance, a jar of seeds can contain different types of seeds, and labeling it allows you to distinguish one jar from another. Similarly, a variable's label (also known as a name) gives you a way to identify the information it contains.

Now that I have clarified why variables are just labels on containers, let's move on to what is termed Best Practices when working with variables.

Best Practices and Rules When Using Variables

Like any other useful concept, there are rules. Of course, you can label your jar anyhow you want but having a standardized method makes it easier for you and the other person to read and add more labels when needed. In the same way, the following are some best practices and rules regarding variable naming.

  1. Make them descriptive. It makes sense to name a jar that contains mustard seeds as Mustard seeds, in the same vein, your variables should be labeled according to what content is placed in them. Instead of e1, you may want to try employee_1 . Both are valid labels, but one is much more readable. Imagine having to decipher variables that are named like r, c , nma, etc. What if these were named radius, circumference, and num_of_apples. It doesn't have to be too long, just descriptive.

  2. Don't start them with a number. A lot of programming languages bar you from having a number as the first character in your variable. Don't worry, it can be used anywhere in the variable name, just not at the beginning. For example, student_1 is allowed but 1st_student is not.

  3. If possible, pick one style and stick with it. There are two prevalent writing styles: camel case and snake case. Here's how each works. Camel case starts with a lowercase letter and capitalizes the first letter of each subsequent word. This name comes from the way the back of a camel looks. On the other hand, snake case uses only lowercase letters and underscores to separate words. Example: numberOfStudents is how you might label a variable when using the camel case. For the snake case, you would do something like this number_of_students . You decide which works best for you and the language you are using.

  4. Dashes? We don't do that here. As I mentioned earlier, it is permissible to use underscores (_) in your programming. However, this does not extend to hyphens or dashes (-). Most programming languages do not allow the use of hyphens or dashes. If you want to use these characters, you might want to consider using the underscore (_) instead of hyphens. This way, you can still use separators while avoiding the restrictions that come with hyphens and dashes.

  5. Use Upper Case Letters for Global Variables. Although not mandatory, it is a widely known and accepted culture to name variables that are global with capital letters. For example, if you have a global variable that keeps the interest rate for your entire program, you may consider naming it INTEREST_RATE instead of interest_rate . This is also true for constant variables.

  6. Make them descriptive. No! It's not a typo. Your variable names must be readable and succinct. This is your way of labeling whatever is in the computer's memory for whatever data you have stored. Don't make life harder for yourself and others who will be working with your code. Good labels make finding objects easier, use them.

Final Words

I hope everything here has made it clear what variables are in a close to non-technical way. Everything mentioned here is directly applicable to most programming languages. There are other practices you will pick up as you journey through your programming adventures. Feel free to share your methods in the comments section, I would love to hear from you.

Happy Coding!