Andy Matuschak gave a good talk on refactoring a mega controller.

Watch it.

It is a very good video by Andy Matuschak, performing delightful live coding and narrating his thoughts process.

You might recognize Andy, an ex-Apple engineer who gave a great talk in WWDC 2014 on app architecture – where is the truth?

The Usual Problem

We often got into is writing Massive View Controller, resulting in 1 single file that contains all kind of code!

Apple sample code and programming guide didn’t help to guide us towards clean code/architecture.


There is no magic.

Mostly Decomposition

Or as Uncle Bob says: Extract till you drop

Where is the truth?

Think hard which class should hold the truth.

It is VERY RUDE for a view controller to change it’s parents properties

For example, don’t change your navigation bar tint color directly. Your view controller does not own the navigation bar. What if another (child) view controller also change the tint?

Test driven development (TDD) will force you to architect your code nicely.

What Pasta is Your Code?

As you decompose/abstract/encapsulate your code, the structure changes.

  1. Spaghetti code - messy
  2. Lasagna code - well layered
  3. Ravioli code - very much decoupled

It is up to you to find the balance, which is not easy.

You want to decompose as much, but not to the extend that it difficult to follow the flow of data.


Storyboard instantiate your initial view controller so you have no chance to init yourself.

A trick is shown in the talk where you instantiate your VC manually in AppDelegate and set it to the navigation VC (the root VC of window).




Back to Home