refactor(arithmetic): extract shared unit handler and dimension validation#3662
Closed
Wolfvin wants to merge 1 commit into
Closed
refactor(arithmetic): extract shared unit handler and dimension validation#3662Wolfvin wants to merge 1 commit into
Wolfvin wants to merge 1 commit into
Conversation
…ation Structural refactoring of src/function/arithmetic/ with verified behavioral preservation. DECOMPOSITION: - Extract unitArithmeticHelper.js from addScalar.js and subtractScalar.js Both files had identical Unit handling code (validation + clone + typed dispatch + fixPrefix). This duplicated logic is now a single shared createUnitScalarHandler() function. - Extract multiplyDimensionValidation.js from multiply.js The 75-line _validateMatrixDimensions function is now an independent module with named sub-functions (validateLeftVectorDimensions, validateLeftMatrixDimensions) for better readability and testability. COHESION: - unitArithmeticHelper.js groups all Unit arithmetic concerns together - multiplyDimensionValidation.js groups all dimension validation together NAMING: - validateMatrixMultiplicationDimensions() replaces _validateMatrixDimensions() - createUnitScalarHandler() replaces inline anonymous function - validateUnitOperand() and validateMatchingUnitBases() are self-documenting SINGLE RESPONSIBILITY: - addScalar.js: only defines scalar addition type signatures - subtractScalar.js: only defines scalar subtraction type signatures - multiply.js: only defines multiplication logic, not validation - unitArithmeticHelper.js: only handles Unit operand concerns - multiplyDimensionValidation.js: only validates dimension compatibility REDUCE COUPLING: - addScalar and subtractScalar no longer depend on Unit internals directly - multiply.js no longer contains its own validation logic - Dimension errors now come from a dedicated module with clear messages VERIFICATION RESULTS (all 4 checks GREEN): - Regrets Cluster: 25/25 GREEN - Raw Output: KEBENARAN 1 IDENTIK - Fingerprint: KEBENARAN 2 IDENTIK - Chain Hash: 4/4 MATCH
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.
What was refactored and why
This PR extracts duplicated code from
src/function/arithmetic/into focused, single-responsibility modules.1. unitArithmeticHelper.js (NEW)
addScalar.jsandsubtractScalar.jsboth contained identical Unit handling logic: validate operands, check unit base compatibility, clone first operand, dispatch via typed system, reset fixPrefix. This 12-line block was copy-pasted between the two files. Extracted into a sharedcreateUnitScalarHandler()function with named helpersvalidateUnitOperand()andvalidateMatchingUnitBases().2. multiplyDimensionValidation.js (NEW)
multiply.js(882 lines) contained a 75-line_validateMatrixDimensions()function that validated dimension compatibility for vector-vector, vector-matrix, matrix-vector, and matrix-matrix multiplication. This is now an independent module withvalidateMatrixMultiplicationDimensions(),validateLeftVectorDimensions(), andvalidateLeftMatrixDimensions()— each self-documenting and independently testable.Benefits
Verification Results
All 4 verifications GREEN after refactoring:
KEBENARAN 1 (Raw Output) vs Final Output: IDENTICAL
All 25 entry functions produce identical output before and after refactoring.
KEBENARAN 2 (Fingerprints before/after): ALL MATCH
Chain hashes before/after: ALL MATCH
Verified using the Regrets behavioral regression testing tool — 25 clusters, 5-run drift detection (all STABLE), 4 chain validations.