Clean Architecture for Functional Programming

1,697 views
Skip to first unread message

swkane

unread,
Sep 14, 2013, 11:20:47 PM9/14/13
to clean-code...@googlegroups.com
Hi,

    After reading the paper Out of the Tar Pit (http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.93.8928) and watching Rich Hickey's talks on Clojure, I am convinced that functional programming (where pure functions are the default and side effects are minimized) is a superior way to program vs. OOP and mutable state. The two languages that I am now interested in learning are Clojure and Haskell. However, I'm wondering how the Clean Architecture and the Clean Code best practices apply to these types of languages, rather than OOP based languages?

Steven

Roberto Guerra

unread,
Sep 15, 2013, 9:40:43 AM9/15/13
to clean-code...@googlegroups.com
I don't think one is better than the other. I've found that many people just don't know OOP and think its all about classes. James Coplien would call it 'class-oriented programming' instead of 'object oriented programming'. There have been a number of talks exploring the symbiosis of FP and OOP and the one that makes most sense to me is what Gary Bernhardt calls 'Faux Oriented  Programming', or 'Functional Core, Imperative Shell'. Where the lower level or core of the application is very 'functional' that is wrapped in an abstraction or shell that is OO.

There are also situations where one is a better fit than the other. For example, FP is a better fit for parsing or data processing than OOP. But for building User Interfaces, I strongly believe OO is the best fit. User Interfaces is about managing the state of the screen, an creating 'objects' that 'communicate' with each other. Sounds just like OO to me. 

The important part is being pragmatic and understanding where each one fits, instead of throwing away the baby with the bathwater. When you hear Rich Hickey talk you have to understand his background and where he is coming from. For systems he builds, FP is the best fit, and he will admit he is not talking about building your run of the mill CRUD application. Oh, and architecture is still important in FP, don't think for one minute you won't have to think about it. Clean  Code best practices also apply to FP. Functional Programmers put a lot of energy into writing very small focused functions that you can chain together. And good names are still important. Everything about Clean Code is still important in FP. By virtue of just using Lisp/Clojure/Haskell doesn't mean your code will be clean or perfect and you don't have to think about it.

Per Lundholm

unread,
Sep 15, 2013, 4:17:27 PM9/15/13
to clean-code...@googlegroups.com
You should definitely go and learn Clojure. The grass might not be as green when you get there but it is an excellent exercise. When you come back to your OOP language, it will not look quite the same.

Also try to learn Prolog, a declarative language where backtracking is the single most interesting thing about it, IMHO.

Erlang is something to look into as well, given the solutions to error handling and massive concurrency.

Regards,
  Per


--
The only way to go fast is to go well.
---
You received this message because you are subscribed to the Google Groups "Clean Code Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clean-code-discu...@googlegroups.com.
To post to this group, send email to clean-code...@googlegroups.com.
Visit this group at http://groups.google.com/group/clean-code-discussion.



--
Per Lundholm, Crisp AB
http://blog.crisp.se/perlundholm

Roberto Guerra

unread,
Sep 15, 2013, 5:46:59 PM9/15/13
to clean-code...@googlegroups.com, per.lu...@crisp.se
Erlang is quite an interesting language/community. They vehemently 'deny' OO, but it is probably the most OO language than most languages that claim to be OO. If you read some of Alan Kay's interviews, he stresses message passing as the real essence of OO, and Erlang focuses a lot on messages.
To unsubscribe from this group and stop receiving emails from it, send an email to clean-code-discussion+unsub...@googlegroups.com.

To post to this group, send email to clean-code...@googlegroups.com.
Visit this group at http://groups.google.com/group/clean-code-discussion.

Uncle Bob

unread,
Sep 23, 2013, 2:15:11 PM9/23/13
to clean-code...@googlegroups.com
There have been a lot of very good replies to your question; and I agree wholeheartedly with all of them.  I'll only add this.  

1. FP and OO are not opposites.  It is quite possible to have programs that are both OO and Functional.  
2. If you look very closely at the clean architecture, you'll realize that much of the objects _are_ functional because they take data structures in, and return other data structures.  Interactors and Presenters, for example, do not maintain any state of their own.  Even those objects that do imply some kind of state, such as entities and gateways, keep that state hidden behind boundaries and present a functional interface instead.


Bernard Notarianni

unread,
Jul 9, 2014, 3:37:27 AM7/9/14
to clean-code...@googlegroups.com

I understand the "messages" metaphor in OOP as the building of a conversation between objects. That's actually why Alan Kay named the language "SmallTalk": the objects are talking each to others. I recommand this excellent book to discover more about Smalltalk: http://c2.com/cgi/wiki?SmalltalkObjectsAndDesign


