Refactoring improves maintainability by restructuring code around clearer responsibilities and more flexible reuse. A function that calculates, formats, and prints results is difficult to reuse; separating those jobs keeps the core calculation independent from its eventual output destination. section 1.3.2
Separate calculation from presentation
Instead of one function that performs a complex calculation, formats the result, and prints it, create a calculation function that returns raw results and a formatting function that prepares them for display. The same results can then support a screen, web page, printer, or API response without changing the calculation. section 1.3.2
Use these signals to decide when a refactoring is worthwhile:
- Split a function when its explanation contains “and,” because that often signals multiple responsibilities. section 1.3.2
- Break up functions that are too long to comprehend easily; around 25 lines is a guideline, not a strict limit. section 3.2.5
- If several extracted functions operate on the same data, consider grouping them as methods of a class. section 3.2.5
- Choose names that reveal each function’s single task, making the resulting code easier to read and reason about. section 3.2.5
Refactoring is not about following a rigid function-length rule. It is a practical response to code that has become difficult to understand, reuse, or adapt. Small, focused functions make the larger application easier to maintain. section 3.2.5





















