SevyDevy

How to Read a Large Codebase: Entry Points, Traces and a Personal Map

, Last updated , 21 min read

Summary

  • How to read a large codebase: do not read it file by file. Pick one behaviour, find its entry point (the place where that behaviour starts) and follow the calls it makes.
  • Search before you scroll. ripgrep, a fast command-line text search, finds every mention of a word. Go-to-definition in your editor jumps straight to where a function is written.
  • Let the running program show you the path. A debugger pauses the code and prints the stack trace, the chain of function calls that led to the current line.
  • Ask git why the code looks this way. A commit is one saved change. git log -S finds the commit that added a word, and git blame names each line's last commit.
  • Beginners can stop after the safe-first-change section. Engineers get real outputs: git log --follow, which tracks a file across renames, found 345 commits where plain git log found 9. A tracing span, one timed step in a request's record, hid a redirect.

What goes wrong when you try to read a big codebase file by file?

Your attention runs out long before the files do. Reading a large codebase from the first folder down gives you hundreds of details with no question to hang them on. By day three you remember file names, not how anything works.

This guide shows how to read a large codebase the way experienced engineers do. You start from one behaviour, trace it through the code, and keep a written map. Then you check your map with the debugger, git history and a small safe change.

Picture a common setup. You join a team, and your first ticket says a login token disappears when a partner API redirects the call to another address. The service makes its web calls with requests, a popular open-source Python library for HTTP. The ticket is made up, but the library behaviour and every command output below are real, with some long lines trimmed.

So the running example is the real requests repository at release v2.34.2, the latest as of 30 October 2026. It is small enough to clone quickly, yet it has 6,466 commits of history. The same moves work on a repository a hundred times bigger, because you never read the whole thing.

Which words do you need before reading a large codebase?

You need six words: entry point, call path, symbol, stack trace, commit and span. Each one gets a plain meaning here.

  • Entry point: the first piece of code that runs for a behaviour. For a web service it is a route handler; for a library it is the function users call, like requests.get.
  • Call path: the chain of functions one behaviour passes through, from the entry point to the line that does the work.
  • Symbol: any named thing in code, such as a function, class or variable. Tools can jump to where a symbol is defined.
  • Stack trace: the list of function calls that are active at one moment. A debugger prints it, newest call at the bottom.
  • Commit: one saved change in git, with an ID, an author, a date and a message that explains it.
  • Span: one timed piece of work inside a trace. A trace is the record of one request as it moves through a system.

How to read a large codebase in six steps?

Work in a loop: ask one question, find where it starts, follow it, prove it, write it down and change something small. Each loop teaches you one path through the system. Repeat it for each new ticket, and within weeks you know the paths your work touches.

A city is a good picture. You do not learn a new city by reading every street name. You learn the route from home to office, then the market, then the station. Your map grows one trip at a time. Where the picture breaks: code can change under you overnight, so every map entry needs the commit it was true for.

  1. 1. Pick one question

    Write it as a behaviour you can test: "Why does the Authorization header vanish after a redirect?" Vague goals like "learn the auth module" give you nothing to check.

  2. 2. Find the entry point

    Look for the place a user, request or job first touches the code: a public function, a route, a command-line main or a scheduled job.

  3. 3. Search for the words

    Search for names from the question with ripgrep. Note each file and line number that looks related.

  4. 4. Jump to definitions

    Follow each call with go-to-definition or a ctags index until you reach the line that does the work.

  5. 5. Prove the path

    Run the behaviour under a debugger, stop on that line and print the stack trace. If it differs from your guess, the stack trace wins.

  6. 6. Write the map, then change something small

    Record the path with file, line and commit. Then add a test or make a throwaway change to confirm you understood it.

The reading loop

Notice the arrow back to the start. Each loop answers one question, and the map is the only thing that keeps growing.

Diagram connections:

  • One question to Entry point (where does it start?)
  • Entry point to Search and jump (follow the names)
  • Search and jump to Prove with a debugger (check the guess)
  • Prove with a debugger to Ask git history (why is it like this?)
  • Ask git history to Personal map (write it down)
  • Personal map to Safe first change (test the map)
  • Safe first change to One question (next question)
  • One question: A behaviour you can test
  • Entry point: Public function, route or main
  • Search and jump: ripgrep, go-to-definition, ctags
  • Prove with a debugger: Stack trace shows the real call path
  • Ask git history: git log -S, git blame, --follow
  • Personal map: File, line, commit, why
  • Safe first change: A test or a throwaway edit

How do you find the entry point of an unfamiliar codebase?

