Demystifying Programming Series (Part 2): Let’s talk about data

June 29, 2020
Demystifying Programming Series (Part 2): Let’s talk about data

Demystifying Programming Series: Part I

Data is an essential part of programming. As it was mentioned in part I of the series, programs are written to communicate with computers. Therefore, programs manipulate data. To illustrate the importance and the use of data in programs, consider a program as a conversation you’re having with a computer. In your everyday life, in order to have a conversation, for example between you and a friend, there needs to be an exchange of data between the conversers. No matter what you’re talking about, whether it’s about something that happened earlier in the day, what you’re working on at work or events that happened earlier in the week, there is content shared in the conversation or there would be no point of having it. You can apply the same concept to a program. A program is a way to share information to a computer. Therefore, data is an essential part of programming and there are many ways to manipulate it. In this post, you can expect to learn about different types of data and some ways to an manipulate it within programs.

Before diving in the post, I want to mention that different programming languages have different types of data and diverse ways of handling it. What I am presenting in this post are the most commonly seen types and ways to handle it.

Simple types of data

Something that has stuck with me from my early days of mathematics is how you can’t add apples and oranges, you have to add apples with apples and oranges with oranges. This is the first time I realized what types were and why they matter. In programming, you have to classify your data in order to let the computer know how it should use it. For example, you wouldn’t use a fork to eat soup, you would use a spoon. I think of the different programming data types as 3 categories: Boolean types, number types and text types.

A data of type Boolean can only hold one of two values: true or false. These types are useful for conditional statements, which I’ll cover later in the series. An example where a Boolean type would be used is to determine if someone is a girl. The way to express a it is as follows:

bool female? = true;

The number type has different subclasses, where the most popular ones are integer, float and double. Integers represent natural numbers, which, for all the ones who forgot what those are from high school maths, represent all positive and negative numbers that don’t have decimals such as 1, -10, 450, 182, etc. A float is used to represent positive and negative decimal numbers such as 1.5, -3.7, -10.896, etc. Finally, a double is simply a float that can store much more precision.

int integerNumber = 5;
float floatNumber = 5.16;
double doubleNumer = 5.9999999999999;

As you can see in the code example above, we usually first define the type of the variable, then the variable name on the left-hand side of the equal sign and the variable’s value on the right hand side.

The text types also have subclasses: strings and chars. Strings are words, sentences, paragraphs, etc. you’d like to store in a variable. They are usually denoted by double quotations marks “ ”. For example, a variable of type string could be “hello” or “My name is Maxine and I am 22 years old.”. Chars stand for characters. ‘a’, ‘.’, ‘*’, ‘T’ are all characters. Chars are usually denoted with single quotes.

string thisIsAString = "Hi I'm Maxine";
char thisIsAChar = 'a';

Personalized Data Types

In order to organize the data in your program, it is possible to create personalized data structures, which can be classes or objects. I’ll focus on objects in this post. For example, consider a person’s profile on a new type of social media. Let’s say each profile holds the person’s name, age, occupation and if they have a driver’s licence. You can think of object as a profiles template and you’ll create instances of that object for each user. The instance of the object will hold the data. We use the data types explained earlier to create this object. The user’s name can be represented as a string, his age as an integer, his occupation as a string again and if he have a driver’s licence as a Boolean.
Here’s what the object would look like in code:

type Person = {
    name: string,
    age: int,
    occupation: string,
    licence?: boolean

Here’s what an instance of this object would look like:

Person Maxine = {
    name: "Maxine Mheir",
    age: 22,
    occupation: "Student",
    licence?: true

These personalized data types are useful since it is possible to embedded them into each other. For example, let’s construct a new object which we could use to define a car. This object will have an entry for the car’s color, the distance travelled, the model, and if it handles Apple Car Play. Now, since we can embed objects into each other, it is possible to add an entry to the person object we constructed earlier for the type of car the person is driving. The type of that entry would be this new car object we have just defined.
Here’s what the car object would look like:

type Car = {
    color: string,
    distance: int,
    model: string,
    AppleCarPlay?: boolean

Here’s what the person object will now look like:

type Person = {
    name: string,
    age: int,
    occupation: string,
    licence?: boolean,
    car: Car

Managing Data

We now know different types of data, but how do we manage this data within our code? The truth is, there are a LOT of different ways to manage it. In order to organize data within a program, developers use what is called data structures. Data structures facilitate storing, organizing, retrieving and manipulating data in a program. There are multiple different data structures that can be used in programs and the selection of which one to use depends on what is trying to be done in the program. Consider data structures as file cabinets: there are different types of file cabinets which allow you to organize, store and retrieve your files in different ways. Therefore, the type of cabinet you use depends on the type of files stored in them and the context in which they will be used.

Just to give an example of what a data structure is like, I want to explain the basics of one of the most important one: the array. It stores a collection of data in a specific order. Let’s use the file cabinet example to understand arrays. If you insert a file in the first spot in the cabinet, then you can’t insert another file at that spot. When all the spots are filled, the cabinet cannot hold more files. When you want to access a specific file, you need to locate its position within cabinet in order to retrieve it. The same concept applies for an array: you can’t insert two elements at the same position within it, it usually has a fixed size and you can access your data based on its location within the array. Here’s what an array of int looks like:

array = [1,2,3]

If I want to access the element ‘2’ in the array, you need to retrieve the element at the 2nd position in the array. You can add an element at any position in the array, but if there already is an element at that position, all the elements in the array will be pushed. For example, if we were to add the element “5” at the 2nd position in the array, the “2” and “3” elements will now be pushed by 1 position and will now be at the 3rd and 4th position respectively in the array. We would get the following array:

array = [1,5,2,3]

Hopefully this post thought you about data types and data management and made you want to learn more about them!