Refactor (public/src/topics/posts.js:108): Reduce High Complexity in Topics.addPostData #253
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.
The file being worked on handles topic posts, including all the a user puts inside a new topic.
The scope of this refactoring covers the Topics.addPostData function, which takes one or more posts adds all the extra information needed to display them the forum or via API.
The specific issue with Topics.addPostData was high function complexity.
This issue impacted the codebase's adaptability by making it harder to extend and modify by requiring it add more to the same long function, increasing the risk of bugs there and elsewhere. It also meant we couldn't test specific parts in isolation, like only bookmarks or user info.
To address this issue, I changed the Topics.addPostData function to simplify it by adding helper functions that each take in fewer arguments. The context constant means fewer arguments are passing from the getPostEnhancements helper function into the changePost helper function. These functions are split off from addPostData to increase understandability and decrease function complexity. These changes have been tested and verified with qlty smells, npm lint, and npm test.
These changes create specific helper functions for specific responsibilities, meaning if someone wants to change one part, they can do that in isolation and test it in isolation. It improves readability because of fewer arguments into functions and specialized functions.
I triggered the code path by creating a new topic with data.