Monday 9 April 2012

OOP Patterns in functional language

The topic was inspired by my Master Thesis, which I have handed in recently but I want to proceed working on it as haven't done it properly.

The thing I tried to do - to implement OOP patterns in functional language - Erlang.
Resulted sources are at github

A lot of developers will admit that it doesn't worth the attempt because of difference in paradigms and because most of OOP patterns are already implemented naturally in FP languages.

I was trying to describe my goals in thesis(paper)  and below is some part from introduction:

Today it is realy hard to find software developer who hadn't heard about design patterns and refactoring techniques.
This terms became very popular few years ago and are very widely used in modern software development.
I remember when I heard the first time at second year of study about patterns and read few articles about it - I was very delighted with this new world and interesting methods of code organization and become very enthusiastic about patterns.
The problem which really caused me to choose the subject for thesis is that "Design Patterns" in our mind is very closely associated with Object-Oriented paradigm.
Since the time of GoF book design patterns where very popular subject for different researches, discussions and pulications, they had a lot of heads involved and thats why the topic was developed very quickly and have now a huge base.
But the problem was that most of this researches and publications were about implementation and application of patterns/refactoring to Object-Oriented paradigm. It was most popular and authors used it as primary in their work. Other paradigms had no such careful attention, and it is black side of OOP monopolism in enterprise application development.
I always have been a loyal supporter of functional paradigm, so I aways wanted to have this gap filled at least in this paradigm.
In order to do classic patterns in functional languages we should escape from OO paradigm limitations. As well we should have new patterns suggested by functional languages.
After developing for over four years in at work(mostly Java), I was pretty comfortable with best practices for creating applications in OO languages.
But when I tried to create my first application in Erlang(am not taking into account Lisp samples written during very short academic course), I ran into all sorts of problems. You can feel it even trying to write any complex XSLT with parsing and recursion.
But I have realized that what I was trying to do was use an OO approach in a functional programming language. There were fundamental differences between the two approaches, and "paradigm shift" is not just yet another philosophic phrase.
Another idea is that to make functional paradigm more popular and widely used we need to involve to it a part of experienced OOP "Coryphaeus". Because functional programming encompasses a very different way of composing programs, programmers who are used to the imperative paradigm can find it difficult to learn.
So I think that it would be very useful to show comparison/make mapping between well-known OOP design patterns and functional analogues, and to create last ones if we need that.
In process of making such comparison we would find out problems and gaps of each paradigm in regard to patterns and refactoring techniques.
Hope it would be helpful for students as well when they try to learn course in functional languages. As at such courses we usually do not have time to look at FP in depth, and are only learning basic syntax. This fact causes us not to think about FP as something which can be used in real life, in production.
But as far as most of students are aware of OOP design principles and patterns, it would be interesting for them to find out that the same problems/tasks can be solved in functional languages in much easier and straight-forward way.