From Course to Real Life: Variables
If you took a programming course in practically any language, you are already familiar with the content of the image above. This is a variable declaration, where the value 1 is assigned to a; therefore, a can be used throughout the code whenever we want to use its content. But if you are new in programming, you are probably using it wrong and it is costing you time debugging. In this article we will see why and how you can use variables like a pro.
When learning variables, our first thought is that we want a value to:
- reuse whenever needed
- track and manage its changes
However, when you start using it more and more, a few considerations arise, and those considerations are what differentiate the amateur from the professional. To highlight those topics, let’s look at the following code:
The code declares 4 number variables: a, b and c, and has a sum of them at the end.
Just by looking at those lines, can you tell the meaning of it? Considering that there are a lot of ways that those variables can make sense, it is hard for the developer to understand at first glance what it’s all about. There arises the first consideration experienced developers take when creating variables: the name.
Create meaningful names is the most important thing you need to consider when creating a variable besides it’s value and type. When great names are selected, you spend less time trying to understand what is wrong with your code that is not running as intended. It is more clear when looking in context:
At the top, variables a and b are being calculated but not much context is given. However, at the bottom, there are 3 variable names: seconds_per_minute, seconds_per_hour and time_of_arrival, which is equal to 2 hours, 20 minutes and 30 seconds. It is easy to decide which one is more concise and easier to understand.
Some thoughts may come to your mind like “This is too much to write” and “The names are huge!”. You are surely right, but the tradeoff between writing too much and readability of the code is worth it in the daily programming. To make things less painful, you can use modern code editors that autocomplete the names for you, like Visual Studio Code or any IDE of your choice.
The next way to use variables that are not always clear is to use them even when you are not reusing its value. Why is that? Let me show you.
In the code above we have a consumer price being the multiplication between the keyboard price and 1.12.
What is 1.12? If you don’t know, that thing is called a “magic number”, it is a number that no one told what it means in the code so it is unknown even to the most knowledgeable persons in the world. If you write code like this, it is a call to change this behavior. The next image is showing you the right way to do it.
Now, declaring 1.12 as tax, it “magically” makes sense. You should use this approach even when you don’t reuse this piece of code, because it is crucial for who develops the code to understand what were the initial meaning of it.
The last consideration is in conflict with the title of this article: sometimes you should not use variables at all. Instead, you may consider using constants. Let me bring the first good example of the list to the table again, the time of arrival code:
At first sight you may not see anything wrong with it, but there is a way to turn this code more reliable and safe, and it is by switching the declaration of the seconds_per_minute and seconds_per_hour from var to const. It makes both declarations now constants, and they are not modifiable by anyone in the code. This is true for all types, except objects and arrays. The modified code looks like this:
“But what about the time of arrival??”. Well, it should be modifiable, because you may display it differently depending on the transport used, and that’s why you should think better when to use constants, because they dictate how your code should behavior to the people who see it.
That said, you can summary it by saying that a professional developer thinks not just about what the code should do, but also how readable it is and how other people will understand it.
It is strange to consider it when you are a beginner, since you are sure that no one will ever see or modify it, but they will and you should practice those skills. Specially naming variables, because it is hard even to the most experienced persons in the market out there.
I hope you liked this content! If you want to see more of that, you can follow me in Medium, I would really appreciate it. Here are some of my social medias and contacts:
- Github: github.com/lucasmelodev1
- Linkedin: linkedin.com/in/lucasmelodev
- Email: contact@lucasmelo.dev