Refactoring improves code design, readability, or organization without changing its intended behavior. It often replaces a large task with smaller, clearer functions. section 11.6 section 11.8
Useful refactoring moves include:
- Break a process into smaller functions, such as verifying an order, calculating totals, applying discounts, and generating an invoice. section 7.8
- Extract repeated subtasks, such as returning nonempty words from a string, into reusable helper functions. section 11.6
- Move changeable decisions, such as feature weights, to a higher-level function where they are easier to find and modify. section 11.6
def process_orders(orders):
total_price = 0
for order in orders:
price = order['price']
quantity = order['quantity']
total_price += price * quantity
tax = total_price * 0.08
total_price_with_tax = total_price + tax
report = f"Total price: ${total_price:.2f}\n"
report += f"Tax: ${tax:.2f}\n"
if total_price_with_tax > 100:
discount = total_price_with_tax * 0.1
total_price_with_tax -= discount
report += f"Discount: ${discount:.2f}\n"
report += f"Total price with tax: ${total_price_with_tax:.2f}\n"
print(report)
a = {'price': 20, 'quantity': 5}
lst = [a]
process_orders(lst)
Use the order-processing example to identify separate calculation and reporting responsibilities while preserving the original output. section 7.8





















