Skip to content

refactor: reduce cyclomatic complexity in high-complexity functions#4

Merged
leonardomso merged 1 commit intomasterfrom
feat/reduce-cyclomatic-complexity
Jan 1, 2026
Merged

refactor: reduce cyclomatic complexity in high-complexity functions#4
leonardomso merged 1 commit intomasterfrom
feat/reduce-cyclomatic-complexity

Conversation

@leonardomso
Copy link
Copy Markdown
Owner

Summary

Refactored 4 functions flagged by gocyclo for exceeding the complexity threshold of 15, improving code maintainability and readability.

Complexity Reductions

Function Before After Improvement
runCheck() 26 ~7 73% reduction
outputText() 20 ~7 65% reduction
Format() 20 ~4 80% reduction
FindFixes() 17 ~5 71% reduction

Changes

cmd/check.go - runCheck() (26 → ~7)

  • Extract exitOnError() for consistent error handling
  • Extract scanFiles() for file scanning phase
  • Extract parseAndFilterLinks() for parsing phase
  • Extract checkLinks() for URL checking phase
  • Extract routeOutput() for output routing

cmd/check_output.go - outputText() (20 → ~7)

  • Extract printSummaryLine() for summary output
  • Extract getEmptyResultsMessage() for empty state handling
  • Extract shouldGroupResults() for display mode decision
  • Extract outputGroupedResults() and outputFlatResults()
  • Extract maybeShowIgnored() for conditional ignored display

internal/output/markdown.go - Format() (20 → ~4)

  • Extract writeHeader() for report header
  • Extract writeSummaryTable() for summary stats
  • Extract writeDeadLinksSection() with table and details helpers
  • Extract writeWarningsSection() with table and redirect details
  • Extract writeDuplicatesSection() for duplicates table
  • Extract writeIgnoredSection() for ignored URLs
  • Use fmt.Fprintf() instead of b.WriteString(fmt.Sprintf())

internal/fixer/fixer.go - FindFixes() (17 → ~5)

  • Extract buildURLToLinksMap() for URL lookup map creation
  • Extract isFixableRedirect() for eligibility check
  • Extract addOrUpdateFix() for fix map management
  • Extract createFix() for fix object creation
  • Extract applyRefInfo() for reference info detection

Testing

  • All 197 tests passing
  • 0 linter issues
  • Verified with gocyclo -over 15 . - no flagged functions from original scope

Go Report Card

This PR addresses the gocyclo warnings from Go Report Card, improving the project's code quality score.

Refactored 4 functions flagged by gocyclo for exceeding complexity threshold of 15:

cmd/check.go - runCheck() (26 → ~7):
- Extract exitOnError() for consistent error handling
- Extract scanFiles() for file scanning phase
- Extract parseAndFilterLinks() for parsing phase
- Extract checkLinks() for URL checking phase
- Extract routeOutput() for output routing

cmd/check_output.go - outputText() (20 → ~7):
- Extract printSummaryLine() for summary output
- Extract getEmptyResultsMessage() for empty state handling
- Extract shouldGroupResults() for display mode decision
- Extract outputGroupedResults() and outputFlatResults()
- Extract maybeShowIgnored() for conditional ignored display

internal/output/markdown.go - Format() (20 → ~4):
- Extract writeHeader() for report header
- Extract writeSummaryTable() for summary stats
- Extract writeDeadLinksSection() with table and details helpers
- Extract writeWarningsSection() with table and redirect details
- Extract writeDuplicatesSection() for duplicates table
- Extract writeIgnoredSection() for ignored URLs
- Use fmt.Fprintf() instead of b.WriteString(fmt.Sprintf())

internal/fixer/fixer.go - FindFixes() (17 → ~5):
- Extract buildURLToLinksMap() for URL lookup map creation
- Extract isFixableRedirect() for eligibility check
- Extract addOrUpdateFix() for fix map management
- Extract createFix() for fix object creation
- Extract applyRefInfo() for reference info detection

All 197 tests passing, 0 linter issues.
@leonardomso leonardomso merged commit 69b96fd into master Jan 1, 2026
2 checks passed
@leonardomso leonardomso deleted the feat/reduce-cyclomatic-complexity branch January 1, 2026 17:42
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