DEV Community

Teaching Functional Programming: Two Big Picture Approaches

Ed Toro on November 06, 2018

Functional Programming (FP) has been around just as long, if not longer than, Object-Oriented Programming (OOP). But it's only (relatively) recent...
Collapse
 
kspeakman profile image
Kasey Speakman • Edited

Thanks for the article. I like where it is going, and I’d love to see more.

One thing though. I do not believe FP is hard, not harder than OO. I think changing paradigms is what is hard. I’ve brought fresh devs into FP and I never had to explain SOLID or inheritance gotchas or law of Demeter or many other things which are considered normal in OO. These just aren’t things that trip up FP programmers. In training them I also never got into type theory. I just focused on pure functions. The reuse you get from some type-based things are nice discoveries along the way. However when you already know how to solve a lot of problems in one paradigm, changing to the other means having to relearn, which is hard. I think it would be equally difficult switching from lots of experience in FP to OO, but not many of those people exist to tell us.

Collapse
 
dizid profile image
Marc de Ruyter

Similar to going from SQL to no-SQL databases or changing from (PHP) to asynchrone (node.js).
An old dog can learn new tricks, but it takes alot of re-training.

Collapse
 
eddroid profile image
Ed Toro

You make a good point about the advantage of being first (First-mover advantage? First-learned? I don't know if there's a name for it.)

I get a sense from FP evangelism that, if not for some historical accident, the tables would be turned. FP would be the dominant paradigm and I would instead feel compelled to write an article about teaching OOP. But I don't know enough about the history of this debate to say one way or the other.

I'm inclined to believe that OOP dominated (and still dominates) because of something inherently more desirable about it rather than a coin toss that always landed in OOP's favor at engineering meetings over the past decades. But I admit I could be wrong.

The "mistake" I hint at in this article is that, upon first learning (or "first-learning", if I may coin a new term) FP (like I did with Scheme), to later see it ignored in the industry is disheartening to a new, job-seeking graduate. Throwing a bunch of FP juniors into the marketplace to see them struggle to find work isn't going to solve the problem, no matter how superior FP may seem to some.

Instead we need to acknowledge that OOP has "won" and try to figure out why and what that means for the future of coding education.

As a bootcamp instructor I like to start with OOP because it's more relevant to the job search, then introduce FP concepts gradually because I believe it's a good skill for the future. To do the reverse, given my local market conditions, would be a disservice to my student's tuition money. So this article represents a teaching strategy for that style of curriculum.

Collapse
 
kspeakman profile image
Kasey Speakman • Edited

Thanks for your generous response.

I tend to agree about starting learners with OOP. Outside of work, I have a youth mentee, and I am starting with imperative/OOP too. Mainly because that's really the only youth learning material available. And that state reflects the broader market for developers. However, internally at our company, I rather feel like I am making our devs all the more valuable by teaching them FP plus experience running FP in production. Because FP work tends to pay pretty well. At least, according to SO salary surveys.

I haven't tracked the history of FP either, but I feel like OO won (like most things that win in the market) because it had compelling products sooner. Smalltalk is a common cited example, and is generally considered a great experience. Interestingly, I see some parallels between Smalltalk and functional methods, which were lost in later incarnations of OO. For example, there was no if in Smalltalk. Instead, you tell the boolean object: ifTrue, run these statements. And your other objects with multiple data cases tended to be modeled this way too. Seems structurally similar to category objects and their operations. Of course, it was still imperative and it still mixed state and behavior together.

I also meant to mention. I agree that FP is generally done a disservice by people being religious about it. OO is and will continue to be a perfectly valid way to solve problems. Follow your bliss! But I like to share my FP experiences in case someone is interested. My particular journey into FP was because, despite my best efforts, I was dismayed at the long term quality of products I created. FP was one of many things I tried. FP doesn't fix the quality problem on its own, but it plays a helpful part for me. In that FP makes pure functions easier to write. And pure functions make refactoring significantly less risky.

