Skip to content

refactor(arithmetic): extract shared unit handler and dimension validation#3662

Closed
Wolfvin wants to merge 1 commit into
josdejong:developfrom
Wolfvin:refactor/arithmetic-decomposition-cohesion
Closed

refactor(arithmetic): extract shared unit handler and dimension validation#3662
Wolfvin wants to merge 1 commit into
josdejong:developfrom
Wolfvin:refactor/arithmetic-decomposition-cohesion

Conversation

@Wolfvin

@Wolfvin Wolfvin commented Jun 13, 2026

Copy link
Copy Markdown

What was refactored and why

This PR extracts duplicated code from src/function/arithmetic/ into focused, single-responsibility modules.

1. unitArithmeticHelper.js (NEW)

addScalar.js and subtractScalar.js both 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 shared createUnitScalarHandler() function with named helpers validateUnitOperand() and validateMatchingUnitBases().

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 with validateMatrixMultiplicationDimensions(), validateLeftVectorDimensions(), and validateLeftMatrixDimensions() — each self-documenting and independently testable.

Benefits

  • DECOMPOSITION: Large inline blocks replaced with focused modules
  • COHESION: Unit arithmetic and dimension validation each in their own file
  • NAMING: Function names now describe exactly what they do
  • SINGLE RESPONSIBILITY: Each file has one job
  • REDUCE COUPLING: addScalar/subtractScalar no longer depend on Unit internals directly

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

Cluster Before After Status
algebra-derivative 3wh099c 3wh099c MATCH
algebra-simplify 3zhoitm 3zhoitm MATCH
arithmetic-add 13mxb0z 13mxb0z MATCH
arithmetic-multiply 2ja6uoq 2ja6uoq MATCH
arithmetic-subtract 4pcst6q 4pcst6q MATCH
arithmetic-divide 4nyotyv 4nyotyv MATCH
arithmetic-gcd 4x1co0j 4x1co0j MATCH
matrix-det 1wp9fs6 1wp9fs6 MATCH
matrix-inv 39qccqt 39qccqt MATCH
trig-sin 5m8e79v 5m8e79v MATCH
(15 more) ... ... MATCH

Chain hashes before/after: ALL MATCH

Chain Before After Status
arithmetic-flow 4rhfnxr 4rhfnxr MATCH
matrix-operations 3zyy1q0 3zyy1q0 MATCH
trig-identity 21nepps 21nepps MATCH
expression-pipeline ygbg17t ygbg17t MATCH

Verified using the Regrets behavioral regression testing tool — 25 clusters, 5-run drift detection (all STABLE), 4 chain validations.

…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
@Wolfvin Wolfvin closed this by deleting the head repository Jul 4, 2026
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