Cognitive Complexity metric

Properties
LC0089 Hidden Design Code Fix Ignore Obsolete

LC0089 reports the calculated Cognitive Complexity score for every procedure and trigger. Unlike LC0090 , which fires only when the score meets or exceeds a threshold, this diagnostic is always reported — it measures every method regardless of its complexity.

The rule is disabled by default. Enable it when you need visibility across the codebase: to baseline complexity before a refactoring effort, to surface scores in code reviews, or to track how complexity evolves over time.

Example

procedure ApplyDiscount(SalesLine: Record "Sales Line"; DiscountPct: Decimal) // Cognitive Complexity: 10 (threshold ≥ 15) [LC0089]
begin
    if SalesLine.Type = SalesLine.Type::Item then       // +1 (1 increment + 0 nesting penalty)
        if SalesLine.Quantity > 0 then                  // +2 (1 increment + 1 nesting penalty)
            if SalesLine."Unit Price" > 0 then          // +3 (1 increment + 2 nesting penalty)
                if DiscountPct > 0 then                 // +4 (1 increment + 3 nesting penalty)
                    SalesLine.Validate("Line Discount %", DiscountPct);
end;

Flattening the nesting with guard clauses drops the score from 10 to 0 — guard clauses are not counted:

procedure ApplyDiscount(SalesLine: Record "Sales Line"; DiscountPct: Decimal)
begin
    if SalesLine.Type <> SalesLine.Type::Item then
        exit;

    if SalesLine.Quantity <= 0 then
        exit;

    if SalesLine."Unit Price" <= 0 then
        exit;

    if DiscountPct <= 0 then
        exit;

    SalesLine.Validate("Line Discount %", DiscountPct);
end;

For the full scoring model — how nesting penalties, logical operators, recursion, and compensating usages like else if are calculated — see LC0090 .

Increment diagnostics

LC0089 reports the total score per method. To see individual increments at each source location, enable the companion LC0089i diagnostic. Combined with the Error Lens extension for VS Code , this makes the scoring fully transparent in the editor.

{
    "id": "LC0089i",
    "action": "Info",
    "justification": "Show every individual increment of the Cognitive Complexity score"
}

Each increment shows the construct type, the total increment, and the nesting penalty breakdown — for example: IfStatement: +3 (1 increment + 2 nesting penalty).

See also

  • LC0090 — Cognitive Complexity threshold exceeded
  • LC0009 — Cyclomatic Complexity metric