1. Templates Considered Harmful
Apple Templates and the Single Responsibility
Principle
Brian Gesiak
March 28th, 2014 at Recruit Co., Ltd.
Research Student, The University of Tokyo
@modocache
2. Today
• The single responsibility principle (SRP)
• The UITableViewController file template
• Model and controller code all rolled into one
• Example of how to separate concerns
• Moving data store logic out of the controller in
GitHubViewer.app
• Apple file and project templates
• Many violate the single responsibility principle
• Best to think of them as“proof of concepts”
48. GHVReposViewController
1. View controller responsibilities
• Kick off repository fetch
• Show network activity indicators
2. UITableViewDataSource responsibilities
• Store repositories
• Specify how many sections in table view
49. GHVReposViewController
1. View controller responsibilities
• Kick off repository fetch
• Show network activity indicators
2. UITableViewDataSource responsibilities
• Store repositories
• Specify how many sections in table view
• Specify how many rows in each section
50. GHVReposViewController
1. View controller responsibilities
• Kick off repository fetch
• Show network activity indicators
2. UITableViewDataSource responsibilities
• Store repositories
• Specify how many sections in table view
• Specify how many rows in each section
• Create each cell based on repository data
51. GHVReposViewController
1. View controller responsibilities
• Kick off repository fetch
• Show network activity indicators
2. UITableViewDataSource responsibilities
• Store repositories
• Specify how many sections in table view
• Specify how many rows in each section
• Create each cell based on repository data
3. UITableViewDelegate responsibilities
52. GHVReposViewController
1. View controller responsibilities
• Kick off repository fetch
• Show network activity indicators
2. UITableViewDataSource responsibilities
• Store repositories
• Specify how many sections in table view
• Specify how many rows in each section
• Create each cell based on repository data
3. UITableViewDelegate responsibilities
• Push view controller for selected repository
53. GHVReposViewController
1. View controller responsibilities
• Kick off repository fetch
• Show network activity indicators
2. UITableViewDataSource responsibilities
• Store repositories
• Specify how many sections in table view
• Specify how many rows in each section
• Create each cell based on repository data
3. UITableViewDelegate responsibilities
• Push view controller for selected repository
100+ lines of code
54. GHVReposViewController
1. View controller responsibilities
• Kick off repository fetch
• Show network activity indicators
2. UITableViewDataSource responsibilities
• Store repositories
• Specify how many sections in table view
• Specify how many rows in each section
• Create each cell based on repository data
3. UITableViewDelegate responsibilities
• Push view controller for selected repository
100+ lines of code
83. GHVReposViewController
1. View controller responsibilities
• Connect table view to data store
• Show network activity indicators
2. UITableViewDelegate responsibilities
84. GHVReposViewController
1. View controller responsibilities
• Connect table view to data store
• Show network activity indicators
2. UITableViewDelegate responsibilities
• Push view controller for selected repository
85. GHVReposViewController
1. View controller responsibilities
• Connect table view to data store
• Show network activity indicators
2. UITableViewDelegate responsibilities
• Push view controller for selected repository
Less than 100 lines of code
90. The Problem with Apple Templates
If Only They Knew About the SRP
91. The Problem with Apple Templates
If Only They Knew About the SRP
• Master-Detail Application (with Core Data)
• AppDelegate
• Sets up UIWindow root view controller
• Sets up Core Data stack, handles errors
• 100+ lines of code
92. The Problem with Apple Templates
If Only They Knew About the SRP
• Master-Detail Application (with Core Data)
• AppDelegate
• Sets up UIWindow root view controller
• Sets up Core Data stack, handles errors
• 100+ lines of code
• OpenGL Application
• View controller does it all!
• Sets up OpenGL context
• Compiles shaders
• Stores vertex data
• 400+ lines of code
93. Not All Templates Created Equal
Some Templates Employ Modular Design
94. Not All Templates Created Equal
Some Templates Employ Modular Design
• Page-Based Application
• Separates concerns among UIPageViewDelegate and
UIPageViewDataSource
• Small classes
95. Not All Templates Created Equal
Some Templates Employ Modular Design
• Page-Based Application
• Separates concerns among UIPageViewDelegate and
UIPageViewDataSource
• Small classes
• SpriteKit Game
• Small classes with a relatively clear separation of
concerns
96. Takeaways
• Single responsibility principle
• Classes should have one, and only one, reason to
change
• Apple’s templates should be thought of as“proof of
concepts”, not as examples of clean, well-structured code
• Just because Apple does it doesn’t mean it’s a good idea
• Use your better judgement on what’s“clean code”and
what isn’t
97. Want More on Clean Code?
• Slides for this talk available at http://modocache.io/
apple-templates-considered-harmful
• Follow me on Twitter and GitHub at @modocache
• Clean Code Resources
• Follow Robert C. Martin at @unclebobmartin
• More on Object-Oriented Design at http://
www.butunclebob.com/
ArticleS.UncleBob.PrinciplesOfOod
• Clean Code videos at http://cleancoders.com/
• Building a Healthy Mistrust of Apple Engineering
• Follow Peter Steinberger at @steipete
• Follow Justin Spahr-Summers at @ jspahrsummers