Refactoring & Decomposition
beginner12 min readLesson 26 of 169
Turn copy-paste into named functions; build programs as pipelines.
Refactoring is improving code without changing what it does. The most common beginner refactor: copy-pasted code โ functions.
Before:
print("Welcome Minh, your balance is 120")
print("Welcome Lan, your balance is 90")
print("Welcome Bo, your balance is 0")
After:
def welcome(name, balance):
print(f"Welcome {name}, your balance is {balance}")
for who, amount in [("Minh", 120), ("Lan", 90), ("Bo", 0)]:
welcome(who, amount)
Same output โ but now one place to change the message, and a name that says what the line does.
How to spot refactor targets
- The same 2+ lines appear more than once โ extract a function.
- A block does one identifiable job โ give that job a name.
- A condition so long it needs a comment โ name it (
is_valid_order(order)).
Decomposition in practice
Build programs as pipelines of small functions:
def read_input(): ...
def process(data): ...
def format_output(result): ...
def main():
data = read_input()
print(format_output(process(data)))
Each function is testable alone; main reads like the problem statement.
That is the shape every remaining project in this course uses.