Maintenance and the end of life · lesson 2 of 7
Ideas in this lesson
Agit blamewho last changed each line, and in which commit
Bgit show <commit>the commit’s message and changes
Cgit log -S "text"every commit that added or removed that text

Reading code you didn’t write

Last updated

Most of a developer's working life is spent in code someone else wrote, often someone who has left. A year after launch, you join Pila's maintenance and find a line in take_number that looks pointless. Delete it? First find out why it's there. In this lesson you'll learn where to start in an unfamiliar codebase, follow one request from end to end, and use Git's history to find the reason behind a line.

By the end of this lesson you can

  • Find your way into an unfamiliar codebase from its README and tests
  • Follow one request through the code from start to finish
  • Use git blame, git show and git log -S to find why a line exists

Where to start

Opening files at random in a new codebase is like reading a dictionary to learn a language. Better places to start:

  • The README (lesson 5.8): what it is, how to run it, where things are.
  • Run it. Use the software as a user would, before reading how it works.
  • The tests (Stage 6). Test names say what the code is supposed to do, in sentences: test_61st_phone_gets_no_number. They're the most reliable documentation, because they fail when they're wrong.
  • The design documents and decision records (lesson 4.9), for why it's shaped the way it is.

Follow one request

Then pick one thing a user does and follow it through the code, start to finish. For Pila, a text arriving:

One text, through Pila
StepWhereDoes
1web.py: receive_text()The gateway posts the text; the function reads the phone and message
2sms.py: handle()Decides what the text asks for: a number, or a cancel
3queue_rules.py: take_number()Applies the rules: hours, one per phone, the daily limit
4storage.py: save_ticket()Saves the ticket in the database
5sms.py: reply_for()Builds the reply; the gateway sends it

One path, followed end to end, teaches you how the parts connect. Write it down as you go: next month's newcomer will thank you, and it's a good addition to the README.

Why is this line here?

The first line of take_number is phone = phone[-10:]: keep only the last ten characters of the phone number. It has no comment. It looks odd. Find out why it's there, using the history:

Git sandbox
  1. See who last changed each line of queue_rules.py, with git blame queue_rules.py
  2. Read the commit that added the line, with git show and the start of its hash
  3. Check no other commit touched it, with git log -S "phone[-10:]" --oneline

Working folder

no changes

Staging area

nothing staged

Repository

7 commits on main

You are in ~/pila, after a year of work by Marco, Bea and Jun. take_number starts with a line that cuts every phone number to its last ten characters.

Commit graph

  1. 7847ffe HEAD →main Document reply_for
  2. 367b5b5 Raise the daily limit to 80 for the bigger centres
  3. e46a2f4 Say Pila now serves six barangays
  4. 4b6c1f2 Treat +63 and 0 numbers as the same phone
  5. 48c10d0 Reply with the number by text
  6. 6c3d6f9 Give out numbers, one per phone, up to 60 a day
  7. 389ecdd Start Pila with a README
Why does the line exist?

Simple Git's Who changed this, and why goes further: blaming past formatting commits, and following a file through renames.

Ask early

The history answered this one. When it doesn't, ask. New developers often spend a day stuck on something a teammate could explain in five minutes, because asking feels like admitting they don't know. On a healthy team (lesson 9.7), asking early is expected.

A good question says what you're trying to do, what you've tried, and where you're stuck: “I'm reading take_number. Blame says the phone[-10:] line is from #23, but I can't find a test for the +63 form. Is there one, or should I add it?”

Spot the trap: the line that “does nothing”

Spot the trapMarco

Tidying queue_rules.py, Marco sees phone = phone[-10:]. “Phone numbers are already ten digits,” he says. “This does nothing. I’ll delete it.” The tests all still pass.

What happens if…

Check what you learned

Which is usually the most reliable description of what unfamiliar code is meant to do?
What does git blame show for each line?
What does git log -S "text" find?

Sources

Every claim on this page comes from these sources: the standard, the founding text of a method, a study, or the official record of a project, from the body that publishes it. Industry reports are labelled as such.

All sources for Simple Software Development