Start from the outside: what users call, what the server receives or what the command line runs. That outside edge is the entry point, and every path you care about starts there.

Good places to look are the README, the packaging file, the tests and any file named main or app. In a Python project, the project.scripts table in pyproject.toml names each command-line script and the function it runs. In a web service, the route table maps each URL to its handler function.

requests is a library, so its entry points are the functions users import. First measure the repository so you know what you face. Then search for the public functions.

bash
git clone https://github.com/psf/requests.git
cd requests
git switch --detach v2.34.2

git ls-files | wc -l
# 129
git log --oneline | wc -l
# 6466

rg -n "^def (get|request)\(" src/requests/api.py
# 24:def request(
# 74:def get(

The search shows that requests.get is a top-level function at line 74 of api.py. Reading those few lines shows that get calls request, and request opens a Session and calls its request method. You have your entry point and your first two hops.

How do you search a large codebase with ripgrep?

Search for the words in your question and the names you meet on the way. ripgrep, run as rg, searches every file under a folder and prints the file name and line number of each match.

By default ripgrep skips files listed in .gitignore, hidden files and binary files, according to its official guide. That skip list is why it stays fast and quiet on big repositories. Pass a folder path every time, so you always know what was searched.

bash
rg -n "def (request|send|resolve_redirects|rebuild_auth|should_strip_auth)\b" src/requests
# (long lines trimmed with ...)
# src/requests/api.py:24:def request(
# src/requests/adapters.py:128:    def send(
# src/requests/adapters.py:634:    def send(
# src/requests/sessions.py:132:    def send(self, request: PreparedRequest, **kwargs: Any) ...
# src/requests/sessions.py:154:    def should_strip_auth(self, old_url: str, new_url: str) ...
# src/requests/sessions.py:186:    def resolve_redirects(
# src/requests/sessions.py:309:    def rebuild_auth(
# src/requests/sessions.py:557:    def request(
# src/requests/sessions.py:752:    def send(self, request: PreparedRequest, **kwargs: Any) ...

rg -n "Authorization" src/requests/sessions.py
# 155:        """Decide whether Authorization header should be removed when redirecting"""
# 324:        if "Authorization" in headers and self.should_strip_auth(original_url, url):
# 327:            del headers["Authorization"]
# (four Proxy-Authorization lines, 345 to 366, trimmed)

Two searches found the likely culprit. Line 327 deletes the Authorization header when a function named should_strip_auth says yes. Notice that send appears four times in two files; the name alone does not tell you which one runs. The debugger section settles that.

ripgrep flags that do most of the work on a new codebase (ripgrep 14.1.0)
FlagWhat it doesWhen to use it
-nShows line numbersAlways, so you can jump to the line
-wMatches whole words onlyShort names like send or get
-t pySearches only one file type, here PythonMixed repositories with docs and config
-lLists matching file names onlyTo see how widely a name is used
--filesLists the files rg would search, without searchingTo check what is being skipped
--hiddenAlso searches hidden files and foldersConfig such as .github workflows

A quick check that a name is used where you think: rg -l -w should_strip_auth . returned two files, sessions.py and tests/test_requests.py. The test file is now on your reading list.

How do go-to-definition, ctags and language servers help?

They answer "where is this defined?" in one keystroke instead of one search. Text search finds every mention of a word. A definition jump finds the one place the symbol is created.

Many editors get this from a language server. The Language Server Protocol, or LSP, is a standard way for an editor to ask a language tool questions. The editor sends a textDocument/definition request with the file and the position you clicked. The server replies with the file and position of the definition.

The LSP specification also has find-references and, since version 3.16, call hierarchy requests. Call hierarchy lists who calls a function and what it calls. That is a call path, built for you.

When no language server is set up, Universal Ctags builds a plain index file called tags. Vi-style editors read that file to jump to definitions. The -x option prints a readable list instead.

bash
ctags -R -f tags src/requests
ctags -x src/requests/sessions.py | awk '/strip_auth|rebuild_auth/ {print $1, $3, $4}'
# rebuild_auth 309 src/requests/sessions.py
# should_strip_auth 154 src/requests/sessions.py

Both tools read the code without running it. In dynamic languages like Python, the method that really runs can depend on runtime values. That is why the next step runs the code.

How do you trace one request through the code with a debugger?

Run the behaviour, pause it on the line you suspect and print the stack trace. The stack trace is the call path the program really took, so it ends every argument about which send runs.

