Zum Inhalt springen

Magic Number (ARCH5182)

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

Key go.quality.magic-number · Severity info · Category quality

Unexplained numeric literals inside expressions hide intent and are hard to change in every place. Extract them into a meaningful constant.

func Retry(fn func() error) error {
for i := 0; i < 5; i++ {
if err := fn(); err == nil {
return nil
}
time.Sleep(250 * time.Millisecond)
}
return errExhausted
}
const (
maxRetries = 5
retryDelay = 250 * time.Millisecond
)
func Retry(fn func() error) error {
for i := 0; i < maxRetries; i++ {
if err := fn(); err == nil {
return nil
}
time.Sleep(retryDelay)
}
return errExhausted
}

Define numeric literals as a const with a descriptive name.