Refactor (src/user/profile.js): reduce complexity of isUsernameAvailable() #161
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: #38
Full path to the refactored file: src/user/profile.js
What do you think this file does?
I think this file handles backend behaviors for the user profile pages, especially the behavior for editing a user profile and/or changing its username, etc.
What is the scope of your refactoring within that file?
My changes are contained/pertinent to function isUsernameAvailable, which gained helper functions validateUsernameFormat and checkUsernameUniqueness.
Which Qlty‑reported issue did you address?
Cognitive Complexity 12 in isUsernameAvailable().
2. Refactoring
How did the specific issue you chose impact the codebase’s maintainability?
By splitting off some of the username checking functionality into helper functions, it is now more atomic and can be extended and modified in a way that is much more understandable. With the introduction of smaller chunks of logic, the code is essentially more human-readable and is unlikely to create confusion for future maintainers, as well as being more accessible for bugfixing.
What changes did you make to resolve the issue?
I moved much of the if-then logic of isUsernameAvailable to more understandable and standalone helper functions: validateUsernameFormat and checkUsernameUniqueness, which handle erroring and other logic that previously was all incapsulated in the high-complexity function.
How do your changes improve maintainability? Did you consider alternatives?
My changes improve maintainability by making the code more readable, consistent, and equally sized. I considered other approaches that could've helped with these factors, such as a case/switch, but found that they wouldn't be appropriate for reducing the complexity, and having the one large function was the main issue with maintainability at play.
3. Validation
How did you trigger the refactored code path from the UI?
I triggered the refactored code from the UI by going to the
user/{currentUsername}/edit/usernameURL and typing in usernames (that existed, had other errors, etc), before clicking out of the input to trigger the username check phase. Doing so showsAttach a screenshot of the logs and UI demonstrating the trigger.
Attach a screenshot of

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