Skip to content

Extract magic numbers to named constants#18

Open
tzermias wants to merge 1 commit into
mainfrom
feature/extract-magic-numbers
Open

Extract magic numbers to named constants#18
tzermias wants to merge 1 commit into
mainfrom
feature/extract-magic-numbers

Conversation

@tzermias

Copy link
Copy Markdown
Owner

Summary

This PR eliminates magic numbers throughout the codebase by extracting them into well-named constants, improving code readability and maintainability.

Constants Added

Protocol Constants:

  • ProtocolPreamble1 = 0xf1 (Command message preamble byte 1)
  • ProtocolPreamble2 = 0xf1 (Command message preamble byte 2)
  • ProtocolResponse1 = 0xf2 (Response message preamble byte 1)
  • ProtocolResponse2 = 0xf2 (Response message preamble byte 2)
  • ProtocolTerminator = 0x7e (Message terminator)

Conversion Factors:

  • HeightConversionFactor = 10 (Multiply height by 10 for protocol)

Memory Constants:

  • MemoryPresetModulo = 0x24 (Modulo for memory preset calculation)

Timing Constants:

  • PollingInterval = 200ms (Polling interval for height operations)
  • SaveMemoryDelay = 200ms (Delay after saving memory preset)
  • OperationTimeout = 60s (Default timeout for operations)
  • InitializationDelay = 200ms (Delay during initialization)

Files Modified

pkg/jiecang/jiecang.go:

  • Added all constant definitions
  • Updated characteristicReceiver to use MemoryPresetModulo

pkg/jiecang/height.go:

  • Updated GoToHeight to use HeightConversionFactor, ProtocolPreamble1/2, ProtocolTerminator, PollingInterval
  • Updated readHeight to use HeightConversionFactor
  • Updated readHeightRange to use HeightConversionFactor

pkg/jiecang/memory.go:

  • Updated all GoToMemory functions to use PollingInterval
  • Updated all SaveMemory functions to use SaveMemoryDelay
  • Updated readMemoryPreset to use HeightConversionFactor

pkg/jiecang/common.go:

  • Updated isValidData to use ProtocolResponse1/2, ProtocolTerminator

Benefits

  • Readability: HeightConversionFactor is more descriptive than 10
  • Maintainability: Change timing/conversion in one place
  • Self-Documenting: Constants explain the purpose of values
  • Consistency: Prevents using different hardcoded values
  • Best Practices: Follows Go convention of named constants

Example

// Before (magic numbers)
data0 := byte((int(height) * 10) / 256)
ticker := time.NewTicker(200 * time.Millisecond)
if buf[0] != 0xf2 || buf[1] != 0xf2 {
    return false
}

// After (named constants)
data0 := byte((int(height) * HeightConversionFactor) / 256)
ticker := time.NewTicker(PollingInterval)
if buf[0] != ProtocolResponse1 || buf[1] != ProtocolResponse2 {
    return false
}

Breaking Changes

None - these are internal implementation details.

Test Results

make test   # ✅ PASS
make fmt    # ✅ PASS
make vet    # ✅ PASS

🤖 Generated with Claude Code

Changes:
- Add protocol constants for preamble bytes (0xf1, 0xf2, 0x7e)
- Add HeightConversionFactor constant (value: 10)
- Add MemoryPresetModulo constant (value: 0x24)
- Add timing constants: PollingInterval, SaveMemoryDelay,
  OperationTimeout, InitializationDelay
- Replace all hardcoded values with named constants throughout codebase
- Update height.go to use HeightConversionFactor in conversions
- Update memory.go to use PollingInterval and SaveMemoryDelay
- Update common.go to use protocol constants in validation
- Update jiecang.go to use MemoryPresetModulo

Benefits:
- Improves code readability and maintainability
- Makes protocol values self-documenting
- Easier to modify timing/conversion factors in one place
- Follows Go best practices for avoiding magic numbers
- Prevents bugs from inconsistent hardcoded values

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@tzermias tzermias force-pushed the feature/extract-magic-numbers branch from 5d1eb9d to 3303392 Compare February 17, 2026 09:40
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