Programming Function Design and Dry-Run Loop
Root Concept
Strong programming starts with clear function contracts. You define input/output first, then implement, dry-run, test, and refine.
CodePLU Goal
Upgrading Human Mental Models
Learn how to think in Workflows
Concept Development By codeplu.com
Programming foundations: function-first workflow
What is Function-First Programming?
Function-first programming means you define behavior before writing implementation details.
By fixing input/output rules early, you reduce ambiguity and make debugging faster.
1type Item = { price: number; quantity: number };2 3function calculateTotal(items: Item[]): number {4 // returns sum(price * quantity)5 return 0;6}7 8// Input: list of items9// Output: total order valueHow the Function Design Loop Works
Define the Contract
Specify input type, expected output type, and important edge cases.
Implement Small Logic
Write the smallest correct function body that satisfies the contract.
1function calculateTotal(items) {2 let total = 0;3 4 for (const item of items) {5 total += item.price * item.quantity;6 }7 8 return total;9}Dry Run and Validate
Trace values step-by-step, then add tests for normal and edge conditions.
Real World Example
Shopping Cart Total
A junior developer needs to calculate invoice totals consistently across the app.
Contract
Input is a list of cart items. Output is one numeric total.
Implementation
Multiply price by quantity for each item and accumulate in total.
Verification
Dry-run with sample data and compare output with expected value.
1const cart = [2 { price: 10, quantity: 2 },3 { price: 5, quantity: 3 }4];5 6const total = calculateTotal(cart);7console.log(total); // 35FAQs
Final Words
When you design with a clear function contract and validate with dry runs, your code becomes easier to trust, test, and extend.