Skip to content

Refactor main.go to reduce cyclomatic complexity (gocyclo warnings) #6

Description

@aplgr

Goal

Reduce cyclomatic complexity in:

  • cmd/grovegrid/main.go function main() (reported complexity 36)
  • cmd/grovegrid/main.go function parseCSV() (reported complexity 19)

Target: get both below the Go Report Card / gocyclo warning threshold (>15), or at least significantly closer while keeping behavior identical.

Why

High complexity makes changes risky and discourages contributors. Smaller functions with clear responsibilities are easier to test and evolve.

Scope

Behavior-preserving refactor (no feature changes):

  • Extract CLI parsing / validation into a small function (e.g. parseFlags()).
  • Extract filesystem concerns (discover CSV files, sort slices, create output dir).
  • Split parseCSV() into smaller steps:
    • open/read
    • delimiter detection
    • header normalization
    • row mapping + validation
    • numeric parsing + condition mapping
  • Consider moving parsing helpers into a dedicated file/package (even within cmd/grovegrid/ is fine).

Acceptance criteria

  • Output for the same input data is identical (HTML + optional JSON), excluding irrelevant formatting differences
  • go test ./... passes
  • gocyclo warnings are reduced (ideally no warnings > 15 for these functions)
  • No new dependencies introduced

Notes / hints

If tests are missing, it’s okay to add a small regression test first (even one fixture) before refactoring aggressively.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions