Skip to content

P1B: Refactor (packages/app/src/components/dialog-custom-provider-form.ts): Function with many returns: validateCustomProvider - #85

Open
MichaelYeh507 wants to merge 3 commits into
CMU-313:mainfrom
MichaelYeh507:refactor-validate-custom-provider
Open

P1B: Refactor (packages/app/src/components/dialog-custom-provider-form.ts): Function with many returns: validateCustomProvider#85
MichaelYeh507 wants to merge 3 commits into
CMU-313:mainfrom
MichaelYeh507:refactor-validate-custom-provider

Conversation

@MichaelYeh507

@MichaelYeh507 MichaelYeh507 commented Sep 6, 2026

Copy link
Copy Markdown

P1B: Starter Task: Refactoring PR

1. Issue

Link to the associated GitHub issue:
Closes #79#79

Full path to the refactored file:
packages/app/src/components/dialog-custom-provider-form.ts

What do you think this file does?
This file checks the "add custom provider" form before it gets saved. It cleans up what the user typed, figures out which fields are missing or wrong, and if everything looks good it builds the settings object the app stores for that provider.

What is the scope of your refactoring within that file?
I changed the body of the validateCustomProvider function and added three small helper functions above it (uniqueError, validateModels, validateHeaders) that now hold the model-row and header-row checks. Nothing else in the file changed.

Which Qlty-reported issue did you address?
"Function with many returns (count = 7): validateCustomProvider" at line 51. Fixing it also removed the "Function with high complexity (count = 31)" warning on the same function.

2. Refactoring

How did the specific issue you chose impact the codebase's maintainability?
The function had seven different places where it could return a value, and most of them were hidden inside nested conditionals and small inline functions. That made it hard to read the checks in order and easy to break the duplicate-detection logic when adding a new rule.

What changes did you make to resolve the issue?
I pulled the model-row and header-row checks into their own functions, and put the shared "is this blank or a duplicate?" check into one helper. The main function now just calls those helpers and keeps its original two returns at the end.

How do your changes improve maintainability? Did you consider alternatives?
Each helper does one job and can be read and tested on its own, and the duplicate check lives in one place instead of being copied twice. I considered just flattening the conditionals in place, but that would have left all the returns inside the main function and kept the copied logic.

3. Validation

How did you validate that the change is correct?
The two tests that already existed for this function pass unchanged after the refactor, which shows the output shape and error messages are the same as before. I added nine more tests that cover each field-level error (provider ID, name, base URL), the required and duplicate checks in the row helpers, and the success case with a plain API key. All 11 pass locally, and the coverage report shows 100% of the lines in the file were run. These tests run automatically in CI because the packages/app test script runs every test under src/. Lint reports no warnings or errors on the two files I changed.

Lint on the two changed files:
lint

Full packages/app unit suite:
bun_test

Attach a screenshot of the test coverage showing the lines were executed by the tests.
new_tests_coverage

Attach a screenshot showing the tests that cover the change passing during CI
new_tests_passing
image

Attach a screenshot of qlty smells --no-snippets <full/path/to/file.ts> showing fewer reported issues after the changes.

Before:
qlty_before

After:
qlty_after

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

P1B: Refactor (packages/app/src/components/dialog-custom-provider-form.ts:51): Function with many returns (count = 7): validateCustomProvider

1 participant