Reduce cognitive complexity in five benchmark test classes#48
Open
sonarqube-agent[bot] wants to merge 1 commit into
Open
Reduce cognitive complexity in five benchmark test classes#48sonarqube-agent[bot] wants to merge 1 commit into
sonarqube-agent[bot] wants to merge 1 commit into
Conversation
Fixed issues: - AZ3eW054PF-XYsB0Knbw for java:S3776 rule - AZ3eW00ZPF-XYsB0KnaW for java:S3776 rule - AZ3eW08FPF-XYsB0Knca for java:S3776 rule - AZ3eW09NPF-XYsB0Kncz for java:S3776 rule - AZ3eW04IPF-XYsB0KnbW for java:S3776 rule Generated by SonarQube Agent (task: fc5c8900-fe9f-4ce9-a928-899598b297f8)
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.
Refactored five benchmark test classes (BenchmarkTest01197, BenchmarkTest01399, BenchmarkTest02295, BenchmarkTest02360, and BenchmarkTest02614) by extracting complex nested control flow from doPost methods into dedicated helper methods. This brings all five methods below the SonarQube cognitive complexity threshold of 15, eliminating critical code smells and improving code maintainability.
View Project in SonarCloud
Fixed Issues
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/BenchmarkTest01197.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 cookie-searching logic (which included nested if statements, a for loop with a compound condition, and deep nesting) with a single method call to
isUserFound(). By extracting this complex block into a separate method, the cognitive complexity of thedoPostmethod is significantly reduced — removing the contributions from the nestedif (cookies != null), theforloop, theif (cookieName.equals(...)), and theif (cookie.getValue()...)checks, which together contributed heavily to the method's cognitive complexity score of 16. This brings the complexity below the allowed threshold of 15.java:S3776 - Refactor this method to reduce its Cognitive Complexity from 18 to the 15 allowed. • CRITICAL • View issue
Location:
src/main/java/org/owasp/benchmark/testcode/BenchmarkTest01399.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 extraction logic (which contained nested while loops, for loops, and if statements contributing significantly to cognitive complexity) with a single method call to
extractParam(). By extracting this complex block into a separate method, thedoPostmethod's cognitive complexity is reduced well below the threshold of 15, addressing the code smell about excessive cognitive complexity in thedoPostmethod.java:S3776 - Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed. • CRITICAL • View issue
Location:
src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02295.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 extraction logic (which contained nested while loops, if statements, and for loops contributing to high cognitive complexity) with a single method call to extractParam(). By moving the complex nested control flow out of the doPost method, the cognitive complexity of doPost is significantly reduced, bringing it below the allowed threshold of 15. The nested while loop with a compound condition, the nested if-check for null values, the nested for loop, and the nested if-check for the matching parameter name are all removed from doPost's complexity calculation.
java:S3776 - Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed. • CRITICAL • View issue
Location:
src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02360.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 extraction logic (which contained nested while loops, if statements, and for loops contributing to high cognitive complexity) with a single method call to extractParam(). By moving the deeply nested control flow out of the doPost method, the cognitive complexity of doPost is significantly reduced, bringing it below the allowed threshold of 15.
java:S3776 - Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed. • CRITICAL • View issue
Location:
src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02614.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 a deeply nested block of cookie-searching logic (containing multiple nested if statements, a for loop with a compound condition, and several levels of nesting) with a single method call to findUserCookie(). By extracting this complex code into a separate method, the cognitive complexity of the doPost method is significantly reduced — removing the contributions from the nested if/for/if/if structure, the compound loop condition, and the deep nesting penalties that collectively added substantial cognitive complexity to the method.
SonarQube Remediation Agent uses AI. Check for mistakes.