Objective-C iOS Code Style Guidelines (Part #1)

I move around on different projects a lot and work with different developers every few months. One thing I’ve noticed is people picking up bad habits from blogs and Stack Overflow posts that get very popular despite their quality (this is not a blanket statement of course)… On the other hand however, I’m able to share experiences with other developers and pickup some good tips in the process. One bone picking point is always what convention to use, when, where and most importantly… Why!

Something i’ve been asked for a few times recently and something I continually tweak as my own preferences and style changes is my Objective-C iOS Code Style Guideline doc. I thought i’d share it here for everyone to use, love and consume. All I ask is that you don’t steal it as your own and that you perform any discussion on twitter using #mike.kz or message me @italoarmstrong 🙂

These guidelines of course are my own personal preferences, which have developed over many years and will continue to change, even as writing this point, I changed my mind on a few points in the below guide. So take inspiration from it, rather than thinking “this is correct way of doing it” as its not. Thats usually the point in a “guideline”.

 

Always use @property declarations for reference variables. This makes memory management much more simple.

@property(strong,nonatomic) IBOutlet UIView *mainView;
@property(weak,nonatomic) IBOutlet UIView *someSubview;
@property(weak,nonatomic) id  delegate;
@property(weak,nonatomic) id  delegate;
@property(assign,nonatomic) BOOL isItTrue;

 

Use @synthesize only with readonly properties or if you override one of the getter or setter. Rely on Auto-Synthesis in most cases.
@synthesize httpClient, connectionBar;

@synthesize keyboardResponderView = _keyboardResponderView;

 

Always use foundation types where available. Although many are simple mapped macro’s, it supports self-documenting code and interacts better with apple frameworks
int numberOfRetweets;

NSUInteger numberOfRetweets;

 

Thats it for now… more to come soon 🙂

 

 

Leave a Reply