← Forward Deployed Engineering Lesson 3 of 12

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.

Find the file where a new WhatsApp message arrives first
Tap a file.

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.

Step through one enquiry
  1. webhookreceive(request): WhatsApp sends the message here.
  2. webhookparse(request): pulls out the phone number, name and text.
  3. leadssave_lead(msg): gets the lead ready to save.
  4. leadsnormalize_phone(msg.phone): tidies the phone number.
  5. leadsfind_lead(phone): is this phone number already in the sheet?
  6. sheetsappend_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.

Run normalize_phone on each number
def normalize_phone(raw): phone = raw.replace(" ", "") # remove spaces if phone.startswith("+91"): phone = phone[3:] # drop the first 3 characters return phone

Can you predict what it does?

✏️ Practice 3.1

Remember:
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.
1. What does normalize_phone("+91 99999 01299") return?
2. What does normalize_phone("0 99999 01300") return?
3. What does 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.

Which fix would you make?
Tap a fix.

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.

Run the tests before and after the fix

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.

Write the pull request note

One more skill: reading errors.

✏️ Practice 3.2

Remember:
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.
Traceback (most recent call last): File "webhook.py", line 18, in receive save_lead(msg) File "leads.py", line 31, in save_lead phone = normalize_phone(msg["phone"])KeyError: 'phone'
1. In which file did the crash happen?
2. On which line?
3. Which key was missing from the message?

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.

Fix Kevin's bug in Google Colab

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.

Question 1You're handed a codebase you've never seen. What do you read first?
Question 2An online shop's orders sometimes get the wrong price. How do you find the code to look at?
Question 3A client says "some emails don't get sent". What do you do before changing any code?
Question 4A report crashes because one function reads "1,500" as text, not a number. Which fix is safest?
Question 5After your fix, the new tests pass but one old test now fails. What does that tell you?

What's next?

This leaves a question
How do you get a language model to do the job reliably?
Lesson 4 answers it.