How NOT to apply the Single Responsibility Principle to functions

As I said earlier, I was rather surprised when I heard Uncle Bob’s version of the Single Responsibility Principle. His way of splitting code into functions, explained right after that, also made me think “WTF?” The idea is to have only one set of curlies per function. Only one loop, only one “if”, and so on.

Not making a big ball of mud out of your class is surely important, but this approach is extremist programming. It seems rather strange to explain obvious things, but since Uncle Bob has an experience of 40 years (and I much less), I’ll do it.

As Scott said on the same show, it’s important how much one sees on the screen simultaneously. And this is a reason not to split code into too many small functions. Each function adds 3 lines (closing curly, empty line, opening curly), so if the functions are very short, you see considerably less on the same screen. But what’s worse, you don’t see your code in the order it executes, and that’s confusing. Code split into many small functions, whose relationships are not clear, is confusing. I’m not theorising here, this is my experience.

The same reason, that seeing all of “something” on the screen together helps understanding, is one reason to split long functions. But this splitting should be made according to some logic, such as the SRP, not the arbitrary rule “Don’t use curlies”.

What Uncle Bob is doing here is using functional programming instead of structured programming. In the previous post I discussed his use of object-oriented programming instead of functional programming. This seems like a pattern, though I don’t quite get its significance.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: