Thursday, July 11, 2013

The perks of OOP

One of the biggest problems with procedural programming, and really with languages that are not object-oriented, is the huge amount of overhead created when working on a large scale. Indeed, object-oriented programming is almost entirely for the purpose of huge scalability.

Let's take an example...


Imagine you work for an auto parts store, and wrote your code based on the task rather than the object—printing the price of a carburetor, for example. If you have 20 different kinds and work in four countries, a slight oversimplification would be to say you would have to make 80 different prints: one for each carburetor, for each country's monetary system.

Now let's say you have 50 total tasks you want to be able to do for each carburetor. Now we're talking 4000 different functions! The kicker is if you wanted to add a new carburetor, you would have to make another 50 tasks. Yikes!

Now let's say you did this in an object-oriented way. Teach each carburetor to know its own information, and teach it to give the important information in a uniform way to one, consolidated, printing function. Suddenly, 50 functions per carburetor suddenly becomes 50 functions total, and adding a new carburetor is just a matter of changing the relevant information.

So it's no wonder object-oriented programming makes sense.

Though the above premise makes sense, its implementation is often very tricky, especially to relatively greenhorn programmers such as myself. But the obvious perk is scalability. In the next post we'll talk about one example using enums...

No comments:

Post a Comment