Code Organization Best Practices
Friday, March 2, 2007
I’ve been working on a pretty large OS X development project for the past few weeks. With any large project, some source code files can become pretty insane with the amount of methods and code in general. I try to adhere to a set of best practices for organizing my code.
I’ve outlined my organization method below, which I am sure is different from yours. I’m curious as to how you organize your code.
@implementation ClassName
#pragma mark -
#pragma mark Static Methods
// +initialize
#pragma mark -
#pragma mark Init Methods & Superclass Overrides
// dealloc, awakeFromNib, etc
#pragma mark -
#pragma mark Subclass Methods
#pragma mark -
#pragma mark IBActions
#pragma mark -
#pragma mark NSApplication Delegate Methods
// applicationWillTerminate, etc.
#pragma mark -
#pragma mark Delegate Methods
// One pragma section per class I am working with.
#pragma mark -
#pragma mark Accessors & Mutators
@end
Tip for those that don’t know: #pragma mark - will add a divider line in your source code when you try to view it.



Comments
Dan Wood says:
This is a pseudo-trackback. :-) My followup in the link.
Posted March 2, 2007 at 6:31 pm
R v Amerongen says:
I did change my templates .[hm] to insert this as a default. If there is a line to much for sure I do remove it. Otherwise it stays. If I use a category file to split my large files I keep the pragma’s in both ( all ). and add a comment line with the path in where I can find the real code in which file. Easy quick and clear.
Posted March 3, 2007 at 6:31 pm
Jérôme says:
Thanks for that hint! I didn’t know.
The suggestion of Dan Wood defining a macro makes it even better, I think.
Posted March 3, 2007 at 6:31 pm
Steve says:
I used to use pragmas long ago but do not use them any more. I find it more useful to have my method popup menu always be in alphabetical order. I also use cmd-double click to go to the code for a method.
Posted March 3, 2007 at 6:31 pm
Roger says:
My organization differs from yours just a little bit:
Why this organization?
The first 3: because this is the order in which I write code.
Notifications at the end because it’s usually the last part I code.
Delegates methods or table source methods are grouped by sections too.
I don’t use custom text in #pragma mark because in 100% of time, the method prototypes already highlights the position in the source code.
Posted March 4, 2007 at 6:31 pm