Programming Paradigms: Object-Oriented vs. Procedural
Comparing the two

A programming paradigm is simply a way of writing a program. I tend to think of a programming paradigm as part of a programmer's unique style, as it is rare for two programmers to produce the same code—even if the code is built to serve the same purpose.
When starting out their journey in object-oriented programming, most beginners start with a procedural programming approach. Procedural programming is very straightforward, processing actions one step at a time.
# Procedural Programming: Code is organized by sequence, going one step at a time for execution.a = 1
b = a
a += bputs a
puts b
Below is a quick program I wrote in Ruby for a procedural programming approach of the game Tic-Tac-Toe. There is very little structure, with the program begin executed step by step, line by line. The program depends heavily on local variables, with one looping structure encapsulating all of the gameplay. This is what I call chunky code, meaning this code is conglomerated into one giant chunk of dependencies (local variables, primarily).
While the code runs and you can play a Tic-Tac-Toe game with it, the actual code is very difficult to read and therefore difficult to maintain. There is a much more organized way to structure this code, and that is with an object-oriented programming paradigm.
Taking an object-oriented approach to a program such as this refers to the ability to group functionality in classes and objects:
Classes/Nouns of Program: Board, Player, Computer, Square, Game
Verbs/Behavior of Program: Mark Square, Display Board, Alternate players
Taking these functionalities, I can group classes and methods with the Nouns and Verbs extracted from the problem description. An initial setup (‘spike’) of the program using an object-oriented paradigm would look like this:
This already looks much more structured and legible compared to the procedural approach (my programming ability in OOP is better compared to my procedural-based programming).
With an object-oriented approach, I can begin to separate or encapsulate behavior into single units of data. I can also explicitly define which methods are available on the class’s public interface and ready to be accessed by any class or object that is in need of that data.
The code above is a completed example of an object-oriented Tic-Tac-Toe game. This code is much easier to read and its functionality is clearly defined in specific units. Data that does not need to be exposed to the public is encapsulated within classes and behaviors are associated with a specific type ( User
, Player
, vs. Computer
), with inheritance to specialize a base class. User
specializes the Player
class. Computer
also specializes the Player
base class.
As programs grow more complex, the need for less dependency, more flexibility, and more legibility increases. Object-oriented programming is one of many different programming paradigms that allow for such benefits. Object-oriented programming incorporates structure to the otherwise unstructured and dependency-heavy form of procedural-based approaches.
Summary
This piece was written to accomplish the following:
- Articulate the benefits behind using object-oriented programming as opposed to a procedural programming approach.
- Give code examples of an object-oriented approach to solving coding problems.
- Define what a programming paradigm is.
- Answer this question: What makes good code?
- Explain why object-oriented programming is used.