Object-Oriented vs. Functional Programming:
Is it Really a Competition?

August 21, 2014

A common misconception is that Object-Oriented Programming (OOP) and Functional (Procedural) Programming are opposing concepts. In reality, they are not. For example, while Ruby is an object-oriented language, it can function just as well in a functional manner.

What is Functional Programming?

Functional programming is a style of programming which models computations as the evaluation of expressions. Or, as Jonathon Morgan puts it, "functional programming is the mustachioed hipster of programming paradigms." Procedural languages know about small, fixed sets of different kinds of data (e.g., strings, numbers, arrays, etc.), known as data types. Data in functional programs should be immutable, meaning that it should never change. The language contains built-in operations (methods) that do things to the data types (e.g., concatenate strings, do math with integers, etc.). Variables made by the programmer hold data types. Every possible data type and operation is already in existence; built into the sytax of the language. Since the programmer creates the variables, she knows which methods she can use to do things to the data types. Functional programs should be stateless. They should perform every task as if for the first time, with no knowledge of what may or may not have happened earlier in the program's execution.

What is Object-Oriented Programming?

Everything in OOP is grouped as "objects." OOP is implemented by sending messages to objects. The first step in OOP is to identify all the objects a programmer wants to manipulate and how they relate to each other. Once an object has been identified, it is generalized as a class of objects. The class defines the kind of data it contains (attributes/variables) and any methods used to manipulate it. A class provides a kind of blueprint for the construction of similar objects. A data class can define subclasses of data objects that share (inherit) some or all of the main class characteristics (e.g., a Dog class could be a subclass of an Animal class). Since a class defines only the data it needs to be concerned with, when an instance of that class (an object) is run, the code will not be able to accidentally access other program data, thus increasing security. Unlike in functional programming, the concept of data classes allows a programmer to create any new data type that is not already defined in the language itself. Object-oriented programs are, therefore, open-ended.

So when do you use functional programming, and when do you use OOP?

When you anticipate a different kind of program evolution: Object-oriented languages are good when you have a fixed set of operations on things (objects) and, as your code evolves, you primarily add new objects. This can be accomplished by adding new classes which implement existing methods, and the existing classes are left alone. Functional languages are good when you have a fixed set of objects, and as your code evolves, you primarily add new operations on existing objects. This can be accomplished by adding new methods that compute with existing data types, and the existing methods are left alone.

When evolution goes the wrong way, you have problems: Adding a new operation to an object-oriented program may require editing many class definitions to add a new method. Similarly, adding a new kind of object to a functional program may require editing many function definitions to add a new case. In 1998, Phil Wadler dubbed this the "expression problem" and a widely accepted solution has yet to hit the mainstream.

When a system's performance and integrity are both critical: Functional programming is typically used when your program needs to do exactly what you expect every time and needs to operate in an environment where its tasks can be shared across hundreds or thousands of networked computers. Functional languages excel at manipulating symbolic data in tree form. Some argue that functional programming is best used for GamingAI, mathematical computations, and concurrency. However, practically everything you can do in OPP can be done in functional programming, and vice versa. It's just another way to code something; a different way to solve problems.

← Previous Post
Next Post →