You have just joined the team behind a neighbourhood cash book app, and your first task is already waiting: the dues page fails when it requests /api/dues/12. The repo (repository: the project folder) has hundreds of files, and you have opened none of them. Where do you start?
You don't need to read everything. Reading an unfamiliar repository is finding your way in code you have never seen. Start from its README and manifest, go on to the file that starts the app, then follow one request all the way to the file that answers it. One question is enough, then follow its trail. It's like Mr. Wiryo, the district's postman: he knows the name on every door, but he only learns what is inside a house once its door is opened. A file name is like the name on that door: what is inside shows only once you open it.
A reading order that works for any repo
- README: what the app is, and how to run it.
- Manifest:
package.jsonin a JavaScript project,composer.jsonin a PHP project. It tells you the framework (the app's main structure), the scripts (ready-made commands) and the first file. - Entry point: the file that starts the app.
- Routes: the list of URLs and the code that answers them. Each pair is a route.
- Handler: the function that answers one route, and whatever it calls.
- Tests: code that checks other code, and examples of how to use it.
- git log and git blame: git commands (git records every change to the code) that show why the code was written that way.
You don't have to do all of them. Once your question is answered, stop.
README and manifest first
This is the manifest of the cash book app, the file package.json:
{
"name": "cash-book",
"main": "index.js",
"scripts": {
"dev": "node --watch src/server.js",
"start": "node src/server.js",
"test": "vitest run"
},
"dependencies": { "express": "^4.19.2", "pg": "^8.11.3" },
"devDependencies": { "vitest": "^1.6.0" }
}
dependencieslists the libraries (code written by other people) the app uses.expressmeans an Express server (a JavaScript server framework),pgmeans the data is in a PostgreSQL database.devDependenciesare only used during development, here thevitesttest library.scriptsare the commands the author prepared, run in a terminal with npm (a tool for JavaScript projects).testandstarthave shortcuts:npm testandnpm start. Any other script runs withnpm run, for examplenpm run dev.scripts.startshows the file that starts the app:src/server.js. Themainfield here is stillindex.js, the default value fromnpm init(the command that createspackage.json), even though that file doesn't exist. For an app, trustscripts.start.- If
testholdsecho "Error: no test specified" && exit 1, that's annpm initdefault too: that repo has no tests yet.
In a Laravel project, composer.json names laravel/framework. The entry point is public/index.php, and the routes are in routes/web.php and routes/api.php. php artisan route:list shows every route with its controller (the Laravel code that answers it). Since Laravel 11, routes/api.php only exists after php artisan install:api has been run.
From URL to handler
The dues page fails when it sends GET /api/dues/12. Follow it from the entry point, src/server.js, to its route file:
// src/server.js
app.use('/api/dues', duesRoutes) // duesRoutes from './routes/dues.js'
// src/routes/dues.js
router.get('/', dues.index)
router.get('/:id', dues.show)
router.post('/', dues.store)
router.get('/:id', dues.show)means:GETto/:idis answered bydues.show.app.use('/api/dues', ...)mounts a router (a group of routes, not a network router) under a prefix (the start of the path). A route's full path is the prefix plus its path in the router:/api/dues+/:idmakes/api/dues/:id.:idmatches exactly one segment (one part of the path between two/). So/api/dues/12matches, andreq.params.id(the value of:idin the request) holds"12", a string./api/dues/12/photodoesn't match, because it has one segment too many.- The query string isn't part of the match:
GET /api/dues?month=10goes to the/route, andmonthis read fromreq.query. - The method has to match too: there is no
DELETEroute, so nobody answersDELETE /api/dues/12, and Express replies 404.
So the answer comes from dues.show, in src/controllers/dues.js. Open that file, then follow the functions it calls.
Applying is not doing
Take a guess first. In an Express repo, app.js holds app.use('/todos', todoRoutes), and routes/todos.js holds router.use(requireAuth). requireAuth is middleware: a function that runs before the handler. In which file is the login token really checked?
In neither of them. The token is checked in middleware/requireAuth.js, the file that calls jwt.verify() (a function that checks tokens). app.js only mounts the router, and routes/todos.js only applies the middleware. Both point somewhere else. Laravel is the same. A controller that writes StoreProductRequest $request doesn't write its validation rules itself: they are in the rules() function of StoreProductRequest. When a file only names something, open the file where that something is written.
Search for the text on the screen
The quickest shortcut: search for the text you see on the page or in the error message. Both terminal commands below search for Dues this month:
rg "Dues this month"
grep -rn --exclude-dir=node_modules "Dues this month" .
rg(ripgrep) searches every file and by itself skips whatever is in.gitignore(the list of files git doesn't record), usuallynode_modules(the folder of libraries) too.grep -rnsearches into folders (-r) and prints the line numbers (-n).--exclude-dir=node_modulesskipsnode_modules, because it holds other people's code.- In VS Code: Ctrl+Shift+F (Cmd+Shift+F on a Mac) searches the whole folder.
Search for a piece of the URL too, for example '/dues', to find its route.
💡 Tests are examples of use
A test's name is often more honest than a comment (a note in the code). Tests are run again and again, so a test that no longer fits the code fails right away. For example:
// tests/dues.test.js
it('adds up the months not paid yet', () => {
expect(countArrears([10000, 0, 0], 10000)).toBe(20000)
})
it('never goes below zero when a resident pays more', () => {
expect(countArrears([20000], 10000)).toBe(0)
})
The test's name is in it(...), and toBe holds the expected answer. So without opening countArrears, you already know how to call it (a list of payments per month, then the dues for one month) and what it gives back. Run npm test to see which ones pass.
💡 git log and git blame for the "why"
Code tells you what happens. The git history tells you why. Git records every change as a commit, with who made it and a message. These four commands read them:
git log --oneline -- src/controllers/dues.js # the commits that changed this file
git blame -L 40,45 src/controllers/dues.js # who, and which commit, wrote lines 40 to 45
git show a1b2c3d # one commit's message and changes
git log -S "rounding" --oneline # the commits that added or removed that text
⚠️ Quick ways to get lost in someone else's repo
- Reading top to bottom. Nobody finishes reading a big repo. Hold one question, follow its trail.
- Stopping at the file that only applies.
router.use(requireAuth)points, it doesn't check. - Forgetting the prefix. The
'/:id'route inroutes/dues.jsis really/api/dues/:id. - Searching in
node_modules,vendorordist. That's other people's libraries or build output (files made automatically), not this repo's code. - Trusting a file name. A name is only a promise, like the name on a door. The code decides.
TL;DR: Hold one question, then follow its trail. Start from the README (the file that explains the project) and the manifest (package.json or composer.json): dependencies names the framework, scripts says how to run the app (npm test, npm start, npm run dev), and scripts.start points at the entry point, the file that starts the app. From the entry point, follow one request through its route (a URL paired with a handler) to the handler, the function that answers it. The route's full path is the prefix (the start of the path) + its own path, and its method and segments (parts of the path between /) must match. A file that only applies something points at the file that does the work. Search for the text on the screen with rg or grep -rn, read tests as examples of use, and ask git log and git blame (the history of changes) why.
⚡ One question, follow its trail
Meaning: You don't need to read all of a big repo (a project folder). Start with the README (the file that explains the project), then the manifest (package.json or composer.json): dependencies (the list of libraries) names the framework, and scripts.start points at the file that starts the app. From there, follow one request to the file that answers it, and stop once your question is answered.
⚡ From URL to handler
Write:
// src/server.js
app.use('/api/dues', duesRoutes)
// src/routes/dues.js
router.get('/:id', dues.show)
Guess first: :id matches exactly one segment (one part of the path between two slashes). Is GET /api/dues/12/photo answered by dues.show?
No. After /api/dues there are two segments, 12 and photo, and that's one too many.
Meaning: app.use mounts a router, a group of routes (paths paired with handlers, the functions that answer them), under a prefix (the start of the path). A route's full path is the prefix plus the path in the router: /api/dues + /:id. So GET /api/dues/12 is answered by the handler dues.show, with req.params.id (the value of :id) holding "12".
⚡ Applying is not doing
Meaning: router.use(requireAuth) only applies the middleware (a function that runs before the handler). The login token is checked by jwt.verify() in middleware/requireAuth.js. When a file only names something, open the file where that something is written.
⚡ Search for the text on the screen
Write:
rg "Dues this month"
Meaning: rg searches every file for the text on the screen, and usually skips node_modules (other people's libraries) by itself. To learn why the code was written that way, read the history of changes with git log and git blame.