HOME
Preface A Model A Medium A History Variables Functions Control Loops Classes
Source Overview

Class II

“Computer programs are good, they say, for particular purposes, but they aren’t flexible. Neither is a violin, or a typewriter, until you learn how to use it.”

Marvin Minsky. Why Programming Is a Good Medium for
Expressing Poorly-Understood and Sloppily-Formulated Ideas.



One Class, Many Objects

Classes help us organise complex ideas, making it easier to maintain our code and giving us greater flexibility in terms of what we can achieve in this medium. When using classes, we talk about creating an object. You can consider classes as a blueprint if you like and an object is one example or instance of that blueprint. What is important to grasp is that each object that we create is a unique entity of that class.

Objects are characterised by three essential properties: state, identity and behaviour. The state of an object is a given value determined by the data fields of that class. The identity of an object distinguishes one object from another. It is useful to think of an object’s identity as a place where its value is stored in memory. The behaviour of an object is the effect of the class methods and hence the operations performed on the data.

Again, these principles echo back to what I mentioned at the beginning of this course, that a computer program is essentially made up of data structures and control structures. Data stores symbolic values whilst control structures deal with processing that data. With classes, we now have the means to define our own data types and furthermore encapsulate both data structures and control structures all in one neat entity.

The world of object-oriented programming or OOP for short, is really the next level up when it comes to programming with JavaScript or Processing. There are many advanced concepts linked with OOP and I’ve noted a few references below for those who may wish to delve into the more profound areas. That said, I rarely teach classes beyond its basic syntax and principle of encapsulation. I don’t expect either for my students to necessarily dive too deep into OOP. Being able to use classes from a third party library is already a big step for most. If you have understood how to use a simple class, then that’s great.

Let's take our last example for a bouncing ball and create another instance of this class. We are going to add another method to our class too that will give it a different behaviour. The idea is to understand how we can create numerous objects of the same class and attribute different behaviours depending on the methods we call upon them. From here, it should be clear that this way of programming gives a certain level of organisation and flexibility. We have a single entity to which we can add further methods and data fields. The class therefore becomes a modular structure that we can eventually build on.


Eg 1. JavaScript
let ball_01;
let ball_02;
...
// notice we have added arguments to our constructor
ball_01 = new Ball(200, 180);
ball_02 = new Ball(450, 180);
...
// notice we can also have arguments for our methods
// this enables us to pass in different values.
ball_01. oscillateY(0.035, 90);
ball_01. oscillateX(0.095, 60);
...
ball_02. oscillateDia(0.05, 135);
...

You can execute this sketch with P5js's online editor

OUTPUT

Not all the code is apparent above, so please check out the source and explore this sketch. To summarise, we have two objects declared and we are calling different methods on each as well as passing in different values as arguments for these methods. We have already seen this concept of passing in data with functions. This is exactly the same thing. Methods are after all just functions that belong to a class. Objects are dynamic, meaning we can make changes to them at run time. Notice what happens when you press the 'm' key on your keyboard. If you look closely at the source code, you'll realise we are changing method calls on our objects.

To drive home the concept of objects and the flexibility of classes, let us look at an example that creates many objects. To do this we are going to create an array or list of objects.


Eg 1. JavaScript
let theBalls = []; // an array of objects
...
// to instantiate our array of objects, we use a FOR loop
   for(var i=0; i<200; i++){
     theBalls[i]=new Ball();
} ...


// and to call our methods on our objects, we also use a loop
   for(var i=0; i<200; i++){
     theBalls[i].display();
     theBalls[i].move();
     theBalls[i].check();
} ...


OUTPUT

This wraps up this chapter and it is also where I end this concise introduction to programming. My main aim with this website and brief course has been to explain the five core concepts: variables, functions, control, loops and classes. I wanted to give some insight into the mechanisms and structures that underlie most modern programming languages, helping the beginner to understand these in a clear manner without going into too much detail, nor diverging into the formal capacities of code. I hope I have managed to do that. If you are a reader from afar who has stumbled upon this course (which I wrote to accompany my teaching course), and you have any suggestions or would like to point out mistakes or confusing passages, please do not hesitate to contact me. I'd be most appreciative of feedback.

From here, the student is encouraged to pursue their exploration of this medium with further readings and personal reasearch in the many artistic projects that exist out there. You are also encouraged to go beyond the context of code as just purely a creative medium. Code, in its wider sense, is a culturally important media and medium shaping many aspects of our current landscape. It is quickly shaping not only form but also ideas and concepts, thoughts and behaviours. The scope of code is far reaching and fundamentally an essential part of contemporary society. Learning to read and write it has already become a vital part of education and it will continue to enrich our understanding. Code is language and I don't know of any aspect of our life that isn't concerned with language. It's all around and within us. Embrace it.


Notes