Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Finding your way in a codebase

After this lesson: I can find where a feature lives in someone else's code.

45 min lessonNo commands

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

WordWhat it meansExample
Codebaseall the code for one product
Fileone document of code in the codebaseorders.py
Functiona named block of code that does one joborder_total
Callone line that runs a functionprice_of(item)
Entry pointwhere the program starts running
Searchfinding 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"

FileLineWhat the line saysWorth reading?
orders.py8def apply_discount(total, code):Yes: the function itself
orders.py17return apply_discount(total, code)Yes: where it is called
emails.py42"Your discount has been applied"No: only words in an email
README.md5Discounts are set by the shop ownerMaybe: 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

Code: python
# 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

Code: python
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?

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.