ppkarwasz commented on PR #477: URL: https://github.com/apache/commons-csv/pull/477#issuecomment-2993649756
Hi @sigee, > My primary goal was to reduce cognitive complexity as defined in [SonarSource's Cognitive Complexity whitepaper](https://www.sonarsource.com/docs/CognitiveComplexity.pdf). The original method contained multiple levels of nested blocks, which significantly increases cognitive load according to this metric. Metrics like SonarSource's can help highlight areas of higher complexity. However, I think it's more important to assess the resulting code from a human perspective: *Does it become easier to understand and maintain?* Personally, I found the refactored version a bit harder to follow and would likely need more time to understand it. > While I acknowledge that I haven't yet simplified the extracted logic itself, I believe this refactoring does provide measurable benefits: > > * The original `createHeaders()` method now has reduced nesting levels, making the high-level flow easier to follow > * The extracted `buildHeaderNames()` method, while containing the same logic, has fewer nested levels than the original method had overall > * This separation makes the complex header processing logic more focused I don’t see the nesting as the primary challenge in understanding the method. The more significant cognitive load comes from unpacking the *semantic steps* of the process—what each block is doing and why. Here’s an approach I believe could make the method clearer: 1. **Header Retrieval** – Depending on the `CSVFormat`, the method extracts raw header names in different ways. This logic could be encapsulated in a `getHeaderNames()` method. 2. **Header Validation** – The duplicate header name handling could live in a method like: ```java private static Headers validateHeaderNames(String[] headerNames); ``` This would cleanly isolate the logic for checking and preparing headers. 3. **`Headers` Construction** – The current `Headers` class requires the manual creation of a list and a map that is then passed to the class constructor. It might be more intuitive and object-oriented to let the class build itself with an `add(String name)` method, abstracting that construction logic internally. Let me know what you think—I’m happy to help iterate on this if you agree with some of these ideas. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
