Refactoring
in Java
Learn how to reduce duplication and improve clarity by restructuring existing code without changing its external behavior.
What is Refactoring?
Refactoring is the process of restructuring existing code to improve its internal design while preserving its external behavior. The program does exactly the same thing before and after: you change how it is written, not what it does.
Why Refactor?
Recognizing Code Smells
A code smell is a surface-level indicator that something in your code may need refactoring. It is not a bug: the code runs fine. It is a hint that the design could be simpler or cleaner.
x, temp, or doStuff.The Golden Rule of Refactoring
The DRY Principle
DRY stands for "Don't Repeat Yourself." Every piece of knowledge or logic should have a single, unambiguous representation in your codebase.
Refactoring vs. Rewriting
| Refactoring | Rewriting | |
|---|---|---|
| Goal | Improve structure | Replace functionality |
| Behavior | Preserved | May change |
| Scope | Small, incremental steps | Large-scale overhaul |
| Risk | Low | High |
| Testing | Tests should still pass | Tests may need rewriting |
Core Techniques
How to Apply Refactoring
Example: Extract Method
Example: Rename + Extract Constant
Example: Eliminating Duplication
Example: Simplify Conditionals
The Refactoring Cycle
Common Mistakes
Refactoring Techniques Summary
| Technique | When to Use | Result |
|---|---|---|
| Extract Method | Long methods, repeated code blocks | Shorter, reusable methods |
| Rename | Unclear variable or method names | Self-documenting code |
| Extract Constant | Magic numbers in code | Named, meaningful values |
| Simplify Conditional | Complex boolean logic | Readable conditions |
| Remove Duplication | Copy-pasted logic | Single source of truth |
Question 1 of 5
What is the primary goal of refactoring?
Question 2 of 5
Consider the following Java code:
double c = p * 0.08;
double d = p + c;
A student decides to refactor by replacing 0.08 with a named constant TAX_RATE and renaming c to tax and d to totalPrice.
Which refactoring techniques did the student apply?
0.08 with TAX_RATE is Extract Constant. Changing c to tax and d to totalPrice is Rename. Both techniques were used together.Question 3 of 5
What should you always do before starting to refactor code?
Question 4 of 5
A method contains 45 lines of code. It reads a file, parses each line into an object, validates the data, calculates a summary, and prints a report. A developer refactors it into five separate methods: readFile, parseLines, validateData, calcSummary, and printReport.
Which code smell was addressed by this refactoring?
Question 5 of 5
Two classes, Dog and Cat, each contain an identical method:
public String describe() {
return name + " is " + age + " years old.";
}
A student moves this method into a shared parent class Pet that both Dog and Cat extend.
What principle does this refactoring best demonstrate?
describe() method existed in two places. Moving it to a shared parent class eliminates the duplication, following the DRY principle: every piece of logic should have a single representation.