clean-code-cpp
Skill1 starsChanged 3 months agoMITView on GitHub
Use when writing or reviewing C++ code — deeper idioms beyond the basics (ownership, noexcept/constexpr discipline, span/string_view, error-handling policy, concrete-before-abstract).
In this repository9 other files it gives its agents.
- agent-orchestrationSkill
- clean-code-audioSkill
- clean-code-bashSkill
- clean-code-coreSkill
- clean-code-cSkill
- clean-code-goSkill
- clean-code-pythonSkill
- clean-code-rustSkill
- clean-code-tsSkill
The file
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 16
Prefer std::unique_ptr + raw *non-owning* pointers/references.
- Very specific: line 33
Call-order rules as parameters: a function that needs the mutex held takes the lock (void flush(const std::scoped_lock<std::mutex>&)), a …
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Clean-code C++ (depth)
Apply on top of the project's base rules (C++20/17, CMake+CPM, clang-tidy, sanitizers, Catch2).
Error handling
- Pick one policy per module and state it: exceptions, or
std::expected/error codes. - Exceptions are fine on the general/message path. Never let exceptions cross a real-time/audio callback.
- Exception messages carry full context (offending value + expected shape).
Ownership & lifetime
- Prefer
std::unique_ptr+ raw non-owning pointers/references. Reach forstd::shared_ptronly when ownership is genuinely shared — it is not a default. - Make ownership legible: one owner, clear handoff. Rule of zero; let RAII types manage resources.
const / noexcept / constexpr
const-correct by default;constexprwhat can be evaluated at compile time.- Mark leaf functions
noexceptwhen they truly can't throw (moves, swaps, small getters) — it enables optimizations and documents intent.
Views & interfaces
std::span/std::string_viewfor non-owning views; take them by value in parameters.- Concrete before abstract: don't introduce templates, interfaces, or virtual layers until a second concrete case demands them.
Loops & algorithms
- A loop with a non-trivial body becomes a named function or a standard algorithm (
std::ranges::find_if,std::lower_bound,std::accumulate). Hand-written binary searches and case-folding loops are where the bugs hide. - Visiting many results: take a callback (
for_each_permutation(s, fn)) or return a range, instead of filling a hugestd::vectorthe caller may only stream through.
Types that carry proof
- Invariants as thin wrapper types:
NormalizedVec3is built only throughnormalize()and converts implicitly back toVec3. Callees stop re-checking, and overloads can skip work the invariant already did. - Call-order rules as parameters: a function that needs the mutex held takes the lock (
void flush(const std::scoped_lock<std::mutex>&)), a receipt the caller cannot fake. It also catches the unnamed-lock bug, wherestd::scoped_lock{m};unlocks on the same line. - Strong types (
Meters,Seconds) where two baredoubleparameters invite a swap; weigh the boilerplate first.
Signals it's going wrong
- A
shared_ptryou can't explain the sharing for → probably aunique_ptr+ reference. - A template with one instantiation → make it concrete.
- A
try/catchin an inner loop → wrong layer for error handling. - A comment saying "must hold the lock" or "call A first" → make it a parameter.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

