Refactoring restructures existing code to improve organization, maintainability, and testability while preserving its intended behavior. It turns duplication, confusing syntax, or awkward boundaries into clearer code without requiring a new feature. section 6.3 section 5.9
A practical refactoring workflow includes:
- Inspect unfamiliar code first; understand its layout, responsibilities, and existing behavior before changing it. section 6.3
- Extract cohesive responsibilities into reusable components or routines, such as separating an AppBar and Drawer from Dashboard.tsx. section 6.3
- Remove unnecessary duplication and simplify expressions, such as replacing repeated filenames or complex regular expressions with stored values and parsed placeholders. section 8.3.4
- Refactor tests alongside production code by generalizing repeated setup and verification into a shared routine. section 8.5.1
- Revisit design assumptions when implementation exposes integrity or cleanup problems; refactoring can reveal requirements that need reevaluation. section 5.9
Extracting dashboard navigation
Dashboard.tsx initially contains navigation logic for an AppBar and Drawer. Move them into TopMenuBar and SideBarNav components, then keep drawer state and toggleDrawer in Dashboard.tsx and pass both through props. The components become reusable on an upload page, while duplicated open-state handling remains a possible improvement. section 6.3
Listing 8.9 Refactored test
def test_incorrect_file_id_modifier(parser):
process_file_and_verify_exception( #1
ach_filename = "invalid_file_id.ach", #1
expected_exceptions_count = 1, #1
expected_exceptions_code = #1
AchExceptions.INVALID_FILE_ID_MODIFIER.value, #1
parser = parser) #1
#1
def process_file_and_verify_exception(ach_filename: str, #2
expected_exceptions_count: int, #2
expected_exceptions_code: str, #2
#2
parser: AchFileProcessor): #2
… #2
Inspect a refactored exception test that delegates repeated file processing and assertions to process_file_and_verify_exception. section 8.5.1





















