Tuesday, 30 December 2014

http://www.johndcook.com : Object oriented vs. functional programming

OO makes code understandable by encapsulating moving parts.
FP makes code understandable by minimizing moving parts.
This explains some of the tension between object oriented programming and functional programming. The former tries to control state behind object interfaces. The latter tries to minimize state by using pure functions as much as possible.
It’s understandable that programmers accustomed to object oriented programming would like to add functional programming on top of OO, but I believe you have to make more of an exclusive commitment to functional programming to get the most benefit. For example, pure functions are easier to debug and to execute in parallel due to their lack of side effects. But if your code is only semi-functional, you can’t have the same confidence in testing your code or in spreading it across processors.
James Hague argues that 100% functional purity is impractical and that one should aim for 85% purity. But the 15% impurity needs to be partitioned, not randomly scattered across your code base. A simple strategy for doing this is to use functional in the small and OO in the large.Clojure also has some very interesting ideas for isolating the stateful parts of a program.
http://www.johndcook.com/blog/2010/11/03/object-oriented-vs-functional-programming/

No comments:

Post a Comment