I can’t say I understand it all, but Alan Kay wrote an article on Quora titled, Why is FP seen as the opposite as OOP rather than an addition to it?
The “three principles of functional programming,” from this tweet:
1. Orthogonal composability
2. Maximum polymorphism
3. Maximum deferment
The “three pillars of functional programming,” from Functional and Reactive Domain Modeling:
1. Referential transparency
2. Substitution model
3. Equational reasoning
When I learned OOP I saw that it was based on several principles that everyone agrees upon. When I started learning FP (and later took two years to write Functional Programming, Simplified) I was surprised there wasn’t a single accepted definition of functional programming. I ran across the principles/pillars in the last two days and was reminded of that again.
Alex Nedelcu has a good article titled, In Defense of OOFP, in which he writes about Scala, OOP, FP, and the Scala collections classes.
Just added a few more lessons to “Hello, Scala” including tuples, Scala + Swing, and a complete little OOP example. Also started a Github repo for it.
I don’t recall hearing of the words “reify” or “reification” in my OOP years, but that may be because I studied aerospace engineering in college, not computer science. Since learning FP I often see those words, so I thought I’d try to understand their meaning.
The short answer is that the main definition seems to be:
“Taking an abstract concept and making it concrete.”
For the longer answer, I found the following definitions and examples of reification.
Every once in a while when something hits you, you really remember it; it stands out in your mind as an “Aha” moment. One of those moments for me was when I saw a particular “Model/View/Controller” (MVC) diagram, and the light bulb went on. This diagram made the MVC pattern very simple, and I’ve never forgotten it.
Scala is a great language in many ways. One great feature is that you can use it as a “Better Java” (i.e., as an OOP language), and you can also use it as a pure FP language. While some people prefer one extreme or the other (not unlike extremist Republicans and Democrats in the U.S.), I appreciate that this lets you find a “Middle Way” of using the best features of both approaches.
Table of Contents
- Defining “Functional Programming”
- A working definition of “pure function”
- Note 1: Higher-Order Functions are a great FP language feature
- Note 2: Recursion is a by-product
- Proof: Wikipedia’s FP definition
- Proof: A wonderful quote from Mary Rose Cook
- That’s great ... but why immutable values?
- See also
Note: This is an excerpt from my forthcoming book on Scala and functional programming. (No one has review this text yet. All mistakes are definitely my own.)Back to top
Defining “Functional Programming”
It’s surprisingly hard to find a consistent definition of functional programming. As just one example, some people say that functional programming (FP) is about writing pure functions — which is a good start — but then they add something else like, “The programming language must be lazy.” Really? Does a programming language really have to be lazy (non-strict) to be FP? (The correct answer is “no.”)
I share links to many definitions at the end of this lesson, but I think you can define FP with just two statements:
There seems to be a lot of OOP-bashing lately, which I’m not a fan of, but this article titled Goodbye, OOP makes decent points about the problems with inheritance, encapsulation, and polymorphism. IMHO, OOP still makes sense in certain areas, including GUIs like Java Swing and JavaFX, so I’m not ready to throw it out completely or bash it.
This article on medium.com has the flame title, Object-Oriented Programming: A Disaster Story. IMHO, a less-flamy but still accurate title would be, “OOP, FP, and the conundrum of encapsulating state.”
As someone who occasionally thinks of himself as an engineer as well as a software developer, I think you have to always look for what is the best solution to the problem, and sometimes that’s more along the lines of OOP, sometimes it’s FP, and sometimes it’s a hybrid.
For instance, when it comes to collection classes, I like the hybrid approach of the Scala collection classes where it appears that the functions are directly available on a collection instance, so I can write something like
res = people.filter(age > 30). In other languages the
filter function may not be directly available on a sequential collection class, so you have to import it and then write something like
res = filter(people, age > 30). In either case you’re doing the same thing in the end, but the Scala approach “feels” better to me, and it’s also easier to read. (This example may just be syntactic sugar, but again, it’s easier to read, and the collections’ methods are a bit of a hybrid.)
As a better example of what I’m trying to say, OOP makes a lot of sense for rich GUIs, like Java Swing. Things like a JFrame, JDialog, JList, JTable, etc., all make a lot of sense in an OOP design. For instance, you create a JTable, the data is updated, users move columns around, and so on. OOP fits this very well.