For the ticket, a tiny local server on 127.0.0.1 answers the first call with a 302 redirect, an HTTP reply that says "go to this other address". The new address uses the host name localhost. The script sends a fake token and prints what the second address received.

python
import requests

r = requests.get("http://127.0.0.1:8000/start",
                 headers={"Authorization": "Bearer FAKE-TOKEN"})
print(r.status_code, [h.status_code for h in r.history])
print(r.text)

# Output with requests 2.34.2 on Python 3.13:
# 200 [302]
# Authorization: None

The bug is reproduced: the token never arrived. Both names point to the same machine, but requests compares host names as text, so it treats localhost as a different host. Its quickstart page says Authorization headers are removed on a redirect off-host.

Now prove the path. Python's built-in debugger, pdb, can set a breakpoint in a file it has not loaded yet; it searches sys.path, Python's list of import folders. Line 158 is the host-name comparison inside should_strip_auth.

bash
python -m pdb demo.py
(Pdb) break requests/sessions.py:158
(Pdb) continue
(Pdb) where
(Pdb) p old_parsed.hostname, new_parsed.hostname
# ('127.0.0.1', 'localhost')

# Frames printed by where, trimmed to file(line)function:
# demo.py(3)
# requests/api.py(87)get()
# requests/api.py(71)request()
# requests/sessions.py(651)request()
# requests/sessions.py(805)send()
# requests/sessions.py(273)resolve_redirects()
# requests/sessions.py(324)rebuild_auth()
# requests/sessions.py(158)should_strip_auth()

The call path the debugger printed

Read it top to bottom. The redirect loop starts inside Session.send, a step you would not guess from api.py alone.

Diagram connections:

  • demo.py line 3 to api.get
  • api.get to api.request
  • api.request to Session.request
  • Session.request to Session.send
  • Session.send to resolve_redirects (302 received)
  • resolve_redirects to rebuild_auth
  • rebuild_auth to should_strip_auth (ask before deleting)
  • demo.py line 3: Your code calls requests.get
  • api.get: api.py line 87: calls request
  • api.request: api.py line 71: opens a Session
  • Session.request: sessions.py line 651: prepares, sends
  • Session.send: sessions.py line 805: walks redirects
  • resolve_redirects: sessions.py line 273: one loop per hop
  • rebuild_auth: sessions.py line 324: may delete header
  • should_strip_auth: sessions.py line 158: host names differ?

What just happened? The program drew the call path for you, and it matched the search results except for one detail: the redirect handling sits inside Session.send.

Seven hops, and you read only the lines on the path. If your service is not in Python, the same move works with your language's debugger. You can also call a stack-printing function such as Python's traceback.print_stack at the suspect line.

What should your personal codebase map contain?

A personal map is a short notes file that records the paths you have proven, not a copy of the code. Each entry answers one question and points to exact lines at an exact commit.

Keep it in plain text next to your work, one entry per question. Five lines per entry is enough. The commit ID matters most, because line numbers drift as the code changes.

One map entry for the running example (written against v2.34.2, commit 6e83187b)
FieldEntry
QuestionWhy does the Authorization header vanish after a redirect?
Entry pointrequests.get in src/requests/api.py line 74
Call pathget, request, Session.request, Session.send, resolve_redirects, rebuild_auth, should_strip_auth
Key linesessions.py line 158 compares host names as text; line 327 deletes the header
Why it existsshould_strip_auth came in v2.20.0 (CVE-2018-18074); host-name stripping is older, from 2.3.0 (CVE-2014-1829)
Proofpdb stack trace and a local redirect test; both on 30 October 2026
Open questionsDoes our partner really redirect to a new host name? Check their logs.

Draw the map too. A box per module and an arrow per call you proved is enough. Add boxes only after you have seen the code run through them; guessed boxes are how maps start lying.

What are safe first changes that teach you the system?

A safe first change is one that cannot hurt users but proves your map. The two best kinds are a test that pins current behaviour and a throwaway edit you break on purpose.

Pin the behaviour with a characterisation test

A characterisation test records what the code does today, right or wrong. It turns a note in your map into a check that fails if anyone changes the behaviour.

python
import requests


def test_auth_dropped_when_host_name_changes():
    s = requests.Session()
    # Same machine, different host name: still treated as a new host.
    assert s.should_strip_auth("http://127.0.0.1:8000/a", "http://localhost:8000/b")


def test_auth_kept_on_same_host_https_upgrade():
    s = requests.Session()
    assert not s.should_strip_auth("http://example.com/a", "https://example.com/b")

# pytest 9.2.0 output: 2 passed

Break it on purpose, then put it back

