Skip to content

Refactor (packages/ui/src/theme/context.tsx:176): Function with many returns (count = 20): init - #64

Open
mirathope wants to merge 2 commits into
CMU-313:mainfrom
mirathope:refactor-context-init
Open

Refactor (packages/ui/src/theme/context.tsx:176): Function with many returns (count = 20): init#64
mirathope wants to merge 2 commits into
CMU-313:mainfrom
mirathope:refactor-context-init

Conversation

@mirathope

@mirathope mirathope commented Sep 5, 2026

Copy link
Copy Markdown

P1B: Starter Task: Refactoring PR

1. Issue

Link to the associated GitHub issue:
#60

Full path to the refactored file: packages/ui/src/theme/context.tsx

What do you think this file does?
(Your answer does not have to be 100% correct; give a reasonable, evidence‑based guess.)
This file implements the app's theming system. It loads theme JSON files, tracks the current theme and color scheme in a reactive store, applies the theme's CSS to the page, and provides functions to switch or preview themes.

What is the scope of your refactoring within that file?
(Name specific functions/blocks/regions touched.)
I refactored the init function by extracting its nested inner functions load, applyTheme, ids/computeIds, onStorage, setTheme, setColorScheme, previewTheme, previewColorScheme, commitPreview, and cancelPreview into standalone module-level functions.

Which Qlty‑reported issue did you address?
(Name the rule/metric and include the BEFORE value; e.g., “Cognitive Complexity 18 in render()”.)
Function with many returns (count = 20) and Function with high complexity (count = 135) in init.

2. Refactoring

How did the specific issue you chose impact the codebase’s maintainability?
Since all helper functions were contained within init, any return statements within these helper functions contributed to the return and complexity of the init function, which made the function difficult to understand, test, and modify.

What changes did you make to resolve the issue?
I extracted each inner function into its own standalone, module-level function (e.g., makeLoad, makeOnStorage, makeSetTheme, makeSetColorScheme, makePreviewTheme, makePreviewColorScheme, makeCommitPreview, makeCancelPreview, computeIds), with init now just wiring them together by calling each factory and using the results.

How do your changes improve maintainability? Did you consider alternatives?
init's complexity dropped from 135 to 27 and its return count went from 20 to effectively 0 for that smell, since each extracted function now has its own small, independently readable and testable scope. I initially grouped some preview-related functions into one factory function, but that just shifted the complexity elsewhere, so I split them into separate functions instead.

3. Validation

How did you validate that the change is correct?
I ran the full packages/ui test suite before and after the refactor and confirmed all 27 pre-existing tests still pass, ran bun lint and confirmed no new warnings/errors were introduced, and added new unit tests in context.test.ts that directly exercise makeSetColorScheme, one of the functions extracted from init which all pass.

Attach a screenshot of the test coverage showing the lines were executed by the tests.
1B-TestCoverage

After bun lint: Same 698 warnings/2 errors exist on main without my changes.
after-bunlint

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

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

After:
The only remaining smell is load's many returns (6), which is unrelated to the claimed issue.
fewer-reported-issues

Before:
actual-before

@mirathope mirathope changed the title Refactor (packages/ui/src/theme/context.tsx): Function with many returns (count = 20): init Refactor (packages/ui/src/theme/context.tsx:176): Function with many returns (count = 20): init Sep 6, 2026
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.

1 participant