What a function does
A customer used a 10% coupon on your $49 plan. They were charged $48.90. Your AI says the bug is 'in calculateTotal'.
This answers: How do I read a function in code? No coding needed. 3 quick decisions.
- Reading code
- Debugging without panic
- Payments and webhooks
Step 1 of 3
Prices are stored in cents, so $49 is 4900. What goes into this function, and what comes out?
function calculateTotal(items: Item[], discount: number) { let total = 0; for (const item of items) { total += item.price * item.quantity; } return total - discount;}How do I read a function in code?
Read the first line for the function's name and inputs, then the return line for its output. The lines between are the recipe. Then check the unit of every number: cents or dollars, percent or amount, seconds or milliseconds. Many bugs in small functions are unit mix-ups, and clear names plus a test with real numbers prevent them.
Breaks at Your server: 10% treated as 10 cents
Why AI tools write this bug
AI tools often name inputs vaguely, like 'discount' or 'time', and guess the unit, so two parts of the app disagree about what a number means.
How to fix it
- Name inputs with their unit, like discountPercent or priceCents.
- Check what the function returns, and in which unit.
- Add a test with real numbers you can check in your head.
- Fix the function, not each place that calls it.
Check your own app
Paste this into Lovable, Cursor or Claude Code:
Without changing anything, find the functions in this project that calculate money, dates or limits. For each, tell me its inputs, what it returns, the unit of each number (cents or dollars, percent or amount, seconds or milliseconds), and whether a test checks it with real example values.
Common questions
- Why store prices in cents?
- Computers can't store some decimals exactly, so 0.1 + 0.2 isn't exactly 0.3. Whole numbers of cents avoid rounding errors, which is why Stripe uses them too.