| name | complexity-analyzer |
| description | Analyze C# code complexity to identify methods that need testing. Use when you need to understand code complexity or prioritize testing efforts. (project, gitignored) |
Code Complexity Analyzer Skill
This skill analyzes C# code to calculate cyclomatic complexity and identify which methods need testing most urgently.
Tool Location: D:\FractalDataworks\RoslynTools\src\RoslynTools.ComplexityAnalyzer
When to Use This Skill
- Before writing tests - to identify which methods to prioritize
- During code review - to find overly complex methods
- When refactoring - to understand current complexity
- When asked "which methods need testing?"
- When asked "what's the complexity of this code?"
- When asked to analyze complexity by namespace, project, directory, or class
What This Skill Does
- Calculates cyclomatic complexity for each method in a C# file or directory
- Prioritizes methods based on complexity (CRITICAL > 15, HIGH > 10, MEDIUM > 5, LOW ≤ 5)
- Groups results by namespace, project, directory, or class
- Identifies testable methods (complexity > 1)
- Excludes trivial methods (auto-properties, simple constructors)
- Generates priority report for testing efforts
API Endpoint
POST /analysis/complexity
Request:
{
"projectFilter": "MyProject",
"groupBy": 1
}
Response:
{
"isSuccess": true,
"data": {
"methods": [
{
"methodName": "ProcessPayment",
"className": "OrderService",
"namespace": "MyApp.Services",
"complexity": 18,
"priority": "CRITICAL"
}
]
}
}
GroupBy Options:
0= ByNamespace (default)1= ByProject2= ByDirectory3= ByClass
How Complexity is Calculated
Cyclomatic complexity counts decision points in code:
- Base complexity: 1
- Each
ifstatement: +1 - Each
while/for/foreachloop: +1 - Each
switchcase: +1 - Each
catchclause: +1 - Each ternary operator
? :: +1 - Each
&&or||operator: +1
Example:
public bool ValidateOrder(Order order) // Complexity = 4
{
if (order == null) // +1
return false;
if (order.Items.Count == 0) // +1
return false;
if (order.Total < 0) // +1
return false;
return true;
}
Priority Levels
- CRITICAL (complexity > 15): Must have comprehensive tests
- HIGH (complexity > 10): Should have thorough tests
- MEDIUM (complexity > 5): Needs basic test coverage
- LOW (complexity ≤ 5): Simple tests sufficient
How It Works
1. Start the Server
dotnet run --project D:\FractalDataworks\RoslynTools\src\CyberdineDevelopment.RoslynTools.Server
2. Load Solution
using CyberdineDevelopment.RoslynTools.Client;
var client = new RoslynWorkspaceClient("http://localhost:5000");
// Load your solution
var loadResult = await client.LoadSolution(@"D:\MyProject\MyProject.sln");
if (!loadResult.IsSuccess)
{
Console.WriteLine($"Error: {loadResult.ErrorMessage}");
return;
}
Console.WriteLine($"Loaded {loadResult.Data.ProjectCount} projects");
3. Analyze Complexity
// Analyze complexity grouped by namespace
var result = await client.AnalyzeComplexity(
projectFilter: "MyProject.Core", // Optional: filter to specific project
groupBy: ComplexityGrouping.ByNamespace
);
if (result.IsSuccess)
{
foreach (var method in result.Data.Methods.OrderByDescending(m => m.Complexity))
{
Console.WriteLine($"{method.Priority,8} {method.Complexity,3} {method.MethodName}");
}
}
4. Workflow
When this skill is invoked:
- Ensure server is running (start if needed)
- Load solution into workspace
- Call complexity analysis endpoint with grouping preference
- Parse and present results showing:
- Area-based summary (namespace/project/directory/class)
- Average and max complexity per area
- Critical/High/Medium method counts
- Hottest areas needing attention
- Recommend which areas/methods to test first
Semantic Analysis Benefits
Using the Server API provides significant advantages over CLI tools:
- Full ISymbol Information: Access to complete type information from semantic model
- Cross-Project Analysis: Analyze complexity across multiple projects in a solution
- Type Resolution: Understand generic types, inheritance, and interfaces
- In-Memory Workspace: Fast, no file locking issues
- Incremental Updates: Modify code and re-analyze without reloading
- Better Accuracy: Semantic understanding vs syntax-only parsing
Output Formats
Single File
=== CODE COMPLEXITY ANALYSIS ===
Total Methods: 15
Need Testing: 12
Can Skip: 3
PRIORITY METHODS FOR TESTING:
Priority Complexity Lines Method Class
--------------------------------------------------------------------------------
CRITICAL 18 45 ProcessPayment OrderService
HIGH 12 32 ValidateInventory OrderService
By Namespace (Directory)
=== COMPLEXITY BY NAMESPACE ===
Area Methods Avg Max Critical High Medium
MyApp.Core.Services 45 6.2 21 2 8 12
MyApp.Api.Controllers 32 4.8 15 0 3 8
MyApp.Data.Repositories 28 3.5 12 0 1 6
=== SUMMARY ===
Hottest Area: MyApp.Core.Services
Average Complexity: 6.2
Max Complexity: 21
Critical Methods: 2
⚠️ AREAS NEEDING ATTENTION:
MyApp.Core.Services
- 2 CRITICAL priority methods
- 8 HIGH priority methods
Integration with Test Generation
After running complexity analysis, you can:
- Use the report to prioritize testing
- Invoke the
dotnet-test-automationskill for high-priority methods - Focus testing efforts on CRITICAL and HIGH complexity methods
- Use area analysis to identify which namespaces/projects need refactoring
Example Usage
User asks:
- "Analyze the complexity of ProductService.cs"
- "Which methods in OrderProcessor.cs need testing?"
- "Show me the most complex methods in this file"
- "What's the complexity score for this code?"
- "Analyze complexity by namespace for the src directory"
- "Which project has the highest complexity?"
Skill will:
- Determine appropriate scope and grouping
- Run the RoslynTools.ComplexityAnalyzer
- Generate priority report
- Suggest which methods/areas to test first
Decision Rules
Exclude from analysis:
- Auto-properties (no logic)
- Simple field assignments
- Properties with only get/set
- Files in obj/ and bin/ directories
Include in analysis:
- All methods with logic
- Properties with computed values
- Constructors with logic
Methods with complexity = 1:
- Still listed but marked as "Can Skip"
- Usually trivial and don't need explicit tests
- Tested indirectly through other methods
Notes
- Uses Roslyn Workspace API with full semantic model
- Understands C# syntax and semantics perfectly
- Works with modern C# features (records, pattern matching, etc.)
- Analyzes entire solutions with cross-project understanding
- In-memory analysis (no file locking)
- Faster than syntax-only parsing due to cached compilation
- Server maintains workspace state between requests
Follow-up Actions
After analysis, suggest:
- "Generate tests for the CRITICAL priority methods"
- "Let's refactor methods with complexity > 15"
- "Run the test-coverage skill to see current coverage"
- "Analyze the hottest namespace/project in more detail"