Refactor (src/user/search.js): Deeply nested control flow (level = 5) #148
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.
Q1.3
What do you think this file does?
This file handles user search and lookup logic in NodeBB. It is responsible for processing user searches, resolving them into user IDs, and handling different conditions when a search is performed.
Q1.4
What is the scope of your refactoring within that file?
The scope of this refactoring is the User.search function. Specifically, the deeply nested conditional logic.
Q1.5
Which Qlty-reported issue did you address?
Deeply nested control flow (level = 5)
Q2.1
How did the specific issue you chose impact the codebase’s maintainability?
The use of nested if/else statements made the control flow difficult to follow and made it difficult to understand the function’s behavior. This made the code harder to maintain, debug, and modify, for people in the future that may want to contribute that are unfamiliar with the logic.
Q2.2
What changes did you make to resolve the issue?
I refactored the original logic by breaking up the User search function into smaller helper functions.
Q2.3
How do your changes improve maintainability? Did you consider alternatives?
Separating the logic into helper functions improves readability and the ability to debug. Each function now has a clearer purpose, making future updates easier less prone to errors. I did not consider an alternative, buy one could have been to flatten the nested conditionals by rewriting them with more specific conditionals.
Q3.1
How did you trigger the refactored code path from the UI?
In order to trigger the refactored code, I went the users page and performed a search on the admin user
Q3.2


Attach a screenshot of the logs and UI demonstrating the trigger.
Q3.3


Attach a screenshot of qlty smells output after changes.
Before:
After: