Refactoring
See csse2002 for course logistics.
Week 6 is one lecture in three parts: Refactoring, Cohesion and Coupling (Part 02), and SOLID Principles (Part 03).
Outline
- Good Practice Refactoring, When to Refactor, Code Smells
- Cohesion and Coupling
- SOLID Principles
Where refactoring sits in the development loop
Refactoring is one step in the development cycle, and the cycle is the argument for the whole topic: refactoring is a step you take only from a passing test, and it is a step you take every time round, not a clean-up project scheduled for later.
Figure 1: The development cycle. Write Test → Test Fails → Write Code → Test Passes → Refactor → back to Write Test. Refactoring is entered only from a green test and exited into writing the next test, which is why ‘have good tests before you begin’ is the second rule of good practice. The red and green washes mark the two test verdicts.
See java-testing and java-junit for the test half of that loop.
Refactoring
See java-refactoring — what refactoring is, why it’s needed, good practice, the two triggers (making a feature easier to add, and making code easier to understand), and code smells (bad naming, duplication, feature envy). Practised further in week9-lab-refactoring.
Cohesion and coupling
See java-cohesion-and-coupling — modularization, the levels of coupling (content → data) and cohesion (coincidental → functional), four worked coupling examples and two worked cohesion examples, and why high cohesion + low coupling is the goal. Practised further in week10-tutorial-coupling-and-cohesion.
SOLID principles
See java-solid-principles — why large software becomes rigid/fragile, and the first two SOLID principles: Single Responsibility and Open-Closed. The remaining three (Liskov Substitution, Interface Segregation, Dependency Inversion) are covered in 2026-04-16-solid-principles-ii.
Key takeaways
- Refactoring is a continuous process, not a one-off tidy-up.
- We need highly cohesive and loosely coupled classes.
- SOLID principles are guidelines, not rules:
- a class should have only one reason to change;
- components should be open for extension but closed for modification.
Homework
- SRP and memory management — check whether SRP improves memory management indirectly, by improving the overall design structure. Think about object creation, immutability and garbage collection. See java-mutability.