Skip to content

R23-T058: Refactor resistor_color_code.py to share colour lookup and validation helpers - #68

Open
code-paul-creator wants to merge 1 commit into
S-AI-Projects:masterfrom
code-paul-creator:R23-T058
Open

code-paul-creator wants to merge 1 commit into
S-AI-Projects:masterfrom
code-paul-creator:R23-T058

Conversation

@code-paul-creator

Copy link
Copy Markdown

Description

Refactors electronics/resistor_color_code.py without changing behaviour: the repeated "look the colour up in this band's table or raise ValueError" code in four functions is replaced by one shared helper, and the nested validation / resistance code is flattened. An equivalence test compares the original and refactored code to prove nothing changed.

Related Issue

Fixes: #58

Type of Change

  • Bug Fix
  • New Feature
  • UI/UX Improvement
  • Performance Improvement
  • Tests
  • Documentation
  • Refactoring / Maintenance
  • CI/CD

Changes Made

  • Added get_color_value(color, color_values, band_name) (with 2 doctests) and used it in get_significant_digits, get_multiplier, get_tolerance, get_temperature_coeffecient. Error messages are unchanged.
  • check_validity: guard clauses instead of nested if/else (nesting depth 4 -> 2).
  • calculate_resistance: guard clause, removed None placeholders, fixed local typo resisitance.
  • Original 24 doctests unchanged (26 total); all 10 raise statements kept.
  • Added work/R23-T058/REFACTOR.md, equivalence_test.py, and before/after complexity and coverage output.

Screenshots / Recordings

Not applicable (no UI changes).

Testing

  • python -m pytest --doctest-modules electronics/resistor_color_code.py -> 26 passed
  • python -m pytest work/R23-T058/equivalence_test.py -> 60 passed (original vs refactored: same results and same exception type and message on thousands of inputs and every raise path)
  • ruff check electronics/resistor_color_code.py -> all checks passed
  • McCabe complexity per function: calculate_resistance 5 -> 3, get_significant_digits 3 -> 1, get_multiplier/get_tolerance/get_temperature_coeffecient 2 -> 1, check_validity 5 -> 5 (flatter), total 22 -> 17

Checklist

  • I have tested my changes locally.
  • I have added or updated tests, if needed.
  • I have updated documentation, if needed.
  • I have followed the project's coding guidelines.
  • I have run the relevant tests and checks.
  • I have addressed previous review feedback, if applicable.

Additional Notes

check_validity stays at complexity 5 (its three error conditions and the colour loop are inherent), so the worst score is unchanged; calculate_resistance, the longest function, drops from 5 to 3. The raise ValueError("Input is invalid") at the end of calculate_resistance is unreachable (check_validity raises first); it was kept so the 10 raise paths stay identical. Details in work/R23-T058/REFACTOR.md.

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.

Paulami Bhosle — R23-T058: Refactor electronics/resistor_color_code.py (374 lines)

1 participant