Object oriented vs. procedural programming

A brief examination and comparison of two prominent programming paradigms.

If you have limited experience with programming and programming languages, then you no doubt would have some experience with procedural programming. In essence, procedural methods are a somewhat "flat" way of building software. The program is designed to start at the top, and finish at the bottom. There are subroutines (sometimes labelled "functions", although functional programming is something else entirely) that take and return values, but that is as complex as it gets.

Procedural programming is simple, which is why C, Pascal, or even BASIC are taught to most beginner programmers. (They are all procedural languages, although with C++, Delphi, and Visual Basic, they have all been extended into object-orientedness.)

Object Oriented Programming (or OOP) is a more structured and organised programming methodology. Data structures and code are merged to form Classes, that contain Methods and Variables. Well-defined interfaces between classes are constructed, fostering code reuse and ease of modification and extensabiltiy. (there are some great nodes that detail many of the OO design principles, that do not bear repeating here)

So which is better? Well, there are a number of advantages to each.

Procedural programming allows more direct control over the hardware it is being run upon. Assembly code, one step above the basic machine code that processors intereperet, is procedural. Procedural languages allow the programmer to write more time-efficient code, and thus are more suitable for time-critical applications. (3d games, statistics engines, etc) Procedural applications are also simpler in design. (as mentioned above)

Object Oriented code will always be less efficient than it's procedural equivalent, and indeed any program that can be written using OO languages can be written in a procedural fashion, but the massive advantage to OO design is that as the application's functionality becomes more and more complex, the programming itself becomes more and more trivial. (whereas the inverse is true for procedural programming) Additionally, because of OO's highly structured approach to design, it is an order of magnitude easier to debug OO code than it is to debug procedural code. (which can turn into a nightmare depending on your development environment, project size, and amount of black magic you're using)

I guess the bottom line is this:

  • Choose Procedural if your project is relatively small, simple, or time-critical.
  • Choose Object-Oriented if your project is large and complex, and you want to be able to reuse your code easily.

Of course, some have been known to use a combination of both within the same project. Sometimes this is a desirable, and in my opinion excusable (if not acceptable) practice.