Header Guards

There are many basic programming practices that I take for granted and perform automatically.

I want to take a moment to highlight one of my fundamental C practices: adding header guards. If you're not familiar with header guards, this is the pattern:

#ifndef DISPATCH_H_
#define DISPATCH_H_

//your code goes here

#endif //DISPATCH_H_

The header guard pattern wraps the entire header contents inside an #ifndef block. If you are including the header in a context for the first time, the #ifndef check will be true and the header "contents" will be included (also #define-ing the header-guard symbol). If the header is included a second time, the #ifndef statement will no longer be true and the header will be empty instead.

One of the primary benefits of header guards is preventing you from including the same header multiple times (perhaps another header also includes it). Header guards are also useful for preventing circular include dependencies by allowing a file to only be included once.

Each header should define a different guard symbol. This symbol is usually related to the filename to reduce the odds of a collision. You must make sure that your headers do not have matching definitions!

Also, note that preceeding underscores (e.g. _DISPATCH_H) are reserved for the standard library. As a best practice, avoid using preceeding underscores.

Header guards are a fundamental programming practice - if you're not already using them, get into the habit!