Messages in Erlang are different animals. They are not the primary brick to build the code as in OOP languages. They are here to help different processes communicate together. OTP (the technical framework on top of Erlang) actually hides most of those messages passing and it is not considered a best practice to have the message passing operator ! in your code.

In Erlang, you really should think with FP mindset when coming to design and write code. Use messages only to separate "micro services" and handle the concurrent and/or distributed part of the system.

Robert C. Martin

unread,
Jul 9, 2014, 11:36:43 AM7/9/14
to clean-code...@googlegroups.com


On Jul 9, 2014, at 2:37 , Bernard Notarianni <bernard.n...@gmail.com> wrote:

The vehemence with which some folks claim that FP and OO are antithetical is amusing. A message is a function call. A function call is a message. The two are isomorphic.

* In smalltalk you might say: obj msg arg.
* In java you might say obj.msg(arg);.
* In C you might say msg(obj, arg);
* In Erlang you might say obj ! {msg, arg}.

These are not fundamentally different concepts. There are some technical differences: The C statement isn’t polymorphic, and the Erlang statement crosses a thread boundary. But those are details.

Over the last few decades we’ve gotten all bolluxed up with the idea that there is something special about OO that makes it fundamentally different from Structured Programming, or Functional Programming. This isn’t quite true. Programming is programming. It’s all about functions and data.

What we’ve learned over the last five decades is what _not_ to do; not so much what _to_ do.

Structured Programming is programming without goto. (Discipline imposed upon direct transfer of control).
OO Programming is programming without pointers to functions. (Discipline imposed upon indirect transfer of control).
Functional Programming is programming without assignment statements. (Discipline imposed upon state).

The fact that these three paradigms are _subtractive_ rather than additive means that they are all mutually compatible. If you want to be structured, OO, and Functional, then don’t use goto, pointers to functions, and assignment.


Per Lundholm

unread,
Jul 9, 2014, 2:42:53 PM7/9/14
to clean-code...@googlegroups.com
Word. 

But I would add to that, that these paradigms helped us to think differently about programming. When I learned SP, I realized how I could get a better structure of my code. With OOP I learned to keep functions and related data together. Functional programming is Lisp back in a new dress and reminds me how nice it was to have as much stateless as possible. Now, objects have state so that feels like a competing concept but you need state even in a Lisp program. You are modelling some reality with your code and it has state.

Then there is Prolog which no one understands. :)


--
The only way to go fast is to go well.
---
You received this message because you are subscribed to the Google Groups "Clean Code Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clean-code-discu...@googlegroups.com.

Frederik Krautwald

unread,
Jul 10, 2014, 5:17:50 PM7/10/14
to clean-code...@googlegroups.com
Then there is Prolog which no one understands. :)

Usually, I only understand the Epilog.

Zachary Kessin

unread,
Jul 11, 2014, 5:48:16 AM7/11/14
to clean-code...@googlegroups.com
I *LIKE* Prolog, but then I am strange I have been spending the last week doing weird integration stuff with Erlog which is a prolog that runs on the erlang VM and erlang it self.

--Zach

On 7/11/14, 12:17 AM, Frederik Krautwald wrote:
Then there is Prolog which no one understands. :)

Usually, I only understand the Epilog.
--
The only way to go fast is to go well.
---
You received this message because you are subscribed to the Google Groups "Clean Code Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clean-code-discu...@googlegroups.com.
To post to this group, send email to clean-code...@googlegroups.com.
Visit this group at http://groups.google.com/group/clean-code-discussion.

--
Zachary Kessin
Mostly Erlang Podcast
Skype: zachkessin
Twitter: @zkessin

Rodrigo Baron

unread,
Aug 29, 2014, 4:21:45 PM8/29/14
to clean-code...@googlegroups.com
A good example of clean architecture in golang: https://docs.google.com/document/d/1iBH1VrrFBOV_OKFP3RdYO0DanaopnJFY-U1Tf84qX6c/edit . Maybe can help you ..

Hok Shun Poon

unread,
Mar 28, 2016, 5:38:09 PM3/28/16
to Clean Code Discussion
Some years later, in 2016, we have React, which seeks to completely blow up the preconception that the only way to build UI's is through stateful OOP.  React treats the UI as an immutable value, and vastly simplifies UI programming.

Matt Hughes

unread,
Mar 30, 2016, 5:05:46 PM3/30/16
to Clean Code Discussion
An up and coming language for web applications is the Elm programming language. From an architecture / code structure point of view, there is the Elm Architecture, which describes how to build up programs from smaller pieces. Elm is an implementation of discreet time functional reactive programming, so it's a functional programming language which takes a lot of inspiration from Haskell. It's easy to get started without installing anything, check out the examples at http://elm-lang.org/examples, or try it out in your browser at http://elm-lang.org/try

Highly worth checking out.

mch
Reply all
Reply to author
Forward
0 new messages