Refactor (src/groups/membership.js): reduce complexity membership exports #247
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 the associated GitHub issue:
#202
Full path to the refactored file:
src/groups/membership.js
What do you think this file does?
I believe this file handles users' memberships to groups within NodeBB. It checks whether or not a user belongs to a particular group before allowing them access to the group.
What is the scope of your refactoring within that file?
I touched many functions in membership.js. The ones I edited to reduce return statements for one of the smells were Groups.getMembers, Groups.getMembersOfGroups, and Groups.getMemberCount. I refactored Groups.isMember, Groups.isMembers, isMemberOfGroups, Groups.isMembersOfGroupList, and getGroupNames.
Which Qlty‑reported issue did you address?
Function with high complexity (count = 24): exports
2. Refactoring
How did the specific issue you chose impact the codebase’s adaptability?
The issue I chose was high complexity, which makes the code harder to understand, and thus, harder and riskier to change without creating bugs. Highly complex code is also harder for new software developers to maintain because they must first spend time understanding it.
What changes did you make to resolve the issue?
I removed return statements when I could to reduce the "Function with many returns" smell as well. I also replaced a nested loop with clearer functions and broke down nested logic repeated across different functions into helper functions at the beginning of the file.
How do your changes improve adaptability? Did you consider alternatives?
I think the code is a little clearer to read for the first time and understand than it was before, due to the inclusion of helper functions, which allow the code to be read similarly to English. At first, I was just going to address the "many returns" smell, but as I was removing the multiple returns within the if statements and returning at the end of the code, my complexity was increasing because I was adding more and more conditionals. Instead, I decided it was probably in the best interest of the code's adaptability to focus on the complexity, rather than the returns.
3. Validation
How did you trigger the refactored code path from the UI?
I triggered the code path in the NodeBB UI by clicking into the "Groups" tab along the top of the screen. This action triggered the first set of console.log messages. Then, I clicked into the "Administrators Group," which triggered the second set of messges in the log.