When writing C++ code for Carbon, we want to keep all of our code consistent, easy to learn, and help avoid spending undue code review time arguing about the same core style and idiomatic issues.
I propose to use the Google C++ style guide as a baseline, and then make minimal, focused additions and adjustments to it to suit the needs of Carbon.
The Google C++ style guide is widely used even outside of Google. Both Mozilla and Chromium use it as their base style guide. It is both comprehensive and specific, providing a strong foundation with wide coverage.
However, there are a small number of places that seem worth adjusting. I propose to use the Google style guide as a baseline and then have a minimal, and focused list of changes and clarifications.
The most expensive cases are those where we actively diverge from this baseline. Doing this outside of mechanical formatting issues would require careful thought and justification that seems likely to have a higher cost than the benefit. I propose focusing only on specific places where there is a direct benefit to Carbon by diverging. The only place where I suggest this is the naming convention. The convention suggested by Google's style guide is more complex than necessary, in part due to its desire to not invalidate a large historical codebase. That is not a concern for Carbon, and there is a specific advantage: using and learning how the proposed naming convention for Carbon itself works in practice. Thus, the proposal is to replace the naming conventions suggested by the Google style guide with those suggested for Carbon.
The remaining suggested modifications simply consist of selecting one of several allowed options in order to increase consistency and modernness. Many of the cases allowing multiple options only exist to retain consistency with an existing code base that isn't a factor for Carbon. I suggest we take advantage of this to have a more consistent and modern style. See the proposed section for details.
Some aspects of this proposal warrant specific rationale.
Advantages:
Disadvantages:
Note: this is likely a bikeshed that we should not invest significant time debating.
Advantages:
Disadvantages:
* and & with the variable in pointer declarationsNote: this is likely a bikeshed that we should not invest significant time debating.
Advantages:
int *p;, the type of
*p is int.Disadvantages:
<type> <identifier>; and
the * is part of the type.
const after the typeNote: this is likely a bikeshed that we should not invest significant time debating.
Advantages:
const alternating with pointers or references
more obviously readable.
const often
provides even better readability.Disadvantages:
const <type> <id>; is
much more conventional.
Carbon will end up heavily using LLVM and Clang for its reference implementation. This will both involve interfacing with those APIs and heavily using libraries from those projects. We could adopt the LLVM coding standards to gain consistency with those APIs.
Advantages:
Disadvantages:
A common, established style guide will allow us to focus on the more important aspects of coding and code review. Once we're familiar with the rules, their consistent application will result in a more readable codebase, and rules that can largely be automated by clang-format will result in a more efficient development process.
This particular ruleset attempts to align with our expected lexical rules for the Carbon language (for example, comment syntax and capitalization rules), which will also mean that we can use a largely consistent style between the aspects of the toolchain implemented in C++ and the aspects implemented in Carbon.