Code style

From carbon-language/carbon-lang · View interactive version

Code style is an AI agent skill from the GitHub repository carbon-language/carbon-lang. Instructions for code formatting and style guidelines in the Carbon toolchain.

How to use this skill

Skill file

Code style

<!--

Part of the Carbon Language project, under the Apache License v2.0 with LLVM

Exceptions. See /LICENSE for license information.

SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

-->

License

license following

CONTRIBUTING license instructions.

Formatting

Bazel files.

files.

Instead, try to format Carbon code based on other Carbon files and the C++

style.

markdown files.

files.

Comments

current code does or why it is the way it is, not narrate what the code used

to be. Do not add a comment to justify a deletion or explain that something

is no longer necessary; a reader of the new code has no idea what is being

contrasted against, and the comment rots as soon as the old shape is

forgotten. Put that reasoning in the commit message instead.

argument reads clearly on its own, inline it rather than naming it so that a

comment has somewhere to attach.

of what the function does or returns, such as "Returns true if the InstKind

is a singleton." Put nuance in a following sentence rather than qualifying

the summary into something harder to read.

changes meaning, a comment that mentions it is not automatically stale. Work

out what the comment actually asserts first: it may be describing what the

code _uses_, which is still true, and rewriting it loses information.

Documentation

This covers standalone prose: /docs, README.md files, and the skill files

under .agents/skills.

commit as the code it describes should name that code freely. Documentation

that lands separately must not: a reader who greps for an identifier from an

unlanded change finds nothing, and a claim that only holds after that change

is false until it lands. This is easy to get wrong when writing up a lesson

while the change that taught it is still in flight.

shape, name it Foo rather than reaching for a real symbol. Save real

identifiers for documenting that identifier, where going stale is at least

detectable.

produced it. State the rule, and enough of the reason to apply it, without

assuming knowledge of the change that motivated it.

Naming

invariant a name describes, rename it in the same change. A factory called

MakeSingletonFooId has to be renamed once Foo is no longer a singleton,

even though nothing forces you to.

of a namespace should return InstId, not TypeInstId, because a namespace

is not a type. Do not let a convenient wider or narrower id type imply

something false.

widens a test so that it no longer says what its name implies, remove it and

let callers use the underlying test, rather than keeping a wrapper that

sounds meaningful. What callers actually want is often the inverse, and is

worth adding under its own name.

value plays is unchanged, keep its name even when a change means it is now

held or spelled differently.

Commit descriptions

code. Only describe process when the process is more interesting than the

change, as with a large mechanical transformation.

audited" or "we investigated every caller" describe effort, not the change.

autoupdater or the formatter. Every change is expected to include those.

listing every function or file touched; the diff already lists them.

codebase uses. Calling something a "type alias" or a "namespace" when it is

a named scope misleads, and is worse than a vaguer but accurate word.

rather than a broader statement that happens to contain it.

Design

output is undesirable, change the printer, not the data structures that

feed it.

Style Guides

Carbon C++ Project Style Guide.

Google developer documentation style guide.

style guide.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.