New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] CyclomaticComplexityRule counts ?-operator twice #1217

dreniers opened this Issue Jun 28, 2018 · 0 comments


None yet
2 participants

dreniers commented Jun 28, 2018

Affects PMD Version: 6.5.0

Rule: category/java/design.xml/CyclomaticComplexity

Description: CyclomaticComplexityRule counts conditional ternary operator twice. I believe this is incorrect, as it just adds one code path and can be rewritten as an if-else statement which just has complexity 1.

Code Sample demonstrating the issue:

  public void get() {
    final String a = b == null ? c : d;

Running PMD through: CLI

Example of output:

15:07:55 E:\dev\pmd-cyclox>more & echo --- & more ruleset-for-cyclox.xml & echo --- & pmd-bin-6.5.0\bin\pmd.bat -f xml -R ruleset-for-cyclo
x.xml -d
class MyClass {
  public void run(String b, String c) {
    final String a = b == null ? c : b;
<?xml version="1.0"?>

<ruleset name="Custom Rules"
Custom rules

        <property name="methodReportLevel" value="1"/>

jun 28, 2018 3:07:56 PM net.sourceforge.pmd.PMD processFiles
WARNING: This analysis could be faster, please consider using Incremental Analysis:
<?xml version="1.0" encoding="UTF-8"?>
<pmd xmlns=""
    version="6.5.0" timestamp="2018-06-28T15:07:56.983">
<file name="E:\dev\pmd-cyclox\">
<violation beginline="2" endline="4" begincolumn="10" endcolumn="3" rule="CyclomaticComplexity" ruleset="Design" class="MyClass" method="run" externalInfoUrl="h
ttps://" priority="3">
The method 'run(String, String)' has a cyclomatic complexity of 3.

PMD reports a complexity of 3, where I believe it should be 2: one for the method entry, and one for the ?-operator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment