The Elements of Programmatic Style

Coding Style seems a perennial topic among computer program writers, often leading to fierce debates on one style or another. These style preferences can differ immensely, and it seems everybody has their own take on what's good style.

Over the past few years, I've developed my own, internalized Style Guide for iOS and Mac Cocoa development, and recently I decided to formalize these rules into an excruciatingly detailed guide. I present The Elements of Programmatic Style. The guide details all of my Objective C style and idioms, from naming conventions to whitespace to project settings and everything in between.

As you read through it, you may very well disagree with some of the guidelines I've laid out. In fact, I hope this is the case. This guide is for me and the people I work with professionally, and it may not apply entirely to you. But it's a starting point, and a great document to reference as a starting point for discussion among my contemporaries. Most of this document is even up for debate, as the language evolves, so too will this document. Objective C is currently in a mutant state.

The one aspect of the document not up for change, and this is the root of much debate, are the guidelines for whitespace. My code keeps bare lines between logical chunks of code, and extra (two minimum) between methods or function definitions. Whitespace is free, but more importantly, it greatly affects readability of code. When I see two bare lines, I know this indicates a logical change in the code.

It goes beyond just grouping chunks together or pure aesthetics. Vertical rhythm is essential for legibility (you'll notice this page even pays particular attention to the line spacing of textual and bare lines). This principle has been in use for centuries, and it's all in the name of readability. Our code deserves to be just as readable, if not more, than any other normal text. If you're interested in the subject, I'd highly recommend both this book and this adaptation of it for the web (which appears down for the moment…hmm).

Having a style guide, whether internalized or explicitly documented is important. Whatever your opinion is over white space or the place of a brace, we can all agree consistency trumps all. If you're considering creating your written style document, feel free to use mine as a starting point. Just be sure to tell me how yours differs!

Speed of Light