Refactor (src/user/search.js): simplify filterAndSortUids to reduce complexity #263
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.
1. Issue
Please provide a link to the associated GitHub issue:
#262
Link to the associated GitHub issue:
#262
Full path to the refactored file:
src/user/search.js
What do you think this file does?
I think this file handles the user search on the server side. It looks up user IDs based on what was put in the search bar, and then filters them by things like online, verified, and banned. After that, it sorts the users if a sort field is given and returns the results so they can be shown in the UI.
What is the scope of your refactoring within that file?
The scope of my refactoring was the filterAndSortUids function. I focused on reducing the complexity of the function while keeping the same behavior for filtering and sorting users. Most of the code stayed the same, I just split it up it helpers to make it readable and manageable, while also reducing the complexity.
Which Qlty‑reported issue did you address?

I worked on the Qlty reported issue "Function with high complexity (count=11): filderAndSortUids. Before my changes, the function had a complexity of 11, and my goal was to lower that by cleaning up the logic and splitting things into clearer steps.
2. Refactoring
How did the specific issue you chose impact the codebase’s adaptability?
My changes made the code easier to understand and adapt in the future. I broke down long, nested checks into smaller steps so it's clearer what each part does. This way, if someone needs to add a new filter or something else, they can do it without making the function even more complicated.
What changes did you make to resolve the issue?
I cleaned up the function by breaking it into smaller steps. I organized how filters and fields are collected, and I made sure the checks for group membership and banned users happen in their own clear blocks. I also made the sorting easier to follow by moving it into its own helper.
How do your changes improve adaptability? Did you consider alternatives?
My changes make the function easier to adapt in the future because each part is more separate and clear. If new filters or sort options need to be added, it’s straightforward to plug them in without making the whole function more complicated. I did think about rewriting it into multiple smaller functions, but I chose to keep it in one place with helper calls so the flow is still easy to follow.
3. Validation
How did you trigger the refactored code path from the UI?


I triggered my refactored code by using the search feature in the NodeBB UI. I typed a username into the search bar on the Users page, which called the filterAndSortUids function I refactored.
Attach a screenshot of the logs and UI demonstrating the trigger.
Attach a screenshot of


qlty smells --no-snippets <full/path/to/file.js>showing fewer reported issues after the changes.