Functional Java by Example | Part 2 – Tell a Story

The example I’m evolving in each part of the series is some kind of “feed handler” which processes documents. In previous part I started with some original code and applied some refactorings to describe “what” instead of “how”.

In order to help the code going forward, we need to tell a story first. That’s where this part comes in.

If you came here for the first time, it’s best to start reading from the beginning. It helps to understand where we started and how we moved forward throughout the series.

Read out aloud

When I first started using Spock as a testing framework, since it came by default with Grails many years ago, I was impressed (and still am) by its many features and ease of use.

You know what Mocks, Stubs and Spies are, right? Mockito has them, Powermock has them and basically every other serious (unit) testing framework. The concept of a Mock isn’t hard to grasp (you can read all about it here), but Spock has a special way of describing the (expected) interactions with its mocks.

After around 120 yards, indicate with your blinkers for at least two seconds before beginning to switch to the left lane.

Slowly move the car into the other lane. Turn your hands on the steering wheel ever so slightly in order to change lanes. It only takes a very slight movement of the wheel; as most modern cars are fitted with power steering. It should take anywhere from one to three seconds for you to change lanes. Any less and you’re doing it too quickly; any more and you’re doing it too slowly.

Another X steps…

Good luck!

Or, consider an alternative universe where the conversation would go like this:

You:

Sir, would you happen to know how to get to the Theater from here?

Pedestrian:

Sure, that’s pretty easy. Here we go:

Take a left turn and cross the bridge. It’s on your right.

Good luck!

The last scenario is a breeze: clear directions what to do & where to go!

The first scenario, however, is ridden by details — with low-level specifics about driving the car itself — and even while we would not hope to get directions like that in real-life we still write software like that.

Tell me something on the right level. If I need specifics I’ll ask for it.