Tag Archives: Skinny ViewControllers

My Quest to Skinnier ViewControllers

Fat ViewControllers are a pain in the rear. So I have been researching different solutions to cope with the problem. There are different approaches batteling this, but none of the solutions I have found are exactly what Im looking for. VIPER is one approach, but its architecture is too fragmented, a project built with this paradigm gives you a lot of small classes and extensions, making the Xcode project a jungle of goups and small files, and adding an extra layer of complexity. For some this is just what they are looking for, but for me its not. Chris Eidhof has also a really interesting approach with intentions. I tried this but so far I have not gotten it to work for me the way I wanted. So because I write Swift in active projects, I needed an approach I was comfortable with, and found in the Ray Wenderlich Swift Style Guide.

The easiest way is to show this in code.

How I used to do it

Ray Wenderlich Style Sheet

My form of skinny ViewControllers

Instead of showing everything into one single file, I ended up with putting each of the extensions into seperate files, using a really simple naming convention. I have the UIViewController, MainVC, in a ViewController group. The DataSource goes into a DataSource group, and calling it MainDS. And the ViewDelegate? You guessed it right, it goes into a ViewDelegate group with the name MainVD. So for the program logic, I put MainLogic into a Logic group. All data models goes into a DataModel group. So, with an easy glance, I know where each file is depending on what I want to edit. Not a lot of searching for where I put something, everything goes into its respective group. The only stuff going into MainVC.swift file, are vars, outlets and actions.

This is probably not the perfect way of arranging a project, but for me it works fine. I really hope you could give me some feedback on it, and pointers on how to enhance the design pattern.

Update

Im really happy with all the feedback I got in very short time, and one tip was to organize all the files in one group for each ViewController. So in MainVC, MainDS, MainVD and so on goes into a Main group. This is a really good idea, and Im going to give it a try in ome of my ongoing projects. Keep the good ideas coming, I really appreciate it!