Matthew Frederick. 101 Things I Learned in Architecture School.
Humans Think in Categories
So far, we have been defining and manipulating what we call primitive data types to write our programs. These primitive data types are the bread and butter of programming languages and come in-box. In strongly typed languages such as Processing, they are expressed with the keywords int, float, boolean, byte...etc. Now, there is only so far we can go with primitive types and there comes a time when we want to make our own data types, combining various data to express more complex concepts. We have in part already done this with functions. However, we are going to take this a step further and study a concept that enables us to define a structure that combines both data with functions or what we call more specifically methods*. Welcome to the world of classes and data abstraction.
In brief, a class is an ensemble of data and methods, a structure that enables us to combine compound elements and define what we call an abstract data type. That may all sound very grand but the concept is rather simple. Classes are a means for classification. In order to grasp what a class is, try to see it as a class in biology where we organise things in specific categories. For example, in the animal kingdom there are many different classes of animals each with their own specific characteristics. We know that all mammals have lungs, are warm-blooded and have hair and fur. These are some of the characteristics that all mammals share and by which we define what a mammal is. A class is therefore a means to organise things that share common features.
In the realm of programming, we have the possibility to organise concepts that share common features. Let's have a look at the basic steps for working with classes.
There are three important steps to creating a class: 1). Data fields - what data belongs to our class. 2). Methods - what operations do we apply to our data. 3). The class constructor - a means for creating objects with our class.
To create a class we need to use the keyword Class followed by a given name. As in declaring a variable, we decide upon the name of the class. Within this structure we define the data fields and the methods that belong to this class. It's always good practice to think about these two parts of our class before writing it in code. We need to understand clearly what we want our class to do. What is it's objective if you like. From here we need to define the various data and methods that will enable us to obtain that objective. Let’s have a look at a very simple example.
To begin with, let's imagine a really simple class for a bouncing ball. Our ball will be just an ellipse. Our ball will have a position, a size, and some value for simulating speed.
Now, after a little observation, you may be wondering why we need a class to do such a simple thing. Indeed, we could have programmed this without using a class and that would have been fine if all we needed were one or two bouncing balls. However, if you had to create a hundred, the task of programming this becomes a little harder. Use a FOR loop I hear you say. Well, that is a viable answer and solution but imagine you want to have control over each of these balls, give each a specific colour, size or behaviour. Perhaps one will move left and the other right. Calling a simple function within a FOR loop does not give us the same flexibility as a class. Let's have a closer look at what we are doing when we make use of our class.
We first declare what is called an object, theBall. We decide upon the name of the object. Secondly, we instantiate this object - we create an instance of our class. Finally we call our methods on our object. These are the three main steps in using our class. The class is a blueprint of an abstract data structure. An object is an instance of that class.
The above described paradigm is what we call object-oriented programming and it is precisely this concept that enables us to create dynamic data types of our own conception and for which we have considerable control. Object-oriented programming is an essential part of taking programming in Processing & P5js to the next level. It is from here we can develop far more complex, flexible and better organised systems. The concept is equally important for understanding and using third party libraries within these two frameworks and for which their are a plethora of possibilities. Libraries add functionality to these frameworks, for example we have libraries dedicated to 3D geometry, sound, computer vision, animation ...etc. What is important to understand is that each library is essentially a collection of classes. Being able to harness their functionality depends on understanding classes and the creation of objects.
In the next chapter we will look at harnessing a little of that OOP power, creating numerous objects to create a fanfare of colourful bouncing balls.