---
title: Centralize Constants
impact: HIGH
impactDescription: makes values easy to find and update
tags: constants, magic-numbers, configuration, quality
---

## Centralize Constants

Magic numbers or strings scattered throughout the code are hard to find and update. Centralizing them improves maintainability.

**Incorrect (magic numbers/strings):**

```go
func ValidateUser(password string) bool {
	if len(password) < 8 {
		return false
	}
	return true
}

func (s *Service) Retry() {
	for i := 0; i < 3; i++ {
		// retry logic
	}
}
```

**Correct (centralized constants):**

```go
package constants

const (
	MaxRetryAttempts   = 3
	MinPasswordLength  = 8
	DefaultTimeoutSecs = 30
)

const (
	StatusPending = iota
	StatusApproved
	StatusCancelled
)

// Usage
if len(password) < constants.MinPasswordLength { ... }
```

**Benefits:**
- Single source of truth
- Self-documenting
- Easy to update across the entire codebase

**Tools:** GolangCI-Lint (gocritic, gomnd), Code Review
