Skip to content

Refactor public/src/admin/appearance/themes.js:init to reduce complexity#65

Open
mhk2-cmu wants to merge 1 commit into
CMU-17313Q:mainfrom
mhk2-cmu:p1b-refactor-themes-init
Open

Refactor public/src/admin/appearance/themes.js:init to reduce complexity#65
mhk2-cmu wants to merge 1 commit into
CMU-17313Q:mainfrom
mhk2-cmu:p1b-refactor-themes-init

Conversation

@mhk2-cmu
Copy link
Copy Markdown

@mhk2-cmu mhk2-cmu commented Sep 7, 2025

Refactor init in public/src/admin/appearance/themes.js

Summary

This PR refactors the init function in public/src/admin/appearance/themes.js to address a high complexity code smell detected by Qlty. The original init function contained deeply nested logic for theme selection and multiple inline operations.

The refactor improves readability and maintainability by:

  • Adding a guard clause to reduce nesting (if (action !== 'use') return;).
  • Extracting a helper function getThemeInfoFrom to encapsulate DOM attribute lookups.
  • Extracting applyTheme to handle socket emission and UI updates.
  • Simplifying the main init body, which makes the control flow easier to follow.

Evidence of Improvement

Before

Qlty reported:

public/src/admin/appearance/themes.js: Function init has high complexity (count = 13).

After

After the refactor, complexity smell was removed. Qlty now reports:

public/src/admin/appearance/themes.js: Function with many returns (count = 6): init

This remaining warning is due to inline callbacks inside init (returns in nested functions). However, the structure is significantly cleaner and more maintainable.

Manual Validation

  • Rebuilt and restarted NodeBB.
  • Verified theme selection works as expected.
  • Confirmed debug log in browser console:

DEBUG: N624-debu — Themes.init triggered

  • Screenshot attached in Gradescope submission.

Issue Link

Resolves #46

@mhk2-cmu
Copy link
Copy Markdown
Author

mhk2-cmu commented Sep 7, 2025

I would like to work on this please!

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Sep 7, 2025

Great! I assigned you (@N624-debu) to the issue. Have fun working on it!

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 (public/src/admin/appearance/themes.js:init): high complexity

1 participant