Reduce cognitive complexity across five benchmark test methods#61
Open
sonarqube-agent[bot] wants to merge 1 commit into
Open
Reduce cognitive complexity across five benchmark test methods#61sonarqube-agent[bot] wants to merge 1 commit into
sonarqube-agent[bot] wants to merge 1 commit into
Conversation
Fixed issues: - AZ3eW0-DPF-XYsB0KndL for java:S3776 rule - AZ3eW4KhPF-XYsB0KoT4 for java:S3776 rule - AZ3eW43iPF-XYsB0Kogx for java:S3776 rule - AZ3eW0l0PF-XYsB0KnWy for java:S3776 rule - AZ3eW0qbPF-XYsB0KnX0 for java:S3776 rule Generated by SonarQube Agent (task: 085e327e-95e0-4f01-8fda-ccffef761f7c)
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 change reduces cognitive complexity in five benchmark test classes by extracting complex parameter extraction logic and consolidating exception handling. Each method exceeded the allowed cognitive complexity threshold of 15 by significant margins (ranging from 16 to 26), and these refactorings bring them into compliance while improving code readability.
View Project in SonarCloud
Fixed Issues
java:S3776 - Refactor this method to reduce its Cognitive Complexity from 19 to the 15 allowed. • CRITICAL • View issue
Location:
src/main/java/org/owasp/benchmark/testcode/BenchmarkTest00528.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 included nested while/if/for loops with compound conditions contributing heavily to cognitive complexity) with a single method call
getParam(request). By extracting this complex block out ofdoPost, the cognitive complexity ofdoPostis significantly reduced — removing the while loop (+1), the&& flagcondition (+1), the nested if for values != null (+2), the nested for loop (+3), the&& flagin the for condition (+1), and the deeply nested if for value.equals (+4), totaling 12 points of cognitive complexity removed fromdoPost. This addresses both the overall cognitive complexity issue on thedoPostmethod (which was at 19, exceeding the allowed 15) and the specific flow contributions from the nested control structures and compound boolean conditions within this extracted block.java:S3776 - Refactor this method to reduce its Cognitive Complexity from 25 to the 15 allowed. • CRITICAL • View issue
Location:
src/main/java/org/owasp/benchmark/testcode/BenchmarkTest01432.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 extracts the parameter-searching logic (while loop with nested if/for statements) from the doPost method into a separate method call
extractParam(request). This reduces the cognitive complexity of doPost by removing the while loop, nested conditionals, and nested for loop that contributed significantly to the complexity score. The doPost method had a cognitive complexity of 25, exceeding the allowed threshold of 15. By extracting this block into a helper method, the deeply nested control flow (while with && condition, nested if, nested for with && condition, and deeply nested if) no longer counts toward doPost's 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/BenchmarkTest01976.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 consolidates six separate catch blocks (for NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, and InvalidAlgorithmParameterException) into a single multi-catch block. Each of those individual catch blocks contributed +1 to the cognitive complexity of the doPost method (5 of the 6 catches contributed, since one catch remains). By merging them into one catch clause, the cognitive complexity is reduced by 5 points, bringing the method's complexity from 16 down to within the allowed threshold of 15.
java:S3776 - Refactor this method to reduce its Cognitive Complexity from 23 to the 15 allowed. • CRITICAL • View issue
Location:
src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02291.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 (containing nested while loops, if statements, and for loops that contributed significantly to cognitive complexity) with a single method call to
extractParam(request). By extracting this complex control flow into a separate method, the cognitive complexity of thedoPostmethod is reduced, since method calls are "free" in cognitive complexity scoring. This directly addresses the high cognitive complexity (23 vs allowed 15) of thedoPostmethod reported as a code smell for thedoPostmethod needing refactoring to reduce its Cognitive Complexity.java:S3776 - Refactor this method to reduce its Cognitive Complexity from 26 to the 15 allowed. • CRITICAL • View issue
Location:
src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02348.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 complex inline parameter extraction block (containing nested while loops, for loops, and conditionals) with a single method call to
extractParam(request). By extracting this logic into a separate method, the cognitive complexity of thedoPostmethod is reduced significantly — removing the nested loops and conditionals that contributed +1 (while), +1 (&&), +2 (nested if), +3 (nested for), +1 (&&), and +4 (deeply nested if) to the complexity score. This directly addresses the high cognitive complexity warning on thedoPostmethod, which was measured at 26 against an allowed threshold of 15. The extraction reduces the nesting depth and control flow complexity withindoPost, making it easier to read and understand.SonarQube Remediation Agent uses AI. Check for mistakes.