Refactor (src/controller/search.js): eliminated complex binary expressions in searchController.search #171
+22
−5
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.
P1B: Starter Task: Refactoring PR
1. Issue
Link to Issue
Full path to refactored file:
src/controllers/search.js
What do you think this file does?
*This file contains the controller that handles search requests from NodeBB. It validates/checks the search parameters or input, as well as checks user access permissions for different types of search. It then calls the actual search file, and creates the view for the /search page. *
What is the scope of your refactoring within that file?
*I made refactors in the searchController.search function in the search.js file, which handles the user permissions of whether the user's priviledges permit the type of search performed. *
Which Qlty‑reported issue did you address?
complex binary expression: 36
complex binary expression: 37
2. Refactoring
How did the specific issue you chose impact the codebase’s maintainability?
The specific issue I chose was not a logical issue, but rather a restructuring one. Initially, it used several conditional statements in one single line, which could potentially increase memory run time, and has unclear logic, which leads to the triggering of the complex binary expression code smell, thus contributing to less maintainability.
What changes did you make to resolve the issue?
I split the conditional statements into individual scope checks, by declaring constant variables with descriptive names (inUsers, inTags, etc.) to help distinguish the checks used in the conditional statement. Then, I simply checked all the conditions with the variable 'allowed', and check it with a simple if(!allowed) statement.
How do your changes improve maintainability? Did you consider alternatives?
My code changes improve maintainability be eliminating the complex binary expression smell, breaking down the conditional statement check into intuitively named constants, which are then combined for a simple conditional check. I think this was an efficient way to solve the problem, as it keeps the logic the same, but simply makes it more intuitive to read and reduces the load in the statement.
3. Validation
How did you trigger the refactored code path from the UI?
Attach a screenshot of the logs and UI demonstrating the trigger.



Relevant UI:
Logs: (Phasakorn Chivatxaranukul printed when search is made)
Qlty Smells Output: (complex binary expression: 36, 37 are not in the output anymore)