Wednesday February 05, 2014

Solving The Huge View Controller Problem

Brad Grzesiak wrote a thoughtful piece on following the single responsibility principle when building view controllers in storyboards. His take challenged me since I had always thought the single responsibility principle was just a good guideline for making sure a class doesn’t do too much right now. But, as Brad points out:

By focusing on why the class might have to be changed, the Single Responsibility Principle allows us to think of how we interact with the code in the present instead of requiring us to project our minds onto the software’s future running environment. This, I believe, is an important, and freeing, distinction.

In other words, separating out the responsibilities clarifies where we need to look when requirements change. And the requirements always change. Software shares much in common with living organisms that must adapt to their environment. That adaptation is quite messy. The organism could get sick or sprout cancerous growths. It’s quite different from the process to build a finished structure, like a bridge, that only changes as it degrades according to the properties of the materials it was built with.

Brad demonstrates how he tries to minimize those cancerous growths in his software. It’s good to see more examples of breaking apart the responsibilities of a view controller into collaborating objects that all live together in a storyboard scene.