Change the line you think matters, run the tests and watch what fails. If nothing fails, either the line does not matter or the tests do not cover it. Both are worth a map note.

bash
# setup once: pip install -e . plus the test group in pyproject.toml
sed -i "158s/!=/==/" src/requests/sessions.py
python -m pytest tests/test_requests.py -k should_strip_auth -q
# 6 failed, 2 passed, 331 deselected

git restore src/requests/sessions.py
python -m pytest tests/test_requests.py -k should_strip_auth -q
# 8 passed, 331 deselected

Six failures confirm the host-name line is central. The two passes are a finding too: those tests still pass with the check inverted. Do this only in your local copy, and run git status before you commit anything.

Other changes that teach without risk

  • Fix a misleading comment or docstring you met on your path. Review comments will tell you how the team thinks.
  • Add a log line or a clearer error message at a point your trace passed through.
  • Write the short design note your map entry already contains, and ask a maintainer to correct it.

For the ticket, the fix belongs in your service, not in requests. Either call the redirect target directly, or handle the redirect yourself and decide when to send the token again.

For engineers

Beginners can stop here. You now have the full loop: question, entry point, search, debugger, map and a safe change. The next two sections are for engineers: git history for the why, and tracing across services.

How does git history explain why code looks the way it does?

Every line was added for a reason, and the commit message often says what it was. Three git commands answer most "why" questions: git log -S, git blame and git log --follow.

Find the commit that added a name with git log -S

git log -S lists commits that change how many times a string appears. In practice that means commits that added or removed it. The git documentation calls this the pickaxe.

bash
git log -S "should_strip_auth" --reverse --format="%h %ad %s" --date=short
# 857e9b7a 2018-08-14 Rework authorization stripping logic as discussed
# ea9436a5 2018-10-28 proper handling for default ports in auth stripping

git tag --contains 857e9b7a --sort=v:refname | head -1
# v2.20.0

The first release with the function is v2.20.0. The project's HISTORY.md file says that release removed the Authorization header on https-to-http redirects on the same host name, listed as CVE-2018-18074. A CVE is a public ID for a security bug. The commit arrived through pull request 4718, the fix the CVE record links to.

One catch: the host-name check itself is older than the function. HISTORY.md lists stripping on redirect under 2.3.0, for CVE-2014-1829. The 2018 work moved it into should_strip_auth and added the scheme and port rules. Either way, the stripping is a security fix, not an accident.

Use git log -G when you care about a line being edited, not added. -G matches added or removed lines against a pattern. On this repo, -G for the host-name check found three commits, including a feature that was added and then reverted.

See who changed each line last with git blame

git blame prints, for each line, the commit that last changed it. Use -L to limit it to a few lines and -s to hide author names and times when you only want commit IDs.

bash
git blame -s -L 158,159 src/requests/sessions.py
# 09fd857eb requests/sessions.py 158)         if old_parsed.hostname != new_parsed.hostname:
# 857e9b7ac requests/sessions.py 159)             return True

Look at the path column: requests/sessions.py, not src/requests/sessions.py. The git blame documentation says blame follows whole-file renames automatically. To follow lines copied or moved from other files, add the -C option.

Keep history across renames with git log --follow

git log with a file path shows only commits made under that exact path, and on a big codebase files move often. The --follow option continues past renames, but it works for one file at a time.

bash
git log --oneline -- src/requests/sessions.py | wc -l
# 9
git log --oneline --follow -- src/requests/sessions.py | wc -l
# 345
git log --format="%h %ad" --date=short --diff-filter=R --follow -- src/requests/sessions.py
# d63e94f5 2023-08-13
# f3190068 2011-08-16

Without --follow you would think the file was 9 commits old. With it you see 345 commits back to 2011, across two renames. In 2011 session.py became sessions.py, and in 2023 the file moved into the src folder.

Which git command answers which question (outputs from requests v2.34.2, git 2.43.0)
Your questionCommandWhat it told us
When did this name appear?git log -S NAMEshould_strip_auth arrived in 857e9b7a, 14 August 2018
Which release shipped it?git tag --contains COMMITv2.20.0
Why is this line here?git blame -L START,END FILELine 158 last changed in 09fd857e
What is the file's full story?git log --follow -- FILE345 commits instead of 9
How did this function change?git log -L :NAME:FILESix commits, with the python diff driver on
Who knows this area?git shortlog -sn HEAD -- FILECommit counts per author for that file

When do you need distributed tracing with OpenTelemetry?

You need it when one user action crosses several services, so no single debugger can see the whole path. OpenTelemetry is an open standard and set of libraries for recording traces, metrics and logs.

The OpenTelemetry docs describe a trace as the big picture of what happens when a request is made. Each span is a unit of work. The root span covers the whole operation and has no parent. Context propagation carries the trace ID between services, by default in the W3C traceparent header.

For reading code, a trace is a stack trace for a whole system. If the team already exports traces, open one for your behaviour before you open any file. It names the services and calls in order, with timings.

Traces have blind spots, though. This script instruments requests and wraps the call in a parent span.

python
import requests
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
from opentelemetry.instrumentation.requests import RequestsInstrumentor

exporter = InMemorySpanExporter()
provider = TracerProvider()
provider.add_span_processor(SimpleSpanProcessor(exporter))
trace.set_tracer_provider(provider)
RequestsInstrumentor().instrument()

tracer = trace.get_tracer("map-demo")
with tracer.start_as_current_span("fetch-profile"):
    requests.get("http://127.0.0.1:8000/start",
                 headers={"Authorization": "Bearer FAKE-TOKEN"})

for span in exporter.get_finished_spans():
    print(span.name, span.attributes.get("http.status_code"),
          span.attributes.get("http.url"))

# Output (opentelemetry-sdk 1.45.2, instrumentation 0.66b2):
# GET 200 http://127.0.0.1:8000/start
# fetch-profile None None

The call made two HTTP requests, a 302 and then a 200. The trace shows one GET span, with the first URL and the final status. The redirect hop, the exact place our token vanished, is invisible.

The source explains why. In instrumentation version 0.66b2, the wrapper replaces Session.send and runs the original inside suppress_http_instrumentation. The redirect's inner send is therefore not recorded. This is not described on the instrumentation's docs page as of 30 October 2026; pin your version.

In the interview

How do you explain reading a large codebase in a system design interview?

Interviewers ask this as "You join a team with a large system. How do you get productive?" or "Walk me through how you debugged something you did not write." Answer with a method and one real example, in about two minutes.

  1. Start with a question

    Say you pick one real behaviour, such as a ticket, rather than reading the code top down.

  2. Name the entry point

    Explain how you find it: routes, public functions, main files, tests and the README.

  3. Show your tools

    Search with ripgrep, jump with go-to-definition, and confirm with a debugger stack trace or a distributed trace.

  4. Use history for the why

    Mention git log -S, git blame and git log --follow, and that commit messages often link the original discussion.

  5. Keep a map

    Describe your notes: question, entry point, call path, key lines, commit ID and open questions.

  6. Prove it with a safe change

    Close with a characterisation test or a small fix that shipped, and what it taught you.

Questions people ask

How long does it take to understand a large codebase?

There is no fixed number, and you never understand all of it. Aim to understand the paths your work touches. If each question takes one reading loop, a few loops a week builds a useful map within your first month. Measure progress by questions answered with proof, not by files opened.

Should I read the documentation or the code first?

Read the README and any architecture notes first, but only for an hour. They tell you the vocabulary and the main parts. Then switch to the code with one question, because docs can be out of date. When the docs and the stack trace disagree, believe the stack trace and fix the docs.

Is ripgrep better than grep for reading code?

For code, ripgrep is usually more convenient because it searches folders recursively by default. It also skips files in .gitignore, hidden files and binary files unless you ask for them. Plain grep works everywhere, including servers where you cannot install tools. The search method in this guide works with either.

What is the difference between git log -S and git log -G?

git log -S finds commits that change how many times a string appears, so it finds where a name was added or removed. git log -G finds commits whose added or removed lines match a pattern. Use -G when a line was edited but the name count stayed the same.

Why does git blame show a different file path than the one I asked about?

Because the file was renamed, and git blame follows whole-file renames automatically. In the requests repository, blame on src/requests/sessions.py shows lines from requests/sessions.py, the path before the move to the src folder. Add -C if code was moved from another file without a whole-file rename.

Do I need OpenTelemetry to read a codebase?

No. For one program, a debugger stack trace gives a more exact call path. OpenTelemetry helps when a request crosses several services, because a trace shows the order of services and their timings. Remember its blind spots: in our run, a redirect inside one requests call did not get its own span.

What should a beginner change first in a new codebase?

Add a test that pins a behaviour you have just traced, or fix a misleading comment on that path. Both are small, easy to review and hard to break things with. They also show your teammates that you understood the code, which earns you bigger tasks.

Keep learning

Practise it with Lala

A question on this guide, hints and a review.

Start a mock interview
Sources (21)