[ Content | View menu ]

Solve Your Problems with Abstraction

Mark Mzyk | December 10, 2007

I mentioned a while back that I was going to write more about the training I received from Bob Martin and I’m finally fulfilling that promise.

Bob had us do an exercise where we modeled a working coffee maker with certain requirements.  Upon being presented with this problem, my first reaction was to think of a finite state machine and to jump into modeling  the coffee maker.  My group agreed, and this was the approach we ended up taking.  Other groups took differing approaches, but at the end we had all modeled what looked like working coffee makers.

Then Bob jumped in.  Now, I’m sure he’s worked the solution to this problem an infinite number of times, but knowing that, it was still impressive watching him work.  Bob didn’t jump to a finite state machine.  He didn’t take any of the other approaches that had been tried by the various groups.  Instead, he took a step back and grouped the components of the coffee machine by their functions, and then used those groupings to build up a class diagram that modeled the interaction of the coffee maker.

It was simple, elegant, and fast.  Or maybe I only thought that because Bob Martin did it.  Still, it was the best solution to the problem presented that day.  And Bob did something no one else in the room did – he created an abstraction.

The abstraction allowed Bob to simplify the problem, and it also allowed Bob to redefine the problem.  It was now a problem of various systems working together, not just individual coffee parts waiting for the on switch to be flipped.

It showed me that I don’t always need to dive into the gritty details of the system.  Taking a step back can often provide more insight while making the problem easier.  Of course, no matter how many times I’ve read about creating an abstraction, sometimes it takes seeing an expert do it to really understand the power behind an approach.

Is it possible to over abstract something?  Absolutely.  But going one or maybe two levels out from the fine grains likely isn’t a bad idea.

Of course, I’m not the first person to write about this.  It’s been said before, and likely much better than I just said it, here.