Skip to content

[duplicate-code] Duplicate Code Pattern: getDefault* Env-Var Wrapper Functions in cmd/flags #1308

@github-actions

Description

@github-actions

Part of duplicate code analysis: #1305

Summary

internal/cmd/flags_logging.go and internal/cmd/flags_difc.go each contain getDefault* functions that are structurally identical 1–3 line wrappers around envutil.GetEnv*. There are 4 such functions. While individually small, the pattern is purely mechanical and signals that a table-driven approach (or direct inline calls) would remove the repetition.

Duplication Details

Pattern: getDefault* env-var wrapper functions in cmd flags files

  • Severity: Low
  • Occurrences: 4
  • Locations:
    • internal/cmd/flags_logging.go lines 34–47 (3 functions)
    • internal/cmd/flags_difc.go lines 29–31 (1 function)

All four functions follow this template:

func getDefaultX() T {
    return envutil.GetEnvY("MCP_GATEWAY_X", defaultX)
}

Concrete examples:

// flags_logging.go
func getDefaultLogDir() string {
    return envutil.GetEnvString("MCP_GATEWAY_LOG_DIR", defaultLogDir)
}
func getDefaultPayloadDir() string {
    return envutil.GetEnvString("MCP_GATEWAY_PAYLOAD_DIR", defaultPayloadDir)
}
func getDefaultPayloadSizeThreshold() int {
    return envutil.GetEnvInt("MCP_GATEWAY_PAYLOAD_SIZE_THRESHOLD", defaultPayloadSizeThreshold)
}

// flags_difc.go
func getDefaultEnableDIFC() bool {
    return envutil.GetEnvBool("MCP_GATEWAY_ENABLE_DIFC", defaultEnableDIFC)
}

Impact Analysis

  • Maintainability: Adding a new flag with an env-var default requires adding another boilerplate function; the pattern will grow as features are added
  • Bug Risk: Low — functions are too simple to diverge incorrectly
  • Code Bloat: ~12 lines of boilerplate for 4 trivial wrappers

Refactoring Recommendations

  1. Inline the calls directly — since each function is called exactly once (at cmd.Flags().*Var(...) registration), remove the helper and call envutil.GetEnv* inline:

    cmd.Flags().StringVar(&logDir, "log-dir",
        envutil.GetEnvString("MCP_GATEWAY_LOG_DIR", defaultLogDir),
        "Directory for log files ...")

    This removes 4 functions without losing readability.

  2. Or centralise defaults in a struct if the number of env-configurable flags grows significantly:

    type flagDefaults struct {
        LogDir               string
        PayloadDir           string
        PayloadSizeThreshold int
        EnableDIFC           bool
    }
    func loadFlagDefaults() flagDefaults { ... }

Implementation Checklist

  • Decide on inline vs struct approach
  • Remove getDefault* wrapper functions
  • Inline envutil.GetEnv* calls at flag registration sites
  • Verify no other callers of the helper functions

Parent Issue

See parent analysis report: #1305
Related to #1305

Generated by Duplicate Code Detector

  • expires on Mar 2, 2026, 3:03 AM UTC

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions