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.
Filed in: Programming,Software Engineering.
And if you really like programming with minimal side-effects I invite you to try out functional programming. One of the main tenets of FP-style languages is to reduce as far as possible the number of side-effects for a given action.
<hint>Erlang would be one such language</hint>
[...] Recent public urls tagged “objectoriented” → OO Side Effect Free Programming [...]
You can do side-effect-free OO programming directly, just have “setter” methods return a copy of the object instead of mutating the original (OCaml even has syntactic sugar for this).
This presentation was a real eye-opener for me:
http://www.ccs.neu.edu/home/matthias/Presentations/ecoop2004.pdf
Thanks John, I’ll look over the presentation. That’s an interesting idea of having setter methods return a copy. I hadn’t considered that.
Also, sorry it took a while for you comment to show up. It was caught by the spam filter and I didn’t notice it right away to free it.