Skip navigation

I figured that perhaps if I have a specific topic to blog about I might be able to sustain my interest in writing. When your subject matter is Everything it can be hard to determine exactly what you should post about.

Which is why I’m planning on writing a series of basic concepts of programming posts aimed at people just getting into coding. I chose to stay on the conceptual side to try and stay language agnostic. This is because I am hoping to help people in my programming class (not specifically, just people at that level), and my current class’ language of choice is VB.net, of which I am not really a fan. So my examples will all be in pseudocode.

But enough waffling, lets get into it.

What is a variable? A variable is something you can declare in your program to hold something. A variable has three things: a name,  a value, and a type.

What’s in a name?

A variables name is what you use to access it’s value. Say your program needs to store the current temperature. You might create a variable called ‘currentTemperature’. What you actually call a variable is up to you, although it is good practise to call it something that represents it’s value. You could have called it ‘sheep’, or ‘kitteh’, or ‘flimFlamManitoba’, but once you have a number of variables, your program could get incredibly confusing.

Also, giving it a short name, such as ‘cT’ or even just ‘t’ might seem like a good idea at the time, and may work just fine if your program is fairly small, and you are the only one working on it, but if someone else has to maintain your code or even if you have to revisit your code some time in the future, it will be a right royal pain in the arse to figure out what t, or q, or z represent.

One final note, you may have noticed the capitalisation I used. Again, it’s up to you, but I personally like to name multi-word variables in what is called Camel Case. This is where you leave the first word lowercase, and the words after that capitalised, for example ‘camelCase’, or ‘slumDogMillionaire’. I won’t go into why the first word is not capitalised, and when it is recommended to be uppercase, because it is beyond the scope of this article. I will cover this later when I talk about Object Oriented Programming.

Variable values:

A variables ‘Value’ is the important bit. To set the value of a variable is to assign it a value. That value gets stored somewhere in memory, and you don’t have to care where because all you need is the name of your variable. Say you are writing a program that gets the current temperature from a temperature sensor attached to your computer. You would declare a variable, perhaps called ‘currentTemperature’, and you can assign the value you got from the sensor, which is for example 22, to your variable. Fairly simple stuff. I’ll give you an example in pseudocode:

declare currentTemperature

currentTemperature = 22

You can also assign a value to a variable from another variable. Let’s say that we now want to move currentTemperature to a variable called oldTemperature (Which, for the sake of this example we have already declared).

oldTemperature = currentTemperature

Really pretty logical. However, there is one final hurdle to variables that can be the trickiest to get ones head around, and that is:

Types:

A type, when talking about variables, is talking about the kind of data you can put into a variable.

There are a number of types of data. 22 is example of  an Integer. Put simply, an integer is characterised by a number without any decimal points.

There are also floating point numbers, or floats for short, that do have a decimal point. For example, 22.93 is a float. 22 could also be a float. All integers can be floats, but not the other way around unless you either round it to the nearest integer or strip of everything after the decimal point.

A string is a series of values that normally represent text. For example, “Hello world” is a string. Note: Strings are usually shown in double or single quotes to show where the string begins and ends.

Why does this matter? When it comes to computer languages, they can be split into two (for the most part) categories in regards to type: Statically typed and dynamically typed.

If you look at the meaning of the word ‘dynamic’, you will see that it means something that is undergoing change or motion. This means for our variable, currentTemperature that the type of value we put in there could me a string, a float or anything really.

Statically typed languages on the other hand, require you to state what kind of data you put into a variable when you declare it, and stick to it. For example, with our previous example, currentTemperature might be declared as follows:

declare currentTemperature as Integer

Why should we have to manage types ourselves? A few reasons. For the computer, an integer is stored in memory in a different way to a string, or a float. Another reason is that different variables take up more memory than others, and for greater efficiency, you should pick the type that is sufficient for any values you expect to put in it, but no bigger. A few bytes here or there is usually not of any importance in simple programs, but in very complex programs, it can have a huge impact on how much memory your program requires.

So what if you need to change from one type to another if you use a statically typed language? In a dynamically typed language, if you need to convert, or cast from one type to another, the computer language handles all of that for you in the background without you having to worry about it. In a statically typed language, you need to manually convert these values, usually using built in functions that the language provides.

After that, it sounds like you would never want to use a statically typed language unless you were a masochist. It’s true, statically typed languages can be a pain. The programmer has to do a bit more work with static variables as opposed to dynamic variables. However, there is a catch. Even though they are generally easier to work with, and often allow faster development, dynamically typed languages give you enough rope to shoot yourself in the foot with, so to speak.

Well, I’ve covered many more topics here than I wanted to, in greater detail than I would have preferred, but hey, shit happens. Tune in next time for the next semi-exciting instalment in this series, which will be based on a topic I have yet to decide on.


Leave a comment