Thread Thread
 
mkoennecke profile image
mkoennecke

I would like to chime in that the difficult bit is the change in paradigm. I started my career with Fortran 77 and I still remember learning OOP. It was hard to get my head around that and the first two OO programs I wrote were crap. Working but crap.

The next paradigm shift came with UI code being event based.

Collapse
 
sudeeprp profile image
Sudeep Prasad

Are they really two different things? Sometimes a function needs state, so we can combine/hide/abstract it in an object... Mostly we can avoid coupling it to state, so it can remain a pure function. Maybe I'm too focused on the implementation... Am I missing something here?

Collapse
 
kspeakman profile image
Kasey Speakman

Yes, the characteristics and design of one versus the other are drastically different and do not work well together. Objects lead you to create an object graph (object containing references to other objects) with mutable state distributed all throughout the graph, and that is what constitutes a program. State is shifting sand -- it is always moving underneath you due to mutations (from external method calls on the object). FP uses immutable state where possible and programs look like a tree of functions with data flowing between them. You can examine/modify an individual pure function in that tree in complete isolation -- you don't have to know or care what the rest of the program is doing because its outputs only depend on its inputs. But to get that benefit, you have to design and solve problems slightly differently than what OO programmers muscle memory will be.

Thread Thread
 
sudeeprp profile image
Sudeep Prasad

I understand what you mean. I'd been stuck with the 'smeared state in objects' rut for a while. I realized that I was just bucketing shared-state without really thinking about the decomposition-goals. I would hesitate to call my mess as object-oriented. It felt more like 'class-oriented random-partitioning'
These days I recognize object-oriented as a caching mechanism, like when UI Widgets need to keep local state to animate/re-draw themselves without a round-trip to the server (like StatefulWidget in Dart). Relationships are more like constraints. Where caching is not needed, a pure function that can be tested in isolation is the way to go!

Collapse
 
kspeakman profile image
Kasey Speakman • Edited

P.S. I love the apple example. In fact, the red delicious apple is not actually red. Rather it reflects the red spectrum of light into our eyeballs so we perceive red when looking at it. And the same goes for any item with color, plus white reflects most of the light spectrum and black absorbs most of it. I find that deeply interesting.

Related: Vantablack

Collapse
 
eddroid profile image
Ed Toro • Edited

To expand on this a bit, one point in favor of FP's model is that, in various scientific senses, the world may actually be functional.

The apple isn't necessarily red. It just reflects red when you shine wide spectrum light on it. It'll appear as a different color with other spectrums of light (or none at all). So "being red" can be modeled as a function whose outputs depend on its inputs.

If you think that's a superior model of the concept of color, you may be a functionalist. (That sounds like the beginning of a joke/meme.)

Collapse
 
antonrich profile image
Anton

It becomes like 2d. Interesting.

Collapse
 
antonrich profile image
Anton • Edited

I guess I gravitated towards FP because I've read that it will help you have less bugs, code that is easier to refactor and maintain.

Tell me, in your life, when you used a buggy software what did you think?

I thought who designed that piece of shit and kinda get emotional a little bit. But then I would go to the internet to find a solution.

So when I discovered that there is a way to write programs and have less bugs that's a green light for me.

Collapse
 
eddroid profile image
Ed Toro

I don't have an opinion on whether FP generates less bugs than OOP. I need to see an example written in each style side-by-side to decide which one I prefer.

Collapse
 
antonrich profile image
Anton • Edited

Then I guess you need an example with the same language? Because I personally cannot give you an example in the same language.

Also I won't give you an example from my experience because I'm a novice. But I've been watching some Elm courses where instructor gave some examples and said that the opposite JS code will actually end up biting you in the ass.

I also want to say that a lot of things end up biting in JS are the lack of types whereas Elm has a strong type system. You probably need an example that demonstrated exactly the difference between two styles rather than the difference between having strong type system and a week one.

Thread Thread
 
