[ Space Commader]: Take Command of Objective-C Whitespace

A set of tools for teams to setup, customize, and enforce Objective-C formatting.


Written by Alan Fineberg.

Square is releasing [ Space Commander] on GitHub. [ Space Commander] is a set of tools we use to setup, customize, and enforce Objective-C formatting across our Register iOS team, and reclaim time that would be spent manually formatting code.

Nitpick No More

Manually fixing code formatting issues can burn a lot of development cycles that are better spent elsewhere — but the alternative is a “broken windows” situation, as inconsistencies mount and readability decreases. Enter automated code formatting!

To date, we’ve automated the formatting of over 3500 distinct Objective-C files and removed the need to comment on formatting from over 1200 pull requests and counting, an estimated time savings of (at least) 200 developer hours.

Space Commander Has Landed

clang-format, provided by LLVM, is a tool that we’re excited about at Square, since it can automatically fix code formatting. It does a lot, yet we needed additional tools to fit our needs of formatting Objective-C code to our particular specification, and coordinating a team of developers easily. That’s where [ Space Commander] comes in.

[ Space Commander] provides:

  • A script to automatically format files that have changed (according to git) using clang-format.

  • Hooks to add custom formatting rules that clang-format doesn’t currently provide, with provided examples, including:

  • Adding an extra newline between class definitions (so there are 2 newlines).

  • A newline at the end of files.

  • Pre-formatting Objective-C code so that clang-format will produce the best result.

  • Scripts to add pre-commit checks, which will fail for unformatted code, and helpfully provide instructions for how to fixup code with a single command.

  • Simplified coordination of formatting rules that could change over time for an entire team, without any ongoing overhead.

  • …as well as testing scripts and fixtures to prevent regressions as the rules evolve.

  • After an initial setup script is run on a repository, formatting will be checked at each commit.

  • Formatting updates that are deployed will automatically be applied without any extra steps (after the aforementioned setup script is run).

  • We also provide an example script that can be run as part of a build process to enforce formatting at the time a pull request is made.

Blast Off

[ Space Commander] has brought our iOS team into the future of automatically formatted code files. We’re happy to hand over the “command” of where spaces, tabs, braces, etc. go in our Objective-C files, so we can ship pristine code faster.

You can clone [ Space Commander] in order to use Square’s code style, or fork it to customize the formatting rules to your own liking.

We hope [ Space Commander] brings you pristine code and saves you time, and of course we welcome contributions of improvements!

This post is part of Square’s “Week of iOS” series. Alan Fineberg - Profile Debt: The First 5000 Years made a big impression on me so I eagerly scooped up The Utopia of Rules by David Graeber. It…medium.com