Refactor (src/topics/unread.js): reduce complexity of function getTids #255
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
Please provide a link to the associated GitHub issue:
Link to the associated GitHub issue:
#154
Full path to the refactored file:
src/topics/unread.js
What do you think this file does?
I think this file is responsible for getting information about the unread topics, including the number of unread topics, and the IDs and data for each unread topic. Whenever the site is reloaded, parts of this file get run to recalculate how many unread topics there are, in order to display the accurate number on the navigation button to access unread topics.
What is the scope of your refactoring within that file?
The function I worked on was
getTids. To reduce the function's complexity, I split the main parts of the function into various helper functions that can be called ingetTids.Which Qlty‑reported issue did you address?
2. Refactoring
How did the specific issue you chose impact the codebase’s adaptability?
The
getTidsfunction is very long and complex, so it can be difficult to understand what exact steps are being taken to compile all of the topics' details that are returned by the function. This can make it harder for anyone trying to add to or modify functionalities related to unread topics. Additionally, if there was an error somewhere in the function, it would be difficult to identify the exact origin of the error within the large amount of code that makes up this single function.What changes did you make to resolve the issue?
As the
getTidsfunction was taking multiple different steps to compile the information on the unread topics, I split up the large function into smaller helper functions that deal with each of the main steps. I called these shorter helper functions ingetTidsto make this main function shorter and clearer.How do your changes improve adaptability? Did you consider alternatives?
My changes help to make the
getTidsfunction shorter and easier to understand, as each of the helper functions is named based on the specific functionality that it is accomplishing, so it is clearer what specific parts of the code are accomplishing. Having multiple helper functions makes the code more readable and also makes it easier to test specific steps in the process of collecting the unread topics' data. I didn't consider any alternatives, as with the function being very long, splitting it into smaller functions seems to be the most effective method to improve adaptability.3. Validation
How did you trigger the refactored code path from the UI?
There were two ways I was able to trigger the refactored code path from the UI. The main method was to reload any page on the site, as
getTidswould be run in order to recalculate the number of unread topics, which is displayed in the sidebar (as shown in the first screenshot below). The second method was to click on the navigation button in the sidebar to open the page with unread topics (as shown in the second and third screenshots below).Attach a screenshot of the logs and UI demonstrating the trigger.

Method 1: Reload

Method 2: Navigate to the unread topics page


Attach a screenshot of

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