Coding Styles and Standards

by

One of the main reasons for having coding standards is to keep your code readable by everyone. By enforcing standards and formatting, the code base becomes consistent, and anyone can easily understand the structure of the code because he will be more familiar with what to expect. It is also very useful when a new developer joins the team because once he is familiar with the patterns, he will be able to easily read the existing code, which results in a more pleasant experience.

In this example, I will define coding standards for an iOS project (which uses Objective-C). We will take advantage of the fact that code formatting can be automated in XCode by using a plugin. The coding style that I chose is based on the Chromium style guide, but I made a few modifications, which can be found in the .clang-format (an example of one I’m using on my current project) file. Feel free to make changes as you see fit; All of the options are defined here LLVM Coding Standards.

Installation
1. Install Alcatraz, the package manager for XCode.
`curl -fsSL https://raw.github.com/supermarin/Alcatraz/master/Scripts/install.sh | sh`
2. Select `Package Manager` from the `Window` menu and search for `ClangFormat` and install that plugin.
3. Select `Clang Format` from the `Edit` menu, and click on `File`. This will load formatting settings from the `.clang-format` file that is located in the repository.
4. When you want to format a file, go to the `Clang Format` menu and select `Format File in Focus`. You can also enable `Enable Format on Save` to have the plugin automatically format the file when you save it. You can also bind formatting to a keyboard shortcut, refer to the ClangFormat homepage for more options.

Additional Standards

The automatic code formatter does not implement all the standards we would like to follow. There are some standards that need to be enforced by the developer himself because they can’t be automated. Please refer to https://github.com/paulsfds/objective-c-style-guide to see some examples.

Other Languages

Coding standards are universal and there exists some popular ones for other languages. I would also suggest searching on Google for popular standards for your favorite language. Also, many well established companies such as Google have their own coding standards that are available to the public. Here are a few popular ones to take a look at:

JavaScript

Ruby

Objective-C

C++

What are your favorite coding styles and standards?

2 Comments

  1. Justin Beck on said:

    Nice post. I’ve been developing iOS apps for about 3 years and never actually knew that you could introduce formatting using a plugin like this.

    Much like my .gitconfig, which I’ve lovingly curated over the years, I imagine myself taking the time to create a .clang-format that fits with the way my colleagues and I work. Glad I stumbled upon you guys during my workday today.

Leave a Reply

Your email address will not be published. Required fields are marked