Code formatting

In this post, I’ll give a quick overview on choosing a personal coding style, and present a way to auto-format code in bulk.

If you take your code seriously, you probably have a code style that you consistently use. For example, maybe you always use braces with every if clause, or you use underscores in instance variable names but not local variables.

Style Guides

If you work with other coders, it’s good practice to have an agreed-on general style. Google engineers have been doing this carefully for a while now, and they’re even nice enough to publish their own style recommendations for many languages, including Objective-C. I don’t agree with every guideline Google suggests (for example, the 80 char line limit), but it’s very good to consider their perspective.  One thing I wish they did (and that I’ve seen in other style guides) is to give an explanation for each style decision; otherwise the rules may feel arbitrary and unintuitive.  If you’re developing a personal or company style guide, make sure to refer to Apple’s suggestions as well!

Google’s Objective-C Style Guide

Apple’s Coding Guidelines for Cocoa

Autoformatting

If you’re just catching up on good style, you may have a lot of source that suddenly looks poorly formatted to you.  Fear not!  You can clean it up automatically using a couple tools. The first is Xcode itself, which knows a fair amount about how to indent those lines.  Press command-comma in Xcode to open the Preferences window, mosey on over to the Indentation section, and set your options.

To auto-indent a section of code, select it (command-A to select all) and press control-I (that’s control-not-command + i).

But Xcode doesn’t handle advanced formatting details, such as enforcing consistent spacing between various elements.  To do a good job with this, you can use the uncrustify tool that comes with UniversalIndentGUI.  After trying out this tool for a while, I decided that I prefer to use uncrustify directly from the command-line.  The GUI seems slightly buggy.  You can autoformat a source file with a command-line like this:

/Applications/UniversalIndentGUI/indenters/uncrustify -c uncrustify.cfg -lOC -f MyFile.m -o MyFile.m

Of course, you may have to edit the paths appropriately for that to work in your setup.  I’ve added a sample uncrustify.cfg config file to the moriarty library.  This file contains all the specific details about how you want your code formatted.  The most concise documentation I was able to find on the format of this file is in the comments of this sample cfg file.

In addition, it turns out there are ways to incorporate command-line tools directly into Xcode.  In theory, legend has it that you can set up a custom user script (these are under the scroll-looking icon in Xcode’s menu) to be able to use uncrustify to auto-format selected code with a keyboard shortcut within Xcode.  I was not able to actually get this to work, but if you are feeling brave, here is the blog post where I first heard such tales.

Time to make some beautifully formatted code!

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*