avalander profile image
Avalander

It's worth saying that those examples are hand picked to show the strengths of Elm.

I've used both, Javascript and Elm (a lot more js than Elm, to be honest) and in many cases Elm's types helped me a lot, but there have been instances where I had to fight Elm to do things that would have been straightforward in Javascript.

Collapse
 
antonrich profile image
Anton

Ed, I just a found a talk by Richard Feldman talking about null and how it propagates into the code. I'm currently watching it and I'm on minute 7.

I will provide the link. But I have to say, that I don't know if this is the example that you want to see. But please do watch this talk even just a little bit, because I'm interested in your opinion on this.

Collapse
 
dfockler profile image
Dan Fockler

OOP seems like it's much more intuitive to the way people interact with the world. There are objects and they interact and you manipulate them. Whereas FP is like how a scientist sees the world. You have an input and you can transform the input(s) and route it to the next place, just like your example of circuitry.

Learning physics feels the same way as learning FP coming from OOP. You have this intuition about the world that may be useful for being a human, but it's all wrong to actually describe mathematically what's happening. So you have to rebuild your intuition with the new concepts you've learned.

Collapse
 
eddroid profile image
Ed Toro

I wish I had used the words "intuitive" and "intuition" somewhere in my post. 😞

Although I'd probably shy away from describing an OOP worldview as "wrong" (not to allege that you are describing it that way). As engineers we just want to use the best tool for the job. Sometimes simple-but-incomplete representations are useful, in physics and in coding.

Collapse
 
walkhard13 profile image
Phillip Smith • Edited

I love that the code examples were used to back up the philosophical ideas presented and not used as the sole crux for this piece. Thank you.

"Do not try and bend the spoon, that's impossible. Instead, only try to realize the truth...there is no spoon. Then you will see it is not the spoon that bends, it is only yourself." Spoon Boy, The Matrix

Collapse
 
stereobooster profile image
stereobooster • Edited

The story around Turing/Church is not quite correct. Turing and Church both tried to solve the same problem (halting problem or Entscheidungsproblem). This problem comes from the list of problems compiled by a German mathematician (Hilbert's problems). They arrived at the same conclusion same-ish time, but Church paper was published a little earlier, and Turing later add small proof to his original paper, which proved equivalence of Turing Machines and lambda calculus. Original Turing machines didn't use 0/1 they used special alphabet (Gödel numbering). Gödel also solved one of the problems from Hilbert's list (1931, Gödel's incompleteness theorems).

If you want to know actual story from Leibniz to Turing I recommend to watch this video.

Some fun facts:

  • Gödel "invented" computational complexity theory - he asked von Neuman in one of his letters is it possible to prove P = NP.
  • there is a modern field of science which studies machines, that are able to do more than original Turing Machines (from 1936). It is called Hypercomputation. Turing himself doubt that his original machines are the limit of computation, so in later works, he proposed new TM with Oracles
  • Hilbert, I guess, is most known for Hilbert Curve, which is very important in quantum physics
Collapse
 
johnkazer profile image
John Kazer

Very interesting thank you.
I find myself writing JavaScript web apps in a sort of hybrid functional-OOP approach. What works best to represent the issue at hand.
I don't know if that makes me weird or just not very good at either?!

Collapse
 
fc250152 profile image
Nando

I'm not a big expert, but reading your discussion on "object vs function" reminded me of something I've read about quantum mechanic. It seems that nothing does really exist, but that all is a kind of process instead ...
All of us are used to think in term of "hard objects" also in our normal life; so it's difficult to conceive the reality as prospected by "all is a process" (it echoes as Panta Rei - 'all flows' in greek).
So, I think that it's our materialistic frame of mind that likes better an Object paradigm versus a Function paradigm ... maybe ;)
At the end of the day, this strong discussion could be better solved by a big laugh, as some Zen master did on the end of his life :)
last but not least ... please excuse my bad english: I'm and old aged italian that didn't studied it at school
have nice days

