Skip to content

refactor: extract magic numbers to named constants#29

Open
aicontentcreate2023-star wants to merge 1 commit intoNP-compete:mainfrom
aicontentcreate2023-star:refactor/extract-magic-numbers
Open

refactor: extract magic numbers to named constants#29
aicontentcreate2023-star wants to merge 1 commit intoNP-compete:mainfrom
aicontentcreate2023-star:refactor/extract-magic-numbers

Conversation

@aicontentcreate2023-star
Copy link
Copy Markdown

Fixes #21

Summary

Replaces hard-coded timeout and limit values scattered throughout the codebase with named constants defined in a centralized location.

Changes

Created internal/constants/constants.go

Defines all magic numbers with descriptive names and godoc comments:

  • HTTP Server Timeouts: Request (60s), Read (30s), Write (30s), Idle (120s), Shutdown (30s)
  • Pagination: DefaultPageSize (10), MaxPageSize (100)
  • Long Operation Tool: MinOperationSeconds (1), MaxOperationSeconds (60)
  • Rate Limiting: DefaultRateLimit (100), DefaultBurstSize (20) — for future use

Updated Files

  • internal/api/router.go

    • 60 * time.Second → constants.DefaultRequestTimeout
  • cmd/server/main.go

    • ReadTimeout: 30 * time.Second → constants.DefaultReadTimeout
    • WriteTimeout: 30 * time.Second → constants.DefaultWriteTimeout
    • IdleTimeout: 120 * time.Second → constants.DefaultIdleTimeout
    • context.WithTimeout(..., 30*time.Second) → constants.DefaultShutdownTimeout
  • internal/tools/long_operation_sdk.go

    • if input.Seconds < 1 → if input.Seconds < constants.MinOperationSeconds
    • if input.Seconds > 60 → if input.Seconds > constants.MaxOperationSeconds
  • test/integration_test.go

    • float64(100) → float64(constants.MaxPageSize)

Benefits

Single source of truth for all configuration values
Self-documenting code with descriptive constant names
Easier maintenance — modify timeout/limit values in one place
All constants include godoc comments explaining their purpose
Code compiles and builds successfully

Testing

✅ Verified go build ./cmd/server succeeds
✅ All magic numbers replaced consistently
✅ No behavioral changes — only refactoring

Fixes NP-compete#21

Creates internal/constants/constants.go with descriptive names for all
hard-coded timeout and limit values used throughout the codebase.

Changes:
- Created internal/constants/constants.go with:
  * HTTP server timeouts (Request/Read/Write/Idle/Shutdown)
  * Pagination limits (DefaultPageSize, MaxPageSize)
  * Long operation tool bounds (Min/MaxOperationSeconds)
  * Rate limiting constants (for future use)

- Updated files to use constants:
  * internal/api/router.go - DefaultRequestTimeout
  * cmd/server/main.go - Read/Write/Idle/ShutdownTimeout
  * internal/tools/long_operation_sdk.go - Min/MaxOperationSeconds
  * test/integration_test.go - MaxPageSize

Benefits:
- Single source of truth for configuration values
- Self-documenting code with descriptive constant names
- Easier to modify timeout/limit values in one place
- All constants include godoc comments

All magic numbers have been replaced and the code compiles successfully.
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.

Extract magic numbers to named constants

1 participant