{"slug": "codemetrics-cli-for-c-projects-in-vs-code-and-visual-studio", "title": "CodeMetrics.CLI for C# projects in VS Code and Visual Studio", "summary": "CodeMetrics.CLI, a new .NET global tool for C# projects, measures code complexity and maintainability directly from source syntax without restoring or compiling, offering metrics such as cyclomatic and cognitive complexity, and supports table, JSON, and HTML report formats. The tool is designed for developers, code reviews, CI gates, and AI agent workflows to verify code quality, with commands like `codemetrics ./src --format html --output report.html` and configurable thresholds via `--max-cognitive` and `--max-cyclomatic`.", "body_md": "CodeMetrics.Cli is a .NET global tool for measuring code quality and complexity in C# source files without restoring or compiling the target project.\n\nWith a time will give you quick feedback on your project code complexity. It is designed for developers, code reviews, CI gates, and agent-driven workflows that need quick, deterministic complexity metrics from source text alone. Allows AI Agents to verify code they produce, ensuring it meets complexity and maintainability standards keeping code readable and maintainable.\n\n*The `--format html` report: summary tiles, complexity and maintainability charts, risk distribution, and a sortable member table.*\n\n```\ndotnet tool install --global CodeMetrics.Cli\ncodemetrics ./src\ncodemetrics ./src --format json --output metrics.json\ncodemetrics ./src --format html --output report.html\ncodemetrics -s src/Checkout.cs\ncodemetrics ./src --max-cognitive 15 --max-cyclomatic 10\n```\n\nThe tool inspects C# syntax and reports member-level metrics including:\n\n- Cyclomatic complexity\n- Cognitive complexity\n- Parameter count\n- Nesting depth\n- Maintainability index\n- Lines of code\n- Source summary totals\n\nA report row is produced for each of these, when it has a body:\n\n- Methods, constructors, destructors, operators and conversion operators\n- Property, indexer and event accessors (`get` ,`set` ,`init` ,`add` ,`remove` )\n- Expression-bodied properties and indexers\n- A top-level statement program, reported once as `<top-level>.<main>`\n\nMembers without a body, such as abstract or interface declarations and auto-property accessors, are not reported. Lambdas and local functions do not get their own row; they fold into the member that contains them, which is what Visual Studio does.\n\nThe number of linearly independent paths through a member. It starts at 1 and\nadds 1 per decision point, counts over the *syntax tree*.\n\n| Adds 1 | Adds nothing | \n|---|---|\n| `if` ,`while` ,`do` ,`for` ,`foreach` | `else` ,`try` ,`finally` ,`goto` | \n| Each `case` label and switch expression arm | `default:` and a discard (`case _:` ,`_ =>` ) | \n| `catch` , and a`when` filter or case guard | `not` patterns | \n| `?:` ,`?.` ,`?[]` |  | \n| Each `&&` ,`\\|\\|` ,`??` ,`??=` |  | \n| Each `and` and`or` pattern |  | \n\nCompound predicates count once per operator, as McCabe specifies, so `a && b`\nis two decisions. Pattern combinators follow the same rule, which keeps\n`x is 1 or 2` level with `x == 1 || x == 2`.\n\nMcCabe's risk bands are 1-10 simple, 11-20 moderate, 21-50 complex, and over 50\nuntestable. Microsoft suggests 10 as a starting limit for `--max-cyclomatic`.\n\nAnalysis is syntax-only, so some constructs are counted conservatively:\n\n- Property and recursive patterns are not decomposed. `x is { A: 1, B: 2 }` scores 1 where`x.A == 1 && x.B == 2` scores 2.\n- LINQ query clauses are not counted.\n- In a case label a bare `_` is read as the discard rather than as a constant of\nthat name. Syntax alone cannot tell the two apart. An escaped`@_` is left as\na constant.\n\nVisual Studio counts over Roslyn's *operation tree*, a semantic model that\nrequires the project to compile. CodeMetrics counts over the *syntax tree*,\nwhich is why it needs no restore or build. Two different methods, so the two\nnumbers will not always agree.\n\n| Construct | CodeMetrics | Visual Studio | \n|---|---|---|\n| `and` /`or` pattern | 1 each | 0 | \n| Switch expression arm | 1 each | 0 | \n| `??=` | 1 | 0 | \n| `default:` label | 0 | 1 | \n\nIn the operation tree several of these collapse into kinds Visual Studio does\nnot treat as branches, and `default:` becomes an ordinary case clause. Neither\ntool is wrong; they apply different rules. Equivalent spellings score alike:\n`x is 1 or 2` matches `x == 1 || x == 2`, and `case 1 or 2:` matches\n`case 1: case 2:`. Under Visual Studio's rules those pairs score differently.\n\n- Solution files (`.sln` ,`.slnx` )\n- Project files (`.csproj` )\n- Directories containing C# source files\n- Single C# files via `-s` /`--single`\n\n- `table` (default human-readable output)\n- `csv`\n- `json`\n- `html`\n\n- Analysis is syntax-only.\n- No project restore or semantic compile is required.\n- Output is deterministic and suitable for CI checks and automation.\n- Single-file mode writes the report to stdout and next to the source file as `<name>.codemetrics.<ext>` .\n\n```\ncodemetrics --help\n```\n\n- Project home: [https://github.com/quicksln/CodeMetrics.CLI](https://github.com/quicksln/CodeMetrics.CLI)\n- Issues: [https://github.com/quicksln/CodeMetrics.CLI/issues](https://github.com/quicksln/CodeMetrics.CLI/issues)\n- License: [MIT](https://github.com/quicksln/CodeMetrics.CLI/blob/main/LICENSE)\n\n```\ncodemetrics ./src --max-cognitive 15 --max-cyclomatic 10\n```\n\nThis exits with code `1` if any member exceeds the configured threshold, which makes it useful in CI pipelines.", "url": "https://wpnews.pro/news/codemetrics-cli-for-c-projects-in-vs-code-and-visual-studio", "canonical_source": "https://github.com/quicksln/CodeMetrics.CLI", "published_at": "2026-09-08 06:22:28+00:00", "updated_at": "2026-09-08 07:01:53.668829+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["CodeMetrics.CLI", ".NET", "Visual Studio"], "alternates": {"html": "https://wpnews.pro/news/codemetrics-cli-for-c-projects-in-vs-code-and-visual-studio", "markdown": "https://wpnews.pro/news/codemetrics-cli-for-c-projects-in-vs-code-and-visual-studio.md", "text": "https://wpnews.pro/news/codemetrics-cli-for-c-projects-in-vs-code-and-visual-studio.txt", "jsonld": "https://wpnews.pro/news/codemetrics-cli-for-c-projects-in-vs-code-and-visual-studio.jsonld"}}