Thursday, December 26, 2013

RHT: 4 Benefits of Object-Oriented Programming

by Robert Half Technology
December 26, 2013

Even though object-oriented programming has been around a while, it has some advantages over other types of coding.

Like parachute pants and Pac-Man, object-oriented programming became popular in the 1980s.

It’s still relevant, of course, and what makes the object-oriented approach so valuable is the way it simplifies problem-solving.

Traditional structured programming breaks down large problems into sub-problems, addressing them in separate units of code. Functional programming treats elements of code as precise mathematical functions, then prevents them from affecting other elements (i.e., no side effects). Object-oriented programming relies on encapsulation to address problems in self-contained objects, giving it four distinct advantages over other coding methods.

1. Modularity for Easier Troubleshooting

Something has gone wrong, and you have no idea where to look. Is the problem in the Widget file, or is it the WhaleFlumper? Will you have to tromp through that “sewage.c” file?

With object-oriented programming, you know exactly where to look. “Oh, the car broke down? The problem must be in the Car class!” You don’t have to look in the WhaleFlumper class or muck around in the Sewer class.

That’s the beauty of encapsulation. Each object is self-contained, and each bit of functionality does its own thing, leaving the other bits alone. Also efficient, this modality allows your team to work on multiple objects simultaneously and minimizes the chance you might duplicate someone else’s functionality.
2. Reuse of Code Through Inheritance

Suppose that in addition to your Car object, one colleague needs a RaceCar object, and another needs a Limousine. You build your objects separately, but discover several commonalities between a RaceCar and Limousine. In fact, each is really just a different kind of Car. This is where the inheritance technique saves time: Make your Car class more generic, then define the RaceCar and Limousine classes to inherit Car traits.

Of course, Limousine and RaceCar can still have unique functions. If RaceCar needs a method to “fireAfterBurners” and Limousine requires a Chauffeur, each class can implement separate functions just for itself. However, because both classes inherit key aspects from the Car class, for example the “drive” or “fillUpGas” methods, your inheriting class (or “subclass”) can simply reuse existing code instead of writing these functions all over again.
3. Flexibility Through Polymorphism

Now, you just need a few drivers, or functions, like “driveCar,” driveRaceCar” and “DriveLimousine.” RaceCarDrivers share some traits with LimousineDrivers, but other things like RaceHelmets and MountainDewSponsorships are unique.

This is where object-oriented programming’s sweet polymorphism comes into play. Because a single function can shape-shift, Mystique-style, to adapt to whichever class it’s in, create one function in the parent Car class called “drive.” Not “driveCar” or “driveRaceCar” — just “drive.” This one function will work with the RaceCarDriver, LimousineDriver, etc. Heck, you can even do “” or “”
4. Effective Problem Solving

Object-oriented programming is often the most natural and pragmatic approach, and it’s able to solve typical programming problems other methods can’t handle.

For example, structured programming tackles simple, straightforward issues. Unfortunately, that’s not how the real world works. </understatement!>. Meanwhile, writing a functional-style program in a language like Haskell or ML can be a chore, and chances are the latest social channel hasn’t implemented a library in Coq yet. In these cases, it’s object-oriented programming to the rescue.

This isn’t to say that object-oriented programming is the One True Way, but if you need to solve complex programming challenges and add new tools to your utility belt, it’s a logical approach.

What are your thoughts about object-oriented programming? Let us know by leaving a comment below.

No comments:

Post a Comment