
CRAP Score Analysis
FreeAssess code risk with CRAP scores for .NET methods.
Free · Opens the source repo
What CRAP Score Analysis does
CRAP Score Analysis is a specialized tool designed for developers working with .NET codebases. It calculates the CRAP (Change Risk Anti-Patterns) score for specific methods, classes, or files, allowing developers to identify code that is both complex and under-tested. By combining cyclomatic complexity and code coverage metrics, this skill provides a comprehensive assessment of code risk, helping teams prioritize their testing efforts effectively.
The CRAP score is calculated using a formula that considers both the cyclomatic complexity of a method and its code coverage percentage. A lower CRAP score indicates lower risk, while higher scores signal the need for more testing or code simplification. This skill is particularly useful when assessing individual methods or classes, as it can highlight areas of code that may pose a risk due to low coverage and high complexity.
Developers can utilize this skill when they need to evaluate the quality of their tests or determine which parts of their codebase require additional testing. It is an excellent resource for teams looking to improve their code quality and maintainability by focusing on the most critical areas. However, it is important to note that this skill is not intended for project-wide analysis or for writing new tests; it is strictly for assessing existing code.
To use the CRAP Score Analysis skill effectively, users must first ensure that they have collected the necessary coverage data, as the CRAP score relies on accurate coverage information. The skill guides users through the process of gathering this data and calculating the CRAP scores, making it a valuable addition to any .NET developer's toolkit.
When to use it
Use this skill when you need to assess the risk of specific methods or classes in your .NET codebase based on their complexity and test coverage.
When not to use it
This skill is not suitable for running tests or writing new tests; it is focused solely on analyzing existing code risk.
What you can build with it
Assessing Method Risk
Use this skill to calculate the CRAP score of a specific method to determine if it requires more tests.
Prioritizing Testing Efforts
Identify high-risk methods in your codebase and prioritize them for additional testing based on their CRAP scores.
Evaluating Test Quality
Analyze existing tests to see how well they cover complex methods, helping improve overall code quality.
How to install CRAP Score Analysis
View source1. Install with the skills CLI
npx skills add dotnet/skills/crap-score --agent claude-code2. Or install it manually
Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.
Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs
Inside SKILL.md
Written by dotnetCRAP Score Analysis
Calculate CRAP (Change Risk Anti-Patterns) scores for .NET methods to identify code that is both complex and undertested.
Background
The CRAP score combines cyclomatic complexity and code coverage into a single metric:
$$\text{CRAP}(m) = \text{comp}(m)^2 \times (1 - \text{cov}(m))^3 + \text{comp}(m)$$
Where:
- $\text{comp}(m)$ = cyclomatic complexity of method $m$
- $\text{cov}(m)$ = code coverage ratio (0.0 to 1.0) of method $m$
| CRAP Score | Risk Level | Interpretation |
|---|---|---|
| < 5 | Low | Simple and well-tested |
| 5-15 | Moderate | Acceptable for most code |
| 15-30 | High | Needs more tests or simplification |
| > 30 | Critical | Refactor and add coverage urgently |
A method with 100% coverage has CRAP = complexity (the minimum). A method with 0% coverage has CRAP = complexity^2 + complexity.
When to Use
- User wants to assess which methods are risky due to low coverage and high complexity
- User asks for CRAP score of specific methods, classes, or files
- User wants to prioritize which code to test next
- User wants to evaluate test quality beyond simple coverage percentages
When Not to Use
- User just wants to run tests (use
run-testsskill) - User wants to write new tests (use
writing-mstest-testsskill or general coding assistance) - User only wants a coverage percentage without complexity analysis
Inputs
| Input | Required | Description |
|---|---|---|
| Target scope | Yes | Method name, class name, or file path to analyze |
| Test project path | No | Path to the test project. Defaults to discovering test projects in the solution. |
| Source project path | No | Path to the source project under analysis |
Workflow
Step 1: Collect code coverage data
If no coverage data exists yet (no Cobertura XML available), always run dotnet test with coverage collection first and mention the exact command in your response. Do not skip this step -- CRAP scores require coverage data.
Check the test project's .csproj for the coverage package, then run the appropriate command:
| Coverage Package | Command | Output Location |
|---|---|---|
coverlet.collector | dotnet test --collect:"XPlat Code Coverage" --results-directory ./TestResults | Typically under TestResults/<guid>/coverage.cobertura.xml. Search recursively under the results directory (for example, TestResults/**/coverage.cobertura.xml) or use any explicit coverage path the user provides. |
Microsoft.Testing.Extensions.CodeCoverage (.NET 9) | dotnet test -- --coverage --coverage-output-format cobertura --coverage-output ./TestResults | --coverage-output path |
Microsoft.Testing.Extensions.CodeCoverage (.NET 10+) | dotnet test --coverage --coverage-output-format cobertura --coverage-output ./TestResults | --coverage-output path |
Never estimate coverage
Guessed coverage produces wrong CRAP scores, which is worse than no answer. If the first command yields no Cobertura XML, work down this list before giving up:
- Add a provider if none is referenced:
dotnet add <test.csproj> package coverlet.collector, then re-run. - Use the standalone collector, which works even when the test host or a shared assembly blocks the in-proc collector:
dotnet tool install --global dotnet-coveragethendotnet-coverage collect -f cobertura -o coverage.cobertura.xml "dotnet test <test.csproj>". - Convert or summarize an existing report with ReportGenerator when only binary
.coverageoutput exists:dotnet tool install --global dotnet-reportgenerator-globaltoolthenreportgenerator -reports:<file> -targetdir:cov -reporttypes:Cobertura. - Tests fail but still run? Coverage is collected from the tests that executed — continue with that data and note the failures.
If every path fails, report that coverage could not be collected, show the commands you tried and their errors, and stop. Report complexity on its own if useful, but never publish a CRAP number derived from an assumed coverage percentage.
Step 2: Compute cyclomatic complexity
Analyze the target source files to determine cyclomatic complexity per method. Count the following decision points (each adds 1 to the base complexity of 1):
| Construct | Example |
|---|---|
if | if (x > 0) |
else if | else if (y < 0) |
case (each) | case 1: |
for | for (int i = 0; ...) |
foreach | foreach (var item in list) |
while | while (running) |
do...while | do { } while (cond) |
catch (each) | catch (Exception ex) |
&& | if (a && b) |
|| (OR) | if (a || b) |
?? | value ?? fallback |
?. | obj?.Method() |
? : (ternary) | x > 0 ? a : b |
| Pattern match arm | x is > 0 and < 10 |
Base complexity is 1 for every method. Each decision point adds 1.
When analyzing, read the source file and count these constructs per method. Report the breakdown.
Step 3: Extract per-method coverage from Cobertura XML
Parse the Cobertura XML to find each method's line-rate attribute under the target <class> element. If line-rate is not available at method level, compute it from the <lines> elements:
$$\text{cov}(m) = \frac{\text{lines with hits} > 0}{\text{total lines}}$$
Method names in Cobertura may differ from source (async methods, lambdas). Match by line ranges when names don't align.
Step 4: Calculate CRAP scores
For each method in scope, apply the formula:
$$\text{CRAP}(m) = \text{comp}(m)^2 \times (1 - \text{cov}(m))^3 + \text{comp}(m)$$
Step 5: Present results
Present a sorted table (highest CRAP first):
| Method | Complexity | Coverage | CRAP Score | Risk |
|---------------------------------|------------|----------|------------|----------|
| OrderService.ProcessOrder | 12 | 45% | 28.4 | High |
| OrderService.ValidateItems | 8 | 90% | 8.1 | Moderate |
| OrderService.CalculateTotal | 3 | 100% | 3.0 | Low |
Include:
- Summary: total methods analyzed, how many in each risk category
- Top offenders: methods with CRAP > 30, with specific recommendations
- Quick wins: methods with high complexity but where small coverage improvements would drop the score significantly
Step 6: Provide actionable recommendations
For high-CRAP methods, suggest one or both:
- Add tests -- identify uncovered branches and suggest specific test cases
- Reduce complexity -- suggest extract-method refactoring for deeply nested logic
Calculate the coverage needed to bring a method below a CRAP threshold of 15:
$$\text{cov}_{\text{needed}} = 1 - \left(\frac{15 - \text{comp}}{\text{comp}^2}\right)^{1/3}$$
This formula only applies when comp < 15. When comp >= 15, the minimum possible CRAP score (at 100% coverage) is comp itself, which already meets or exceeds the threshold. In that case, coverage alone cannot bring the CRAP score below the threshold -- the method must be refactored to reduce its cyclomatic complexity first.
Report this as: "To bring ProcessOrder (complexity 12) below CRAP 15, increase coverage from 45% to at least 72%." For methods where complexity alone exceeds the threshold, report: "ComplexMethod (complexity 18) cannot reach CRAP < 15 through testing alone -- reduce complexity by extracting sub-methods."
Validation
- Verify that coverage data was collected successfully (Cobertura XML exists and contains data)
- Confirm every coverage figure came from that XML — no estimated, assumed, or source-comment-derived values
- Cross-check that method names in coverage data match the source code
- Confirm CRAP scores by spot-checking the formula on one method manually
- Ensure a 100%-covered method's CRAP equals its complexity exactly
Common Pitfalls
- Estimating coverage when collection fails: never do it — the resulting CRAP scores are wrong in the direction that matters. Work through the fallbacks in Step 1, then report the blocker instead.
- Trusting a stale complexity comment in the source: compute cyclomatic complexity from the current code; a
// complexity: 7comment left by a previous author is not evidence. - Giving up on a shared-assembly or test-host collector error:
dotnet-coverage collectruns out of process and usually succeeds where the in-proc collector fails. - Stale coverage data: Always regenerate coverage before computing CRAP scores. Old coverage files will produce misleading results.
- Method name mismatches: Cobertura XML may use mangled/compiler-generated names for async methods, lambdas, or local functions. Match by line ranges when names don't align.
- Generated code: Exclude auto-generated files (e.g.,
*.Designer.cs,*.g.cs) from analysis unless explicitly requested.
Frequently asked questions about CRAP Score Analysis
Similar skills
Quality Playbook Generator
Run comprehensive quality audits on any codebase.
PR Draft Summary
Automate PR summary generation for openai-agents-python.
Final Release Review
Streamline your release candidate audits with ease.
Unit Test Vue Pinia
Efficiently write and review unit tests for Vue 3 applications.
Slang Shader Expert
Optimize and integrate Slang shaders with ease.
Telemetry Standards
Ensure consistent event tracking in Supabase Studio.
