Using Variables the Clean Way

Using Variables the Clean Way

Your Programming 101 with Clean Code Principles

Introduction 🚀

Hello, friends and geeks alike! Welcome to this post all about variables and the clean code tricks that go with it.

Clean Code - The What and Why

Clean Code is code that's easy to understand and easy to change. You can get to that goal by following some standards and tricks.

Imagine this: You have a business with this amazing product idea. Out of the fear of someone coming up with the same idea, you decide to rush your code and release it out into the world. After a bit, your product becomes successful. This success leads to your userbase is asking for more features and bug fixes, which sounds great! But oh no, now it has been a couple of months of you not touching your code. The problem is you don't know what everything does anymore. Not only that but, there's terrible naming, and the project structure is messy. All these problems together end up in your code breaking down with the smallest change.

Now, my friend you're what we call 'screwed'. Your userbase will not be happy that you take so much time to add new features or the lack of solving errors. They will get frustrated and abandon your product causing you to go bankrupt. That's why learning clean code from the get-go is so important.

About This Course

This course is a programming 101 course like what you would find at university but with a twist! My clean code tricks will not only help you make your code more readable, but you can show off your clean code proudly to friends or colleagues. This course will consist of examples in Python (and sometimes C# if needed).

Do you already feel comfortable with using variables? In that case, I would recommend skipping to the 'Ultimate Clean Code Tricks' section of this post.

How To Use Variables - The Basics 🔍

What Are Variables?

Variables are containers. These containers hold some type of data that you can use somewhere else in your file. Example:

name_of_person = "john"
print(name_of_person)

The variable name_of_person contains the value 'john'. In this case, by using the print() function in python we print the value 'john' to the screen because you point to name_of_person.

You always have to use quotation marks (" " <- these bad boys) for the language to know that it's text. In development terms, a text variable is called a 'string'.

Storing Numbers in Variables

You can store many things in variables. I showed you how a string works, now I'm going to show you another big one. You can store (rounded) numbers into variables. A variable that contains a round number is better known as an 'integer'.

Using integers can be interesting because you can use them for calculations. Example:

number_one = 4
number_two = 2

sum_one = number_one - number_two
sum_two = number_one + number_two

print(sum_one)
print(sum_two)

Sum_one does a subtraction on the two integers. It stores 4 - 2, which means the value of sum_one is 2. sum_two is an addition, which means when you print sum_two it will show you 6.

Things To Keep In Mind

I hope you got an idea of what variables are and what they do. Here is some stuff to keep in mind:

  • You cannot start variable names with a number

  • If you try to print the variable 'duck' but you type in 'Duck', it won't work. Variables are case-sensitive (at least in Python)

  • You can't do "john" + 6. strings and integers in calculation don't go together and will return an error

If you want to see all of what I just covered in action some more I would recommend watching 'Python Tutorial for Beginners | Variables in Python' by Telusko on YouTube.

Ultimate Clean Code Tricks 🧹

There is quite a lot of stuff to keep in mind when it comes to variables the Clean Code way but trust me, they all make sense.

1. Always check the naming convention of the programming language you're working with.

Variables get written differently in every language. Make it a habit that you always check the way variables are written if you get into a new language.

Variables are most of the time based on these three formats:

  • camelCase: The first word always starts with a lowercase letter. All words after that with an uppercase letter.

  • PascalCase: Every word starts with an uppercase letter

  • snake_case: Every single word starts with a lowercase letter. There are also underscores after every word

Python uses snake_case for its variables.

2. Make your variables meaningful and searchable

Be clear with your naming so you don't have to guess what the value is later on. Instead of calling your variable 'days'. call it 'days_until_birthday'.

Don't give variables names that consist of one single letter like variable 'a'. It's unclear. If you have a lot of files and code, and you want to search for variable 'a', it's also going to be hard to find.

3. Make your variables pronounceable

Imagine you have a variable called 'yyyymmdd' to save a date. If you want to discuss something about that variable with a colleague, you're going to sound crazy. It can also be an issue for developers with visual impairment. It's important that they can understand a variable name from a screen reader. In conclusion: make your variable pronounceable, and call it 'date_created' instead.

4. If you have very long variable names, don't make them too similar

If you have two variables called 'amount_of_files_disk_c' and 'amount_of_files_disk_d', it is going to happen that you pick the wrong variable because they're so similar. It's also is going to raise issues with searching because of the same reason. Don't do this.

The End

Congratulations, you made it to the end! 🎉 I hope you learned a thing or two and that you enjoyed it. If you have any questions or recommendations, you are more than welcome to leave a comment or contact me on Twitter or Instagram.

Be sure to follow me for my next post on clean code!