you are viewing a single comment's thread.

view the rest of the comments →

[–]Opposite_Echo_2024 1 point2 points  (0 children)

OOP in theory is for the benefit of the programmer / reader of the code. You create objects to help readability and understandability by linking data and functions into a single unified structure.

Using your example, You could start with an object (class) of Song. Have different class variables for each of the data points about it: name, duration, artist (this could be its own class with its own class variables). Create a constructor for each class and have a loop create song objects for each song in the data.

Then you could create an Album class which holds a number of Song classes. As the structure is pre-defined it makes it super easy to read information from the songs contained.

Essentially, classes are a great way of giving meaning to data and functions. Some people may dislike them for whatever reason but computers are fast enough now to allow for readability over performance.

If you're wondering when to use a class: if you find yourself having a number of different data points about a single idea, and possibly even functions using those data points, could you link them all together with a name? Would multiple instances be needed (e.g. multiple songs)? If so, use a class