Refactor (src/messaging/rooms.js:470): complex binary expression #133
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:
#60
Full path to the refactored file:
src/messaging/rooms.js
What do you think this file does?
I think this file handles actions regarding messaging someone within a chat room when you click "chat with this user" in NodeBB after you've looked at their profile. Once you click on the chat feature, it opens up a chat room for you two, and this file handles loading all the information in the room.
What is the scope of your refactoring within that file?
Within the Messaging.loadRoom async function, I refactored the binary expression originally on line 470 that I believe handles determining conditions on displaying the room upon load.
Which Qlty‑reported issue did you address?
470 Complex binary expression
2. Refactoring
The issue makes the codebase more maintainable as it splits up the binary expression into distinct parts, which helps us better read and understand the expression but also leaves the ability to edit these parts in the future if we want to change the individual conditions themselves later on.
I split the binary expression into its three main parts and placed them into separate variables. This allowed me to make sure the functionality stayed the same, but the very long binary statements were cut down into smaller chunks.
The changes improves maintainability since it will speed up the process of comprehending and making changes to this conditional in the future. I considered alternatives like splitting into multiple if statements, but I thought that just breaking down the conditional into separate variables would be cleaner and easier to read.
3. Validation
I created a new user in the system, and then as admin I added that user to the Global Moderators group. Then, as the new user, I went to the group and clicked on the admin's user profile, then navigated to "chat with this user" and clicked on that button. That popped up a new chat room and ran the print statements that I had put in my code.
Attach a screenshot of
qlty smells --no-snippets <full/path/to/file.js>showing fewer reported issues after the changes.