crap4csharp continues the lineage of Robert C. ("Uncle Bob") Martin's original crap4clj, and is
a C# port of its Java sibling crap4java.
crap4csharp is a standalone CRAP metric tool for C# projects, modeled after crap4java.
It combines method cyclomatic complexity with Coverlet (Cobertura) method coverage and reports CRAP scores. On each run it deletes stale coverage artifacts, runs coverage, then analyzes the selected files.
CRAP = CC^2 * (1 - coverage)^3 + CC
CCis cyclomatic complexity.coverageis method coverage fraction from Cobertura line counters (the .NET analog of JaCoCoINSTRUCTIONcounters).
For each invocation, per module (nearest .sln, else .csproj, else the project root):
- Delete stale coverage artifacts:
coverage/
- Run
dotnet test --collect:"XPlat Code Coverage" --results-directory coverage - Read the produced
coverage.cobertura.xml - Analyze the selected C# files
dotnet testBuild:
dotnet build -c ReleaseFrom the project root you want to analyze:
dotnet run --project src/Crap4CSharp -c Release--help Print usage to stdout
(no args) Analyze all C# files under src/
--changed Analyze changed C# files under src/
<file ...> Analyze only these files
<directory ...> Analyze all C# files under each directory's src/ subtree
Examples:
dotnet run --project src/Crap4CSharp -c Release -- --help
dotnet run --project src/Crap4CSharp -c Release
dotnet run --project src/Crap4CSharp -c Release -- --changed
dotnet run --project src/Crap4CSharp -c Release -- src/Sample.cs
dotnet run --project src/Crap4CSharp -c Release -- project-a project-b0success, threshold respected1invalid CLI usage, or a fatal error (e.g. no tests ran / no coverage produced — see Notes)2CRAP threshold exceeded (> 8.0)
- Fail fast: if a module runs no tests or produces no coverage,
crap4csharpexits non-zero rather than continuing — a deliberate, stricter departure fromcrap4java. A method simply absent from an otherwise-populated report is still reported asN/A. - Report output is sorted by CRAP descending, with
N/Aat the bottom.