|
| 1 | +--- |
| 2 | +id: TASK-102 |
| 3 | +title: Fix java:S3776 in CliTaskExecutorService.java at line 333 |
| 4 | +status: Done |
| 5 | +priority: high |
| 6 | +assignee: [] |
| 7 | +created_date: '2026-02-20 21:47' |
| 8 | +labels: |
| 9 | + - sonarqube |
| 10 | + - java |
| 11 | +dependencies: [] |
| 12 | +references: [] |
| 13 | +documentation: [] |
| 14 | +ordinal: 102000 |
| 15 | +--- |
| 16 | + |
| 17 | +# Fix `java:S3776`: Refactor this method to reduce its Cognitive Complexity from 30 to the 15 allowed. |
| 18 | + |
| 19 | +## Description |
| 20 | + |
| 21 | +SonarQube for IDE detected a code quality issue. |
| 22 | + |
| 23 | +- **Rule:** `java:S3776` |
| 24 | +- **File:** `src/main/java/com/devoxx/genie/service/cli/CliTaskExecutorService.java` |
| 25 | +- **Line:** 333 |
| 26 | +- **Severity:** High impact on Maintainability |
| 27 | +- **Issue:** Refactor this method to reduce its Cognitive Complexity from 30 to the 15 allowed. |
| 28 | + |
| 29 | +## Task |
| 30 | + |
| 31 | +Fix the SonarQube issue `java:S3776` at line 333 in `src/main/java/com/devoxx/genie/service/cli/CliTaskExecutorService.java`. |
| 32 | + |
| 33 | +## Acceptance Criteria |
| 34 | + |
| 35 | +- [x] Issue `java:S3776` at `CliTaskExecutorService.java:333` is resolved |
| 36 | +- [x] No new SonarQube issues introduced by the fix |
| 37 | +- [x] All existing tests continue to pass |
| 38 | + |
| 39 | +## Implementation Notes |
| 40 | + |
| 41 | +The `createStreamReader` method (line 333) had a cognitive complexity of 30, far exceeding the limit of 15. |
| 42 | + |
| 43 | +**Root cause:** The method created a `Thread` with a large lambda body containing: |
| 44 | +- A nested `invokeLater` lambda with further conditionals |
| 45 | +- A `try-with-resources` block with a `while` loop |
| 46 | +- Multiple `if` statements and ternary operators, all deeply nested |
| 47 | + |
| 48 | +**Fix:** Extracted the thread body into three focused helper methods: |
| 49 | + |
| 50 | +1. **`streamLines(...)`** — Contains the stream-reading loop logic (reads lines, collects stderr, calls helpers). Cognitive complexity ~7. |
| 51 | +2. **`logLineIfNeeded(...)`** — Handles the periodic logging condition (`lineCount <= 5 || lineCount % 50 == 0`). Complexity ~3. |
| 52 | +3. **`printLineToConsole(...)`** — Dispatches a line to the EDT via `invokeLater`, prefixing with task ID when parallel. Complexity ~5. |
| 53 | + |
| 54 | +The refactored `createStreamReader` now has complexity ~1 (single ternary for stream name selection). |
| 55 | + |
| 56 | +**Files modified:** |
| 57 | +- `src/main/java/com/devoxx/genie/service/cli/CliTaskExecutorService.java` |
| 58 | + |
| 59 | +## Final Summary |
| 60 | + |
| 61 | +Fixed `java:S3776` in `CliTaskExecutorService.createStreamReader()` by decomposing the 44-line monolithic thread lambda into three private helper methods: `streamLines()`, `logLineIfNeeded()`, and `printLineToConsole()`. The original method had a cognitive complexity of 30 due to deeply nested lambdas (thread body → invokeLater lambda), loops, and multiple conditionals. After extraction, `createStreamReader` has complexity ~1, while each helper stays well under 10. All 7 existing `CliTaskExecutorServiceTest` tests pass. The only build failures in the project are pre-existing unrelated errors in `FileSelectionPanelFactory.java`. |
0 commit comments