Isurwars/Correlation / .agents/skills/refactor-complexity
refactor-complexity
UnverifiedGuide architectural refactoring to keep function cognitive complexity strictly below threshold (<= 25). Decomposes monolithic functions into modular subroutines and parameter structs without inline suppressions. Use when resolving clang-tidy cognitive complexity warnings.
Cognitive Complexity Refactoring
This skill directs the detection, diagnosis, and systematic refactoring of functions that trigger readability-function-cognitive-complexity violations in clang-tidy.
1. Quality Threshold & Guardrail
- Cognitive Complexity Target: $\le 25$.
- Rule Enforcement: Zero
NOLINTorNOLINTNEXTLINEsuppressions allowed. - Target Files: All C++ header, implementation, and CUDA/HIP files (
*.hpp,*.cpp,*.cu,*.cuh).
2. Refactoring Patterns
Pattern A: Parameter Object Extraction
Replace functions taking 6+ related loose variables or returning multiple outputs with a typed struct:
template <typename T>
struct SpatialPartitionData {
GPUSearchGrid grid{};
std::vector<T> wrapped_x;
std::vector<T> wrapped_y;
std::vector<T> wrapped_z;
std::vector<int> element_ids;
std::vector<int> atom_bin;
std::vector<unsigned long long> bin_offsets;
std::vector<unsigned long long> bin_indices;
size_t num_bins{0};
bool ignore_periodic_self_interactions{false};
};
Pattern B: Pipeline Stage Decomposition
Extract discrete pipeline stages into dedicated anonymous namespace helpers:
prepare_*()orbuild_*()— Preprocessing and data structure construction.check_*()orhas_*()— State and condition inspection.execute_*()— Core computation or kernel dispatch.unpack_*()orformat_*()— Result collation and tensor transformation.
Pattern C: Flattening Deep Control Flow
- Replace nested ternary operators with clear structured branches or lookup tables.
- Use early returns/guard clauses to reduce indentation nesting levels.
- Extract nested loops inside conditional branches into dedicated helper functions.
3. Step-by-Step Workflow
- Diagnostic Scan: Run
clang-tidyto identify line numbers and exact nesting penalties:clang-tidy -p build path/to/file.cpp - Decompose: Identify the top 2-3 nesting clusters and extract them into single-responsibility functions.
- Internal Linkage: Place helper functions in the anonymous
namespace { ... }or mark themstaticto satisfymisc-use-internal-linkage. - Const Correctness & Member Initialization: Ensure all newly extracted structs have default member initializers and const-correct parameters.
- Verify: Re-run
clang-tidyandcorrelation_unit_teststo confirm clean compilation and zero warnings.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementation | Stars: 318k | Updated | Claude CodeCodex | Unverified | Install |
| requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirements | Stars: 318k | Updated | Claude CodeCodex | Unverified | Install |
| using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallback | Stars: 318k | Updated | Claude CodeCodex | Unverified | Install |
| verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions always | Stars: 318k | Updated | Claude CodeCodex | Unverified | Install |