[ Content | View menu ]

OO Side Effect Free Programming

Mark Mzyk | August 27, 2008

Last year Bob Martin gave my coworkers and me some training on how to implement Test Driven Development.  Bob advocated for very small methods – even suggesting that methods often only have one or two lines of code.  If they started approaching five to ten, they could probably be broken down into separate methods.

Yesterday, while performing the activity of brushing my teeth, I started thinking about some Erlang code I was writing in my spare time.  Also, previously at work, some of my coworkers had been looking at Fitnesse, which is contributed to by Bob Martin, and commented on how short the methods were.

With both of these thoughts fresh in mind, it hit me:

Small methods are the object oriented equivalent of side effect free programming in functional programming.

A small method is in effect an immutable variable that gets passed around.  At the very least, it minimizes and isolates side effects, while enabling easy refactoring and testing.  It’s the closest to side effect free programming you can get in OO.