I have an iPhone application that contains several views and their associated controllers. Looking at sample code, I’ve seen different different ways to organize these files – either have all of the views grouped, then all of the controllers grouped, or group the views and controllers by functionality.
Option 1 – Views and controllers grouped separately
-Views
|
- EditItemView.h
- EditItemView.m
- AddItemView.h
- AddItemView.m
-Controllers
|
- EditItemViewController.h
- EditItemViewController.m
- AddItemViewController.h
- AddItemViewController.m
Option 2 – Items grouped by functionality
-AddItem
|
- AddItemViewController.h
- AddItemViewController.m
- AddItemView.h
- AddItemView.m
-EditItem
|
- EditItemViewController.h
- EditItemViewController.m
- EditItemView.h
- EditItemView.m
Option 1 seems to make more sense from a MVC standpoint – the code is grouped together, but I’m wondering as the app grows to 10+ views and controllers, is that the most logical and maintainable? Is there a best practice recommendation around this? Currently, I will be the only one maintaining the app, but whether or not there will be multiple developers, I want to use best practices as much as possible. Are there published standards on this?
I’m working on a big xCode project right now. It isn’t for the iPhone, but I don’t think that matters for the sake of file structure layout 🙂
I started out with option #1 and later moved to something like option #2 when the number of files increased. I tend to group things by “interfaces” i.e., all of the sources associated with a particular area of functionality within the application, and then create sub-groups for larger sections if need be.
As far as naming goes, I prefer to identify Model, View and Controller using as little class name real-estate as possible, so my class names look similar to:
This still allows for a quick visual check to see the type of a class (M, V, or C) but it leaves more room for the descriptive part of the name.
I’ve also found it useful to specify some classes that do not fit into the MVC pattern (gasp!):
Anyway, that’s already more information than you asked for, but I find the naming issue to be relevant to the layout issue, since the real question is: what is the best way to organize my code for a large xCode project?
Hope it helps. Here is how it all looks when put together: