Reduce cognitive complexity in BenchmarkTest classes to meet SonarQube threshold#55
Open
sonarqube-agent[bot] wants to merge 1 commit into
Open
Reduce cognitive complexity in BenchmarkTest classes to meet SonarQube threshold#55sonarqube-agent[bot] wants to merge 1 commit into
sonarqube-agent[bot] wants to merge 1 commit into
Conversation
Fixed issues: - AZ3eW0APPF-XYsB0KnNg for java:S3776 rule - AZ3eW0DHPF-XYsB0KnOU for java:S3776 rule - AZ3eW0crPF-XYsB0KnU3 for java:S3776 rule - AZ3eW0E_PF-XYsB0KnO6 for java:S3776 rule Generated by SonarQube Agent (task: 6769d774-2aa8-40a1-98f0-9993aa401b5d)
Author
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR resolves 4 CRITICAL SonarQube issues (java:S3776) by refactoring complex conditional logic in four test classes. The changes extract nested control flow into helper methods or combine nested conditionals using logical operators (&&), reducing cognitive complexity from 16–20 down to the 15-point threshold in each affected method. These improvements enhance code maintainability and reduce the cognitive burden on developers reviewing or modifying these test utilities.
View Project in SonarCloud
Fixed Issues
java:S3776 - Refactor this method to reduce its Cognitive Complexity from 20 to the 15 allowed. • CRITICAL • View issue
Location:
src/main/java/org/owasp/benchmark/testcode/BenchmarkTest00094.java:49Why is this an issue?
Cognitive Complexity is a measure of how hard it is to understand the control flow of a unit of code. Code with high cognitive complexity is hard to read, understand, test, and modify.
What changed
This hunk replaces the inline cookie-parsing logic (which included nested if statements, a for loop, and conditional checks) with a single method call to getParam(). By extracting this complex block out of the doPost method, the cognitive complexity of doPost is reduced significantly, bringing it below the allowed threshold of 15. The nested conditionals and loop that contributed heavily to the complexity score (including the cookie null check, the for-each loop, and the cookie name comparison) are no longer counted against doPost's complexity.
java:S3776 - Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed. • CRITICAL • View issue
Location:
src/main/java/org/owasp/benchmark/testcode/BenchmarkTest00296.java:39Why is this an issue?
Cognitive Complexity is a measure of how hard it is to understand the control flow of a unit of code. Code with high cognitive complexity is hard to read, understand, test, and modify.
What changed
This hunk merges two nested
ifconditions into a singleifwith a combined condition using&&. The original code had anifat line 76 (cognitive complexity +3 including nesting) and a nestedifat line 77 (cognitive complexity +4 including nesting), contributing 7 to the total cognitive complexity. By combining them into oneifstatement, the nesting depth is reduced by one level, eliminating the innerifentirely. This reduces the cognitive complexity of thedoPostmethod from 16 to 15 (removing the +4 nested if and replacing the +3 if with a +3 if that includes the&&condition), bringing it within the allowed threshold of 15 and resolving the code smell about excessive cognitive complexity.java:S3776 - Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed. • CRITICAL • View issue
Location:
src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02354.java:39Why is this an issue?
Cognitive Complexity is a measure of how hard it is to understand the control flow of a unit of code. Code with high cognitive complexity is hard to read, understand, test, and modify.
What changed
This hunk replaces the inline parameter-searching logic (which contained nested while loops, if statements, for loops, and boolean flag conditions contributing to high cognitive complexity) with a single call to an extracted helper method
findParam(request). By moving the complex control flow out of thedoPostmethod, the cognitive complexity ofdoPostis significantly reduced, bringing it below the allowed threshold of 15.java:S3776 - Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed. • CRITICAL • View issue
Location:
src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02503.java:39Why is this an issue?
Cognitive Complexity is a measure of how hard it is to understand the control flow of a unit of code. Code with high cognitive complexity is hard to read, understand, test, and modify.
What changed
This hunk merges two nested
ifconditions into a singleifwith a combined condition using&&. The original code had anifcheckingcookieName.equals(cookie.getName())with a nestedifcheckingcookie.getValue().equals(...), which contributed +3 and +4 to cognitive complexity respectively (due to nesting levels 2 and 3). By combining them into oneifstatement, the nesting depth is reduced by one level, eliminating the extra cognitive complexity from the deeply nested innerif. This reduces the method's overall cognitive complexity from 16 to 15, bringing it within the allowed threshold and resolving the code smell about excessive cognitive complexity in thedoPostmethod.SonarQube Remediation Agent uses AI. Check for mistakes.