Why #import Order Matters

March 25, 2013 — 8 Comments

There are subtle issues around #import order. You may not believe me — until you try reusing code in a new project.

Unfinished Bridge 1” by Jason Thomas, used under CC BY-NC-SA 2.0 / Altered by Jon Reid

In #imports gone wild! we looked at the problems caused by having too many #import directives. But it’s also possible that you have too few, resulting in bad header files — especially if you don’t pay attention to #import order in your .m files.

Minimal and complete

When it comes to imports, header files should satisfy these two conditions:

  • They should be minimal
  • They should be complete

“Minimal” just means a header file should import no more than it needs.
“Complete” means the header file imports everything that needed to compile it. Consider

#import "foo.h"
#import "bar.h"

If removing foo.h (or changing the order) causes bar.h not to compile, then bar.h is not complete.

Incomplete headers

One way to have an incomplete header is by relying on precompiled headers. In particular, just because the precompiled headers include a particular header doesn’t mean you can omit it elsewhere.

Another way to have an incomplete header is with a poor #import order that masks dependencies. In C-based languages, programmers often begin their implementation files by including the most general headers from the largest scope. They work down from there, until they reach the most specific headers:

  1. system headers
  2. other headers
  3. finally, the unit’s own header

This is backwards. Consider a header foo.h that depends on <QuartzCore/QuartzCore.h>. If foo.m imports QuartzCore first, then other stuff, then finally gets to its own header, you may not feel the need to import QuartzCore in foo.h. …And this will break for the next programmer who comes along and just imports foo.h.

Good #import order

The solution is simple: reverse the order! Start from the most specific, then work towards the most general. Most importantly, include your own header first. Large-Scale C++ Software Design by John Lakos is the only book I know about “physical design” — how to arrange source code into files. The author states,

Latent usage errors can be avoided by ensuring that the .h file of a component parses by itself – without externally-provided declarations or definitions… Including the .h file as the very first line of the .c file ensures that no critical piece of information intrinsic to the physical interface of the component is missing from the .h file (or, if there is, that you will find out about it as soon as you try to compile the .c file).

In other words, by including your own header first… if the header is not complete, you will fail fast!

Here’s what I do. If I’m writing foo.m, I first import foo.h. I keep this separated from other imports by a blank line. All the others follow, in sorted order:

#import "foo.h"

#import "abc.h"
#import "def.h"
#import &lt;Abc/Abc.h&gt;

Sorting helps me find duplicates. It also puts angle brackets imports <> after quoted imports, so that the most general headers come last.

Questions: What ordering do you use for your #import statements? What tools do you use to help with your ordering? Leave a comment below.

Disclosure of Material Connection: Some of the links in the post above are "affiliate links." This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, I only recommend products or services I use personally and believe will add value to my readers. I am disclosing this in accordance with the Federal Trade Commission’s 16 CFR, Part 255: "Guides Concerning the Use of Endorsements and Testimonials in Advertising."

Enjoyed this article? Sign up to get future articles by email.

Email Address:

8 responses to Why #import Order Matters

  1. One of the things I like about AppCode is its ability to organise imports, automatically removing unused imports or adding them when you refer to new symbols while writing. That’s great for TDD as it means you can focus on what you want to say and let the IDE handle the ceremony needed by the compiler.

    On the other hand, it considers imports that are already included in the project’s PCH to be redundant and will remove those. Using the same rules you outline above, this is not a good thing.

    • Yup. But since my precompiled headers contain only angle-bracket-style imports, and AppCode visually distinguishes those headers it thinks are redundant, I can choose to apply AppCode’s rules only to quote-style imports. Then once in a while, I do a sweep: I temporarily set my prefix not to include any headers, and let AppCode do its thing. That’s been a timesaver on big legacy projects with messy imports.

  2. Hi Jon, stumbled upon your post while surfing. Interesting post. One question about the “specific to general” ordering of header (#imported of course) though.

    Support by accident there are two #define, the first occurrence in the local (now ordered first) header which is the one the programmer wants, and the other in a framework (now ordered latter) header. Wouldn’t the preprocessor pick the definition defined in the framework instead of what the programmer wants?

    As a side note, this scenario does justify your other post (http://qualitycoding.org/preprocessor/) about #define which I agree 100% with.

    • Chris, if you have a project containing two different files but sharing the same name, you have other problems! Thankfully, the problems should be obvious if it doesn’t even compile.

  3. Thanks for the article, Jon! It exactly matches the way I organize the imports, and my reasoning is the same. The only exclusion is for the empty line after the first #import “foo.h” :-)

    I’d just wanted to add that in my case I use quite a few (own) frameworks, and there’re inter-framework dependencies. So, imports for most specific frameworks go first, then imports for less specific frameworks and etc.

    Btw, what I like about imports, is that when used properly, they reveal/show the real dependencies, so it allows to estimate the complexity of the source that uses them.

    Last thing: you can make an simple automator action with “sort | uniq” for sorting selected imports in Xcode.

  4. Thank you for writing this post. I am still pretty new to iOS development, and seeing tips like this helps me get started with the best food forward. I do occasionally program in C though, so this will also be helpful there.

    I just have one small question. Is there any functional reason for splitting the unit’s own header from the other imports with the blank line? I would guess it is just to demarcated the difference between it and the “other headers”, since they look identical (while the system ones are obviously different due to the ). Is that why? Or is it just style, like curly-braces inline or on the next line from an if-statement?

    • Nick, I like to keep the “own header” separate to emphasize its position, especially when other programmers are involved who may not know why it comes first.

Leave a Reply

*

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> 

Have you Subscribed yet? Don't miss a post!