Skip to content

Fixed code smell in src/user/email.js: reduced complexity of function 'validateEmail'#317

Open
aaronlockhartdev wants to merge 1 commit into
mainfrom
alockhar/task-1-context
Open

Fixed code smell in src/user/email.js: reduced complexity of function 'validateEmail'#317
aaronlockhartdev wants to merge 1 commit into
mainfrom
alockhar/task-1-context

Conversation

@aaronlockhartdev

Copy link
Copy Markdown

Refactor: Reduce complexity of sendValidationEmail function

Changes

Refactored the UserEmail.sendValidationEmail function to reduce cyclomatic complexity from 10 to 4, addressing a Qlty code smell report.

Problem

The sendValidationEmail function had high complexity due to multiple nested conditionals and responsibility handling within a single function, making it harder to understand, test, and maintain.

Solution

Extracted four helper functions, each handling a single responsibility:

  • _normalizeOptions: Normalizes and validates the options parameter, handles backward compatibility with string arguments, and retrieves email from uid if not provided
  • _prepareConfirmationData: Prepares the confirmation data and fires filter hooks for extensibility
  • _persistConfirmationData: Handles all database operations for storing confirmation data
  • _sendEmail: Manages logging and sends the confirmation email via emailer or action hooks

The main sendValidationEmail function now has a clean, linear flow that clearly shows the validation and processing steps.

Benefits

  • ✅ Reduced cyclomatic complexity from 10 to 4
  • ✅ Improved code maintainability through separation of concerns
  • ✅ Each helper function is testable in isolation
  • ✅ Clearer intent with descriptive function names
  • ✅ No functional changes - all existing behavior preserved

Files Modified

  • src/user/email.js

Testing

All existing functionality is preserved. The refactoring maintains backward compatibility with existing callers of sendValidationEmail.

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