Skip to content

Conversation

mohitsatr
Copy link
Member

@mohitsatr mohitsatr commented Jan 28, 2025

resolves #16165

@mohitsatr
Copy link
Member Author

Github, generate report for CyclomaticComplexity/all-examples-in-one

Copy link
Contributor

@mohitsatr
Copy link
Member Author

@nrmancuso @mahfouz72 please take a look at case
how should nested switch statements be treated ?

@nrmancuso
Copy link
Contributor

nrmancuso commented Jan 29, 2025

how should nested switch statements be treated

@mohitsatr please propose a couple of solutions with examples, and provide your recommendation.

@mohitsatr
Copy link
Member Author

Github, generate report for CyclomaticComplexity/all-examples-in-one

Copy link
Contributor

@mohitsatr
Copy link
Member Author

@nrmancuso Well, according to me there are only two possible solutions:

  1. Treat the outer most switch block as single decision point and ignore everything inside it, including any nested switches.
  2. Treat the outer most switch block as single decision point but still count any nested switch while ignoring everything else in the block.

i went with second approach (for no particular reason).

Second Approach:
if there's a nested switch block, complexity increases for the switch keyword but anything inside that 2nd level switch block is ignored. in other words, complexity is just no. of switch keywords in the function.
See the example below :

// Cyclomatic Complexity is 4 
void test3(Object obj1, Object obj2, Object obj3) {
        int j,k,l,m;
        switch (obj1) {                //1
            case Integer i1:
            case Integer i2:
            case Integer i3:
                break;
            case Integer i4:
                switch (obj2) {        //2
                    case Integer i1:
                       j = 1;
                       break;
                    default:
                        break;
                }
                break;
            case Integer i5:
                if (j == 1) {               //ignored
                    k = 1;
                }
                else {
                    l == 1;
                }
                break;
            case Integer i6:
                switch (obj3) {        //3
                    case Integer i6:
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }
    }

but that raises the question: why not the first approach ? why are we even worrying about switch block inside a switch block. we can simply ignore everything inside the switch block. After all switchBlockAsSingleDecisionPoint is not the default behaviour and if a user enables it, then they must expect Check to ignore nested switches too, right ?

@nrmancuso
Copy link
Contributor

I vote for (1), @mahfouz72 @romani @rnveach what do you think?

@mohitsatr mohitsatr marked this pull request as ready for review February 6, 2025 03:03
@romani
Copy link
Member

romani commented Feb 8, 2025

@mohitsatr , please proceed with (1). This is the only possible way, switchBlockAsSingleDecisionPoint is driving behavior, no need to look inside switch at all.

@mohitsatr mohitsatr force-pushed the cc-switch branch 2 times, most recently from 4cca388 to 6c6c0ee Compare February 9, 2025 03:09
@mohitsatr
Copy link
Member Author

Github, generate report for CyclomaticComplexity/all-examples-in-one

Copy link
Contributor

github-actions bot commented Feb 9, 2025

@romani
Copy link
Member

romani commented Feb 9, 2025

@mohitsatr , please check jdk21 compilation .

Remove wip from description.

@mohitsatr
Copy link
Member Author

@romani have you gone through the regression report ? don't want to waste time if it's already reviewed by you.

@romani
Copy link
Member

romani commented Feb 11, 2025

@mohitsatr , I am waiting explicit comment that PR is ready for review as it was in WIP for long, so please do all verifications on your side first.

@mohitsatr
Copy link
Member Author

@romani this one is ready for review. on my part I checked all the files in report and Im pretty confident that change is working . I have not gone all in Openjdk. it is tiring.

@mohitsatr mohitsatr changed the title Issue #16165: fix cc's switchBlockAsSingleDecisionPoint Issue #16165: fix CyclomaticComplexity's switchBlockAsSingleDecisionPoint to properly handle switch blocks Feb 14, 2025
Copy link
Member

@romani romani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

items:

@mohitsatr
Copy link
Member Author

Github, generate report for CyclomaticComplexity/all-examples-in-one

Copy link
Contributor

Copy link
Member

@romani romani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks a lot.

Copy link
Contributor

@nrmancuso nrmancuso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good stuff

@nrmancuso nrmancuso merged commit 2ab9638 into checkstyle:master Feb 24, 2025
102 checks passed
@mohitsatr mohitsatr deleted the cc-switch branch February 24, 2025 04:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regression: CyclomaticComplexity.switchBlockAsSingleDecisionPoint does not work properly anymore (10.21.1)
3 participants