Having taken some time off, I am excited to start Tech Tuesdays back up. We will continue looking at programming and the next topic are more complex data types which are also referred to as data structures. If you have been following the programming series, you may recall that we already had three separate posts dealing with data types. These introduced the concept of data types and discussed why they matter and how they are implemented. But those posts dealt with simple types of data such as a single number or character. How do we go about representing something more complicated like a chess board or a person inside of a computer program?
Let’s take the example of a person. We might want to keep track of the person’s name, age, and gender. Now with what we already know, we could write code as follows:
var personName = "Albert Wenger"; var personAge = 45; var personGender = "male";
We simply create three separate variables, each using a basic data type, to keep track of the three pieces of data. But this seems unsatisfying as we don’t have a way of referring to the person “as a whole” — so we can’t write something like
winner = fight(cook, thief);
What we need instead is a way of bringing the individual attributes together into a single unit that we can then use throughout our program. Different programming languages offer different ways of accomplishing this goal. These go by names such as structs, records and objects. We will have one or more posts on that in what will be a mini-series on data structure.
There is another expressive problem though that we need a way of addressing. Let’s say we want to keep track of a group of persons, such as a class of students. If there are 25 students in the class then it would be very cumbersome to have to say something like
var student1 = ...; var student2 = ...;var student3 = ...; ...
So even once we can refer to a person as whole, we still don’t have a way to refer to a collection of persons. The same problem exists even for simple data types — right now we don’t have a way of referring to a collection of numbers. Again programming languages offer data structures to help with this. These go by names such as arrays, lists, and sets and will be the subject of another group of posts in this data structure mini series.
We could start by looking either at the first problem — grouping multiple attributes into a single whole — or the second problem — collections of multiple things. I haven’t decided yet which makes more sense, so if anyone reading this has a suggestion as to where to start I welcome it.