Basic Concepts of Programming Languages - Variables

Hello and welcome to the second article in this series. In the first article, we covered an overview of the concepts of programming. In this article, we're going to be looking at variables. With that being out of the way, let's get started.

What is a Variable?

In simple terms, a variable is a container where we store values in a computer program.

For a more technical definition: A Variable is a name given to a memory location on the computer, so we can store values and retrieve those values during the life of the program.

When developing programs, there are times we need to store some data just while the program is running, so we can use it at a later point in our program. When we create a variable in our program, a memory address is reserved for that variable till it is no longer needed or at the end of the program.

Rules to Naming Variables

  1. Variables must begin with a letter, underscore ( _ ) or a dollar sign ($).

  2. Variables cannot start with numbers or special characters.

  3. Variable names cannot contain spaces.

  4. You cannot use reserved words for your variables. (Reserved words in a programming language are words that inform the programming language to perform specific tasks.

  5. Variables are case sensitive, so a variable name of firstName is not the same as firstname .

Naming Conventions

Different programming languages have various variable naming conventions, but here is a list of the common ones.

  1. Pascal Case - The first letter always starts with a capital letter, if the variable contains multiple words, every first letter is capitalized. E.g. First, FirstName, SumOfIntegers.

  2. Camel Case - Here, you start the name with lowercase and if it's multiword, the next word would begin with an uppercase letter. E.g. firstName, sumOfInteger.

  3. Snake Case - Here, all the words are lowercase, but multiple words are joined with an underscore. E.g. first, first_name, sum_of_integers.

  4. Kebab Case - This is similar to the snake case, the difference being we use a hyphen ( - ) to separate words instead of an underscore ( _ ). E.g. first, first-name, sum-of-integer.

Types of Variables

Variables usually have a data type assigned to them i.e the type of data they will store. Some programming language like Java, C#, you have to explicitly specify the type of data the variable would hold, while in some like Python, Javascript and PHP, the type is dynamically gotten based on the value assigned to it. Here are some of the most used data types

  1. String (Text)

  2. Number (floats, integers)

  3. Booleans (true/false)

  4. Arrays.

  5. Objects

In upcoming articles, we will look at the different data types in-depth.

Note:

Coming up with good variable names is a very challenging aspects of programming because they’re supposed to be clear and easy to identify what they represent. For example, I’m trying to store the sum of an arithmetic operation, using ab as the variable, while that is a valid name, it doesn’t make reading my code easy, and it adds a little bit of complexity to figure out what ab stands for, but using sum as my variable name here would help me and readers of my code easily understand what the variable is about, so practice using clear names for your variables.

Conclusion

This is a foundational approach to variables in programming languages, variables are a very important aspect of learning to code, and understanding this simple concept would help you be a better programmer and also help you write cleaner code.

0
Subscribe to my newsletter

Read articles from Promise Oghenevwefe directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Promise Oghenevwefe
Promise Oghenevwefe

I am Fullstack developer passionate about teaching and learning new things. Join me as I'd share the things I have learnt and going to learn in a series of short, simple and straight forward articles.