Reading Code You Didn't WriteLesson 2.1
How to read a function without running it
function signature reading, parameter types and names, return type inference, side effects vs pure functions, default parameters, destructuring in signatures
The Signature Is the Contract
Before you read a function's body, fully understand its signature. The signature tells you what the function expects and what it promises to give back — that's 80% of what you need.
Dissecting a Signature
// What does this function do before reading the body?
async function fetchUserOrders(
userId: string,
options: { limit?: number; status?: 'pending' | 'shipped' | 'delivered' }
): Promise
// Reading the signature:
// - Takes a userId (string, required) — likely a DB lookup key
// - Takes options (object, optional fields) — filtering/pagination
// - Returns a Promise of Order array — async DB operation
// Conclusion: fetches orders for one user, possibly filtered Pure vs Side-Effect Functions
A function that only uses its parameters and returns a value is pure — easy to reason about in isolation. A function that reads/writes files, makes network calls, or mutates external state has side effects — you need context to fully understand it.
// Pure — reads only its inputs
function calculateDiscount(price: number, percent: number): number {
return price * (1 - percent / 100);
}
// Side effect — mutates external state
function applyDiscount(cart: Cart, percent: number): void {
cart.items.forEach(item => {
item.price = calculateDiscount(item.price, percent);
});
}Identify which category a function belongs to before diving into its body — it determines how much surrounding context you need to understand it correctly.
