What’s the function of this function?

I came across this post by Martin Fowler today, and it got me thinking. I’m pretty serious about decomposing methods for re-use, and I definitely prefer shorter methods, but I haven’t been quite as strict about it as what I’ve heard him and others say (e.g. if it doesn’t fit on the screen, it’s too long).

But in this piece he gives an analysis question that I think may be the key for me in this:

The argument that makes most sense to me, however, is the separation between intention and implementation. If you have to spend effort into looking at a fragment of code to figure out what it’s doing, then you should extract it into a function and name the function after that “what”. That way when you read it again, the purpose of the function leaps right out at you, and most of the time you won’t need to care about how the function fulfills its purpose – which is the body of the function.

It’s a fairly obvious question; it’s something I think about regularly when writing APIs. But I’d never really thought about doing it at a lower level, within private methods.

So I’m going to give it a shot this month and see what happens. I’ll try to remember to post an update.

Leave a Reply

Your email address will not be published. Required fields are marked *