OOP vs FP

Edward Kim bio photo By Edward Kim

There appears to be a startling amount of debate on the effectiveness of Object Oriented Programming vs Functional Programming. But the fact of the matter is that both methods allow a person to build a program. It all depends on the programmer's propensity that really determine whether one programming is "better" for an individual than the other. However, I believe if one wants to be a skilled programmer, one needs to know both and manipulate each tool accordingly (that's my own opinion).

Functional programming is a style for building the structure and elements of computer programs that treats computation as the evaluation of mathematical functions and avoids state (contents of memory location in computer) and mutable (changeable) data. Functional programming emphasizes functions that produce results that depends only on their inputs and not on the program state. This means that in functional code the output generally depends on what you input into the code and not on other stored variables.

A key aspect of functional programming is that it tends to avoid what are known as "side effects". A side effect is an operation that modifies the state of the computer or interacts with the outside world (people, other computers, etc...). By attempting to eliminate side effects from the process, functional programming provides referential transparency, which makes it easier to verify, optimize, and parallelize programs, and easier to write automated tools to perform the tasks.

An expression is considered referentially transparent if it can be replaced with its value without changing the behavior of the program. For example 5x5 can be replaced by 25 which makes it referentially transparent (generally any functions in the mathematical sense are functions). However, assignments such as x = x + 1 are not transparent because the values change according to the input. By sticking to these concepts, functional programming tends to be more compact and have less moving parts that could potentially cause errors.

Object-Oriented Programming tends to be found on the opposite end of the spectrum in comparison to functional programming. Where functional programming likes to maintain the integrity of the value of a variable, OOP can manipulate the mutable object as many times as necessary. OOP is very dynamic and there is a great deal of side effect when creating code under this style, since we want to interact with the code. Expressions can be substituted and they can either be valid only at a certain point in the execution of the program or they can be made globally to reach throughout every part of a program. The defining and ordering of these expressions into a design the programmer likes is the foundation for OOP. OOP attempts to make programs easier to follow intuitively, easier to clean, and easier to adjust.

It depends on the skill and creativity of the programmer to really bring out the strengths of each programming style. Each has it's own flaws and advantages and each can be used in conjunction with the other. There is not one perfect style/language/etc... because there is such a variety of people in the world. It's your choice.