C++ style guide

The Fuchsia project follows the Google C++ style guide, with a few exceptions.

By policy, code should be formatted by clang-format which should handle many decisions.

Exceptions

Braces

Always use braces { } when the contents of a block are more than one line. This is something you need to watch since Clang-format doesn't know to add these.

// Don't do this.
while (!done)
  doSomethingWithAReallyLongLine(
       wrapped_arg);

// Correct.
while (!done) {
  doSomethingWithAReallyLongLine(
       wrapped_arg);
}

Conditionals and loops

Do not use spaces inside parentheses (the Google style guide discourages but permits this).

Do not use the single-line form for short conditionals and loops (the Google style guide permits both forms):

// Don't do this:
if (x == kFoo) return new Foo();

// Correct.
if (x == kFoo)
  return new Foo;

Namespace Names

  • Nested namespaces are forbidden, with the following exceptions:
    • internal (when required to hide implementation details of templated code)
    • code generated by the FIDL compiler
  • The following top-level namespaces are forbidden:
    • internal
    • fuchsia (except in code generated by the FIDL compiler)
  • Namespaces in SDK libraries must be kept to as short a list as possible. A later document will provide an explicit list of allowed namespaces; in the meantime, new namespaces should be introduced thoughtfully.
  • Namespaces in non-SDK libraries should also be chosen so as to reduce the risk of clashes. Very generic nouns (eg. media) are best avoided.

Rationale: Tip of the Week #130: Namespace Naming

#includes

#includes use either <angle brackets> or "quotes" depending on context.

  1. Use "" if the path is from the root of the source tree (e.g. "garnet/foo/bar/baz.h") or if the include is in the same directory as the includer (e.g. "baz.h").
  2. Use <> otherwise, which typically means the header is a “public” header of some sort (e.g. <lib/foo/bar.h> or <zircon/foo.h>).
  • Third-party headers may be included using the root-relative path (e.g. "third_party/skia/include/core/SkPaint.h") or a public include path (e.g. <gtest/gtest.h>).
  • Public headers should use <> with their canonical path (e.g. <lib/foo/bar.h>) even if included from the same directory.
  • Non-public headers should use root-relative paths (e.g. "garnet/foo/bar/baz.h") even if included from the same directory, except in cases where this would be inconsistent with existing code (e.g. zircon).