Advantages of using classes and other ways to do things

I have never used classes until now. And i dont really understand how they work.

I feel like it has made my code much more complex than it is rather than neaten stuff up.
Why do people use the class function?
If i want to remove it from my code, what can i replace it with?
I am new to programming

Hi Bobby! I think that object-oriented programming is not very difficult to understand as a general concept but it usually needs some time.

Personally, i think that the main benefit of using classes and objects is that those chunks of code can be reused in other programs and they have their variables and methods. So, as a general rule of thumb, if you are writing something that you’ll probably use again in the future, maybe is a good idea to write it as a class/object, otherwise just write functions.

I hope this is somewhat clear

2 Likes

Trust me, you want to use classes in the long run, even if you don’t re-use them in other sketches.

Main advantage is that you have a package of variables and functions that describe one object (eg. a ball), and all are in one place.
This is much better than to have these variables declared globally because they have a defined scope then. It’s also better to have the functions stored in one class than to have them just in the code one after another.

You can have different classes, like one class for balls and one for paddles.

Simplicity

When you have an Array or an ArrayList of the class, the structure in the class is still very simple, because we only look at one ball and not at 100 balls. And let’s say you have a for-loop in draw(), so this for-loop is also simple: for(Ball b1 : balls) b1.run();.

Remark

Did you read the tutorial on objects on the website?

https://www.processing.org/tutorials/objects/

1 Like

Anyway to re-organize a class into simple code, you need to make the variables global

When you have many objects from that class, you need to work with parallel arrays (another good reason for object oriented programming)

Then make the functions global also

  1. From several variables to arrays - Processing 2.x and 3.x Forum
  2. From several arrays to classes - Processing 2.x and 3.x Forum
1 Like

I too initially resisted the use of class objects but soon realized if I want to really be able to code my ideas that I needed to learn it.

It did seem a little confusing at first but with each project it becomes more comfortable to use. And as others have noted above, especially when you want multiple instances of the same object in your sketch.

**Dan Shiffman’**s book Learning Processing is very good at explaining OOP along with his tutorials on YouTube. Also on YouTube, tutorials by Abe Pazos are quite good for beginnners.

1 Like

Just want to mention that classes are good thing to know, a great way to start if code will grow, important to understand for using libraries (and the Processing API itself, like PVector and PShape)… however, classes are not always the best solution, just an important option. Sometimes you can keep it simple.

A great example is Processing itself – it has ways to draw two rectangles through a function with primitive data, or as objects. You can approach a sketch that draws multiple rectangles by storing the geometry in float[][] rects, or by creating an ArrayList of shapes made with createShape(RECT…) – or an ArrayList with custom rect objects. Which approach to use depends on what you are trying to accomplish.

1 Like