Skip to content
This repository was archived by the owner on Mar 15, 2026. It is now read-only.

Refactor duplicated code patterns#1

Merged
AndyH0ng merged 4 commits intomainfrom
copilot/refactor-duplicated-code
Nov 12, 2025
Merged

Refactor duplicated code patterns#1
AndyH0ng merged 4 commits intomainfrom
copilot/refactor-duplicated-code

Conversation

Copy link
Contributor

Copilot AI commented Nov 11, 2025

Eliminates code duplication across bot commands, competition handlers, and validation utilities.

Changes

  • Date update handlers: Consolidated handleUpdateStartDate and handleUpdateEndDate (~50 duplicate lines) into parameterized updateCompetitionDate helper

  • Type assertions: Extracted repeated assertion patterns into assertUserInfo and assertUserAdditionalInfo helpers (3 instances)

  • String normalization: Created normalizeString helper for strings.ToLower(strings.TrimSpace()) pattern (4 call sites)

  • Pattern matching: Unified ContainsSQLInjection and ContainsDiscordAbuse (~20 duplicate lines) via generic containsPatterns function

Example

Before:

func handleUpdateStartDate(s *discordgo.Session, m *discordgo.MessageCreate, dateStr string, competition *models.Competition) {
    errorHandlers := utils.NewErrorHandlerFactory(s, m.ChannelID)
    startDate, err := utils.ParseDateWithValidation(dateStr, "start")
    if err != nil { errorHandlers.Validation().HandleInvalidDateFormat("START"); return }
    if !utils.IsValidDateRange(startDate, competition.EndDate) { 
        errorHandlers.Validation().HandleInvalidDateRange(); return 
    }
    err = ch.commandHandler.deps.Storage.UpdateCompetitionStartDate(startDate)
    // ... error handling and success message
}

func handleUpdateEndDate(/* same logic with end date */) { }

After:

func handleUpdateStartDate(s *discordgo.Session, m *discordgo.MessageCreate, dateStr string, competition *models.Competition) {
    ch.updateCompetitionDate(s, m, dateStr, competition, true)
}

func handleUpdateEndDate(s *discordgo.Session, m *discordgo.MessageCreate, dateStr string, competition *models.Competition) {
    ch.updateCompetitionDate(s, m, dateStr, competition, false)
}

func updateCompetitionDate(s *discordgo.Session, m *discordgo.MessageCreate, dateStr string, competition *models.Competition, isStartDate bool) {
    // Single implementation handles both cases via parameterization
}
Original prompt

Find and refactor duplicated code


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits November 11, 2025 16:31
- Extract common date update logic into single helper function
- Create reusable type assertion helpers for UserInfo and UserAdditionalInfo
- Add normalizeString helper to eliminate repeated string normalization
- Consolidate pattern matching logic in ContainsSQLInjection and ContainsDiscordAbuse

Co-authored-by: AndyH0ng <60703412+AndyH0ng@users.noreply.github.com>
Co-authored-by: AndyH0ng <60703412+AndyH0ng@users.noreply.github.com>
Co-authored-by: AndyH0ng <60703412+AndyH0ng@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor duplicated code for better maintainability Refactor duplicated code patterns Nov 11, 2025
Copilot AI requested a review from AndyH0ng November 11, 2025 16:38
@AndyH0ng AndyH0ng marked this pull request as ready for review November 12, 2025 04:14
@AndyH0ng AndyH0ng merged commit e19f634 into main Nov 12, 2025
2 of 3 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants