GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [apex] CyclomaticComplexity: False negative with switch statements · Issue #4863 · pmd/pmd

[apex] CyclomaticComplexity: False negative with switch statements #4863

New issue
Copy link
New issue
Copy link
Closed
#7176
Closed
[apex] CyclomaticComplexity: False negative with switch statements#4863
#7176
Copy link
Labels
a:false-negativePMD doesn't flag a problematic piece of codein:documentationAffects the documentation [doc]
Milestone
7.30.0

Description

@aidan-harding
aidan-harding
opened on Mar 19, 2024
Issue body actions

I'm happy to fork the repo and have a go at fixing this if you're happy to accept the changes once you're happy with them.

Affects PMD Version: 6.55

Rule:

Cyclomatic Complexity

Description:

The cyclomatic complexity of a method should be increased by adding a switch statement, but it does not.

Moreover, the documentation states that it covers:

As such, they include all control flow statements, such as ‘if’, ‘while’, ‘for’, and ‘case’.

Which is not only not true, but also syntactically incorrect, as in Apex the keyword when is used instead of case.

Code Sample demonstrating the issue:

public class CyclomaticComplexity {
    void foo() {
        switch on Trigger.operationType {
            when BEFORE_INSERT {
                System.debug('Before Insert');
            }
            when AFTER_INSERT {
                System.debug('After Insert');
            }
        }
    }
}

Expected outcome:

PMD should report a cyclomatic complexity of 3 (starts at one, then +1 for each branch in the switch). It actually reports a cyclomatic complexity of 1.

Running PMD through: CLI

Activity

  1. aidan-harding
    added
    a:false-negativePMD doesn't flag a problematic piece of code
    on Mar 19, 2024
  2. jsotuyod
    added
    needs:pmd7-revalidationThe issue hasn't yet been retested vs PMD 7 and may be stale
    on Mar 20, 2024
  3. aidan-harding commented on Mar 20, 2024

    @aidan-harding
    aidan-harding
    on Mar 20, 2024
    Author
    More actions

    I checked this in 7.0.0-rc4 and it has the same behaviour.

  4. adangel
    changed the title [-][Apex] switch statements do not contribute to Cyclomatic Complexity numbers[/-] [+][apex] switch statements do not contribute to Cyclomatic Complexity numbers[/+] on Mar 21, 2024
  5. jsotuyod
    added
    in:documentationAffects the documentation [doc]
    and removed
    needs:pmd7-revalidationThe issue hasn't yet been retested vs PMD 7 and may be stale
    on Apr 2, 2024
  6. adangel
    marked [apex] cyclomatic complexity does not count branches of switch statement #5101 as a duplicate of this issue on Nov 5, 2026
  7. adangel
    changed the title [-][apex] switch statements do not contribute to Cyclomatic Complexity numbers[/-] [+][apex] CyclomaticComplexity: False negative with switch statements[/+] on Nov 5, 2026
  8. adangel
    added this to the 7.30.0 milestone on Nov 5, 2026
  9. adangel
    added a commit that references this issue on Nov 5, 2026

    Update references to pmd#4863

    465e66b
  10. adangel
    closed this as completedin #7176on Nov 5, 2026
  11. adangel
    added a commit that references this issue on Nov 5, 2026

    [apex] Fix pmd#4863: Count switch cases in cyclomatic complexity (pmd…

    This issue will close once commit 645071b is merged into the 'main' branch.
    645071b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-negativePMD doesn't flag a problematic piece of codein:documentationAffects the documentation [doc]

    Type

    No type

    Projects

    No projects

      Milestone

      • 7.30.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.