liveBook · Manning

Refactoring

As explained in Build Financial Software with Generative AI (From Scratch)

Refactoring is the restructuring of existing code to improve its organization or maintainability without changing its externally observable behavior.

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

How Build Financial Software with Generative AI (From Scratch) covers it

How deep it goes
Walks through extraction, simplification, and test generalization, but stops short of prescribing a comprehensive refactoring catalog.
Examples
Demonstrates navigation extraction, BDD-style syntax cleanup, regular-expression replacement, and generalized ACH exception tests.
What it assumes
Assumes enough familiarity to inspect existing components, tests, or database structures, while teaching the rationale for incremental cleanup from concrete problems.
What you can do after
Apply it via Next.js, Material UI, Python test code, or database-processing code to make existing implementations clearer and easier to test.
How it connects
Connects refactoring with component design, maintainability, unit testing, test-driven development, database design, and data integrity.

Also explained in

Compare how 22 books explain refactoring →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Financial Software with Generative AI (From Scratch). The section links point to the parts of the book it draws on.