Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

tutorial convention

Tianshu Huang edited this page Oct 4, 2018 · 1 revision

Convention and Standard Practices

Headers and Include Guards

Suppose you have the following program structure:

main.h
|
|--foo.h--+
+--bar.h--|
          +--baz.h

Where both foo.h and bar.h depend on baz.h. If both foo.h and bar.h #include "baz.h", you'll get an error complaining about redefinition of already defined names; if one of them includes baz.h, but not the other, the compiler will sometimes complain about undefined names, depending on the compile order (which can't always be controlled).

The solution? Something called an include guard.

At the beginning and end of every .h file (such as example_file.h here), the following lines should be present:

#ifndef EXAMPLE_FILE_H
#define EXAMPLE_FILE_H

/* code here */

#endif

When the compiler reaches this file, it reads #ifndef. This is called a compiler macro. If EXAMPLE_H is already defined, then the compiler ignores the file (ifndef <=> if not defined).

Style

Please, for the love of god or lack thereof, use consistent style.

As a starting point, try Google's C++ Standards (C is subset of C++, so style for C++ will still work with C) or Region V's Style & Standards.

General recommendations:

  • 4 or 2 spaces per indentation
  • Max 79 characters per column
  • Keep files to under 250 lines, and split files into multiple when that gets exceeded
  • Include guards. Always.
  • Keep a consistent function header format, and make sure every function has one.
Clone this wiki locally