Object Oriented Programing is a programming methodology that breaks down replicatable code into classes so it can be re-used or built on top of. But why is it better than procedural programming?

Kings Castle Lego

When I was learning about object oriented programming (OOP), I stumbled on a reddit comment that explained OOP using Legos. This is exactly how it was explained.

Say you’re playing with legos and you want to build a castle with towers, knights and cannons. You can build everything using lego blocks you pick one by one and assemble them to get what you like. You could also use pre-made pieces that make building a castle easier (like window frames, the rolling gates, etc).

Object-Oriented Design is like using the pre-made Lego pieces. It means using the same tools and components for similar tasks. Your castle will have more than one tower, so why build a new tower from scratch each time? If you’re going to have a lot of knights with swords and armor, why not buy those pre-made Lego characters and slap some armor on them?

Lego characters are similar to a ruby class. The pirate, knight, and astronauts are subclasses, since they have all of the qualities that a regular character has, plus unique ones inherent to them. Each individual pirate or knight is considered an object of that subclass. If you ever wanted to use those characters for a pirate ship, or a spaceship, you can just take the swords off of ‘em and throw on a pirate hat or a helmet.

Procedural programming would be programming things in order. It does not take into account that things can be re-used. Every time a new instance of something needs to be built, it is just created all over again, from scratch. For small projects, it always looks easier (and quicker) to do things using procedural programming, however once projects grow and scale, it will be a mess.