Extract magic numbers to named constants#18
Open
tzermias wants to merge 1 commit into
Open
Conversation
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>
5d1eb9d to
3303392
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
characteristicReceiverto useMemoryPresetModulopkg/jiecang/height.go:
GoToHeightto useHeightConversionFactor,ProtocolPreamble1/2,ProtocolTerminator,PollingIntervalreadHeightto useHeightConversionFactorreadHeightRangeto useHeightConversionFactorpkg/jiecang/memory.go:
GoToMemoryfunctions to usePollingIntervalSaveMemoryfunctions to useSaveMemoryDelayreadMemoryPresetto useHeightConversionFactorpkg/jiecang/common.go:
isValidDatato useProtocolResponse1/2,ProtocolTerminatorBenefits
HeightConversionFactoris more descriptive than10Example
Breaking Changes
None - these are internal implementation details.
Test Results
🤖 Generated with Claude Code