Learning Functional Programming Makes You a Better OO Developer

By Marika Engström  |  March 2, 2018

Pure functions in functional programming are like school book examples of clean code: they always evaluate to the same result given the same argument value(s) without any side effects. Pure functions rhyme also very well with the Single Responsibility Principle (SRP) in SOLID principles: a class should only have a single responsibility. A concept that can equally be applied for other constructs, such as functions and methods. Learning functional programming and specially writing pure functions can therefore make you a better OO programmer.

 

When I started learning functional programming, Scala, I saw myself falling back to Java style programming and not really embracing the power of functional language. Therefore I bought the book Functional Programming in Scala. Already the first example in the book was an eye opener for me. Below is the second iteration of a simple method to buy a coffee (listing 1.2, page 5):

1. class Cafe {
2.    def buyCoffee(cc: CreditCard, p: Payments): Coffee = {
3.      val cup = new Coffee()
4.      p.charge(cc, cup.price)
5.      cup
6.    }
7. }

 

As a Java developer I would have been ok with this solution. Specially if the Payments would be an interface, in which case it could be mocked and the method can easily be tested. However the line 4 has an obvious side effect and therefore the code was refactored to following (page 5):

1. class Cafe {
2.    def buyCoffee(cc: CreditCard): (Coffee, Charge) = {
3.      val cup = new Coffee()
4.      (cup, Charge(cc, cup.price))
5.    }
6. }

Seeing the example above made me to realize that I had not challenged myself to think one step further. The benefits of this solution are not only getting rid of the side effects, but also the second solution is easier to reuse for example when the customer wants to buy more than one coffee. By learning functional programming and specially writing pure functions you will (almost) automatically get functions that match OO design principles as well as criterias for a clean code. To put it in Martin Fowlers words: “Functions should do one thing. They should do it well. They should do it only.

 

There are others that have made the same observation that functional programming rhymes well with the OO design principles. Here are a couple other blog posts of the same subject for further reading SOLID: the next step is Functional by Mark Seemann and Solid principles in Clojure by Eric Nordman.

Note: Examples from the book Functional Programming in Scala written by Paul Chiusano and Runar Bjarnason, 2014.

AUTHOR

Marika Engström

Senior Software Engineer

You may also like

Go See Talents AB

Convendum

Västra Järnvägsgatan 3

111 64 Stockholm​

Sweden

  • Facebook - White Circle
  • LinkedIn - White Circle
  • Instagram - White Circle
  • Twitter - White Circle

Copyright © 2019 Go See Talents AB

We use cookies to give you the best user experience. By using this website, you agree to the use of cookies. Privacy Policy.