Your first ticket
It is your second day in a new job. A ticket arrives: "The discount code SAVE10 gives the wrong price."
The code has 400 files, and you wrote none of them. Nobody expects you to read them all. You only need to find the few lines that matter.
By the end of this lesson
Note
I can find where a feature lives in someone else's code.
Key words
| Word | What it means | Example |
|---|---|---|
| Codebase | all the code for one product | |
| File | one document of code in the codebase | orders.py |
| Function | a named block of code that does one job | order_total |
| Call | one line that runs a function | price_of(item) |
| Entry point | where the program starts running | |
| Search | finding every place a word appears in the code |
The cells students fill in are left blank in this preview.
Names tell you where to look
Good code uses names that say what a thing does. A file called orders.py is about orders. A function called apply_discount changes a price.
So start with the words in the ticket. The ticket says "discount". Search the codebase for that word. Most editors do this with Ctrl+Shift+F.
Worked example: searching for "discount"
| File | Line | What the line says | Worth reading? |
|---|---|---|---|
| orders.py | 8 | def apply_discount(total, code): | Yes: the function itself |
| orders.py | 17 | return apply_discount(total, code) | Yes: where it is called |
| emails.py | 42 | "Your discount has been applied" | No: only words in an email |
| README.md | 5 | Discounts are set by the shop owner | Maybe: background |
The cells students fill in are left blank in this preview.
Follow a call to where the work happens
Search finds a starting point. Then you follow the calls.
Start at the entry point, the line that runs first. See which function it calls. Go to that function and see what it calls in turn.
Stop when you reach the lines that do the job in the ticket.
Try it: run the shop code
# orders.py: a small shop's order code, written by somebody else.
def price_of(item):
prices = {"mug": 800, "tea": 450, "teapot": 2400}
return prices[item]
def apply_discount(total, code):
if code == "SAVE10":
return total - total // 10
return total
def order_total(items, code):
total = 0
for item in items:
total = total + price_of(item)
return apply_discount(total, code)
# Prices are in pence, so 2560 means 25.60 pounds.
print(order_total(["mug", "teapot"], "SAVE10"))
A common mistake
Warning
Many people think they must understand the whole codebase before changing anything. You only need the path from the entry point to the lines in the ticket. Experienced engineers work this way every day.
Practise: follow the delivery fee
def price_of(item):
prices = {"mug": 800, "tea": 450, "teapot": 2400}
return prices[item]
def delivery_fee(total):
if total >= 3000:
return 0
return 399
def checkout(items):
total = 0
for item in items:
total = total + price_of(item)
fee = delivery_fee(total)
print("Goods:", total, "Delivery:", fee)
return total + fee
print(checkout(["tea", "mug"]))
# Change the order to ["teapot", "mug"] and press Run.
# Which function decides that delivery is now free?