Reading at scale
Lesson 15 A file in 60 seconds
exports, types, entry point, happy path, then stop
What you'll learn
- Read a file from the outside in: exports, imports, signatures, happy path
- Say which lines are the file's front door and which are risky
- Stop early and know where to look next
A new file can be 300 lines long. You don't need 300 lines to know what it is for. You need about 60 seconds and a few questions, asked in order.
The idea
Read a file from the outside in. 1. What it offers: the exports are its front door. 2. What it leans on: the imports tell you if it touches the database, the disk or email, and those are the risky lines. 3. The signatures: the names and inputs of its functions. 4. The happy path: follow one normal request from top to bottom. 5. Stop.
Error branches, helpers and edge cases are read only when a question needs them. The goal is not to understand the whole file. It is to know where to look when you need to.
1import express from "express";2import { db } from "../db.js";3import { sendReceipt } from "../mail.js";45const router = express.Router();6const TAX_RATE = 0.2;78function totalWithTax(items) { /* ... */ }910router.get("/:id", async (req, res) => { /* find one order */ });1112router.post("/", async (req, res) => {13 const order = await db.orders.insert(req.body);14 await sendReceipt(order);15 res.status(201).json(order);16});1718router.delete("/:id", async (req, res) => { /* cancel an order */ });1920export default router;
- Line 3 Imports tell you what the file leans on: a database and email. Those are the risky calls.
- Line 8 A helper. The name and the input are enough for now. Don't read the body yet.
- Line 13 The happy path of POST: save the order, send the receipt, answer 201. The order is saved before the receipt is sent: keep that as a question.
- Line 20 The front door. This file offers one thing, a router, and the app mounts it.
Take away
- Read a file from the outside in: what it offers, what it leans on, the signatures, one happy path.
- Then stop. Read the rest only when a question sends you there.
- Reading order is not running order: check what runs when the file loads.
Quiz: test yourself
7 questions: a quick quiz, then a review drill. Pick an answer to see why it's right.