iOS Coding Conventions

Even for someone who thinks code, like art, is a form of expression and should reflect the characters of its creator, there is no denying that coding convention is a necessary evil for effective collaboration, especially for shipping production code. So far, my practice has been to cheery pick what makes sense from a number of recommendations, some of which are listed below:

http://abizern.org/cocoa-coding-conventions/

http://www.cimgf.com/zds-code-style-guide/

https://github.com/NOUSguide/Coding-Conventions

http://google-styleguide.googlecode.com/svn/trunk/objcguide.xml

Somehow I get the occasional feeling that my output lacked organization and discipline. Take this class for example, should those init logic really be crammed inside viewDidLoad? And certainly there must be a way to organize initViews into smaller, more manageable chunks? A coding convention I began to follow in the past month, introduced by @samgrossberg and @kompfner, turned out to be a great help.

While yet to be convinced about avoiding NIBs and using AutolayoutShorthand over VFL, code written to this convention does *read* much better. At first glance, the nitty gritty suggestions in the document border on OCD (case in point – import statements sorted alphabetically, really?), but the up side is, while reading through a project’s worth of code written in this style, you find yourself getting into the gist of the code much faster as your brain gets quickly accustomed to the pattern. This consistency surely does have great productivity yields down the road and makes it worth the effort during coding.

I’ll put up a before and after comparison of code written in my old style and this new one to illustrate the difference. Meanwhile, do take a moment to read through the convention below. It’s available as a public gist on https://gist.github.com/kenshin03/7963568.

Do you have a favorite Objective-C coding convention? Much appreciated if you could share them here or via @kenshin03 on twitter.

About these ads

1 Comment

  1. Pingback: iOS Coding Conventions – a refactoring case study | Cogito Ergo Sum

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s