Reading someone else's code
Map it before you touch it
Northstar already has some code. Kevin, the IT lead, wrote a small program that saves every WhatsApp enquiry as a row in the lead tracker.
All of a project's code together is its codebase. Before you change someone's codebase, learn its shape: what each file is for, and where things start.
The README file usually says what the project does. Start there, then look at the file names.
Now follow one message all the way through.
Follow one message
The fastest way to understand a codebase is to follow one real thing through it, from the moment it arrives to the moment it's saved.
Here is the path of one enquiry. Each step is a function, a named piece of code that does one job.
- webhook
receive(request): WhatsApp sends the message here. - webhook
parse(request): pulls out the phone number, name and text. - leads
save_lead(msg): gets the lead ready to save. - leads
normalize_phone(msg.phone): tidies the phone number. - leads
find_lead(phone): is this phone number already in the sheet? - sheets
append_row(…): if not, adds a new row.
So where do the duplicate leads come from?
Reproduce the bug first
In discovery you found the same person saved as two or three leads. The path shows why that matters: a lead counts as new when its tidied phone number isn't found.
Before fixing anything, make the problem happen on purpose. Then you know what "fixed" looks like.
Run Kevin's function on real numbers from the sheet. Each should come out as the same 10 digits.
Can you predict what it does?
✏️ Practice 3.1
replace(" ", "") removes spaces only. Dashes and a leading 0 stay.startswith("+91") then phone[3:] drops the first 3 characters, but only if they are exactly +91.
normalize_phone("+91 99999 01299") return?normalize_phone("0 99999 01300") return?normalize_phone("+91-99999-01301") return?The smallest safe fix
It's not your codebase. Kevin will look after it long after you've gone, so change as little as you can.
The bug lives in one function. The safest fix changes that function and nothing else.
How do you know it didn't break anything?
Prove it works
A small piece of code that checks other code gives the right answer is a test. Write one for every number that failed, and keep the ones that already passed.
Run all the tests before and after your change. The new ones should go from failing to passing, and the old ones should still pass.
Now tell Kevin what you changed.
Explain the change
You don't just change someone's code. You send a pull request: your change plus a short note, which the owner reads and accepts.
A good note says what changed, why, and what could go wrong. Kevin should be able to say yes without asking you anything.
One more skill: reading errors.
✏️ Practice 3.2
When code crashes, Python prints a traceback. Read it from the bottom: the last line says what went wrong, the line above it says where.
Do it for real
Now fix the real code. Kevin's project is in the course's GitHub repo, with a copy of the lead sheet.
In the notebook you'll send a fake WhatsApp message and watch it add Arun a fourth time. Then you'll write tests that fail, fix normalize_phone, watch every test pass, and clean up the duplicates already in the sheet.
Put your fix on GitHub with your pull request note. It's your first portfolio piece.
Colab runs Python in your browser for free. You just need a Google account. About 45 minutes.
Then check what stuck.
Lesson test
Five questions, all on new examples.