Follow the transformation
Identify genuinely duplicated logic rather than merely similar-looking lines.
Define the varying inputs as parameters and the common result as a return value.
Replace each duplicate block with a call to the new abstraction.
Refactoring changes code structure without intentionally changing observable behaviour.
Refactoring changes code structure without intentionally changing observable behaviour. When the same logic appears in several places, extracting it into a well-named function or method creates one source of truth, makes testing easier and reduces the risk that later fixes are applied inconsistently.
Learning goal: explain why Refactor Duplicated Code behaves this way, apply it to a small example, and verify the result independently. Begin by being able to justify this first step: Identify genuinely duplicated logic rather than merely similar-looking lines.
Treat this as a sequence of observable decisions rather than one opaque command. Stage 1: Identify genuinely duplicated logic rather than merely similar-looking lines. Stage 2: Define the varying inputs as parameters and the common result as a return value. Stage 3: Replace each duplicate block with a call to the new abstraction. Final checkpoint: Prefer a simple function over a class hierarchy when no persistent object state is needed.
Identify genuinely duplicated logic rather than merely similar-looking lines.
Define the varying inputs as parameters and the common result as a return value.
Replace each duplicate block with a call to the new abstraction.
# Step 1 — Define the reusable `net_amount` function; its indented body describes what happens for each call.
def net_amount(gross, discount_rate):
# Step 2 — Return the computed value to the caller so the result can be reused or tested.
return gross * (1 - discount_rate)
# Step 3 — Display the current value explicitly so the result/state can be inspected during execution.
print(net_amount(100, 0.10))
# Step 4 — Display the current value explicitly so the result/state can be inspected during execution.
print(net_amount(250, 0.20))One tested function now defines the discount calculation for both cases.
For Refactor Duplicated Code, trace the specific input through the mechanism above and independently verify one returned value, state change or side effect.
InputObjects/values supplied to the operation.StateNames or mutable objects that may change during execution.OutputReturned value, side effect, file, plot or exception to inspect.Use Refactor Duplicated Code when it answers a defined question in Object-Oriented Python and its inputs/assumptions match the current data or program state.
Reconsider Refactor Duplicated Code when the required information is unavailable, the operation would violate a validation/data boundary, or a simpler operation answers the question more transparently.
Construct a tiny example of Refactor Duplicated Code. First identify genuinely duplicated logic rather than merely similar-looking lines. Then define the varying inputs as parameters and the common result as a return value. Predict the result before execution and explain one boundary or failure case.
Which approach best demonstrates understanding of Refactor Duplicated Code?
Step 1Identify genuinely duplicated logic rather than merely similar-looking lines.Step 2Define the varying inputs as parameters and the common result as a return value.Step 3Replace each duplicate block with a call to the new abstraction.Step 4Run tests before and after the refactor.