Collapse
 
eddroid profile image
Ed Toro

lobste.rs/s/sjkxch/teaching_functi...

Here is some reaction to this post from another community. I share it to both acknowledge that they're correct and to compare that community with this one.

Collapse
 
drbearhands profile image
DrBearhands

I must agree with much that is said there. However, your post seems to have gathered a lot of attention, so you're doing something right, much more than myself.

To clarify, I believe teaching should consist of teasing (cultivating interest for it) and after that dumping knowledge. Most teachers fail at the first part. I personally lost interest in a few subjects for this very reason, later discovering they were really cool.

This post was a success in 'teasing', the numbers clearly show that.

Collapse
 
differentsmoke profile image
Pablo Barría Urenda • Edited

I will preface the following opinion by saying that I feel like I have never really "gotten" OOP, which may mean that I haven't, or it may mean that I have and was simply underwhelmed by it. So please, feel free to enlighten me on how I got it wrong:

Having clarified that, I must say that the way I understand it, while FP is a way of structuring processes, OOP is a way of structuring code, so the two paradigms operate at different levels, and their frequent opposition is more a result of companies staking their worth on buzzword branding than anything having to do with the paradigms themselves.

OOP was the ultimate programming buzzword for so long, that my realization that it is mostly about how one organizes a program's code, and deciding which parts of it can call which other parts, was something of a buzz kill. I feel OOP has little bearing on how things are done, mostly on where they are kept, while FP, which can be traced back to Lambda Calculus which is itself a model of computation, is much more involved in the how of things.

Collapse
 
alexm77 profile image
Alex Mateescu

Well, I had some thoughts around the matter as well (take a look here if you have time on your hands: dev.to/alexm77/how-i-relearned-oop...).
I have also found that (for reasons I cannot explain) OOP makes it easier to get to grips with projects that go beyond "hello world" examples. Yet, in the grand scheme of things, expressing business logic in functional terms is simpler more often than not.
As for why FP hasn't taken off till now, my theory is the compilers/interpreters are inherently more CPU intensive and until recently we just didn't have enough CPU HP to make these things tractable.

Collapse
 
belinde profile image
Franco Traversaro

Great article and I really enjoyed reading it, but still I don't get how FP can be "better" than OOP when working on a "normal" web project (there's a lot of "" as you see). It's interesting the part about hardware programming, and I see the advantages of a FP approach, but I remain of my advice that FB smells like an articulated academical argument. Surely I'm wrong, but I can't see it better 😟

Collapse
 
eddroid profile image
Ed Toro

I don't mean to give the impression that FP is better. I'm more interested in presenting it as a valid alternative. It is possible to build a "normal" web project using it. But I'm not arguing we should.

Once we accept the possibility that we can, I hope curiosity will compel us to try to figure out how. There are plenty of web frameworks for functional-styled languages to explore.

If you find yourself exploring one of those frameworks, it may violate your intuition. And that will feel "wrong", like a bad code smell. And we're trained to "trust our guts" on those kinds of things.

Before you go on that journey, try some of the thought experiments presented in this article to open yourself up to the possibility that you intuition may be, instead of protecting you from making a mistake, just hiding something useful.

Since most languages are multi-paradigm, you can begin to explore these features in your own code without making a huge commitment - even if you just replace some for-loops with forEach, map, filter, or reduce in some deep down method somewhere. FP isn't all-or-nothing.

Collapse
 
qm3ster profile image
Mihail Malo • Edited

Turing Machine

I understand OOP
I understand FP

But that... thing. It scares me.

Collapse
 
baso53 profile image
Sebastijan Grabar

Wow, what a mind blowing comparison between OOP and FP. It almost seems like a conspiracy theory when you're reading it, but it is such a creative explanation. I'm out of words!

Collapse
 
ondrejs profile image
Ondrej

One of the best introduction articles to FP I have read, Ed. Thanks for it, bookmarking & will share ! Keep on writing.