Programming Function Design and Dry-Run Loop

Author: codeplu.com
Last Updated: 07 Mar 2026
Est. Duration: 12 min
Skill Level: Beginner

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 Playground
Code Logo Only

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.

Concept Definition: Function Contract
typescript
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 value

How the Function Design Loop Works

1

Define the Contract

Specify input type, expected output type, and important edge cases.

2

Implement Small Logic

Write the smallest correct function body that satisfies the contract.

javascript
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}
4

Dry Run and Validate

Trace values step-by-step, then add tests for normal and edge conditions.

Real World Example

From vague requirement to verified function.

Shopping Cart Total

A junior developer needs to calculate invoice totals consistently across the app.

1

Contract

Input is a list of cart items. Output is one numeric total.

2

Implementation

Multiply price by quantity for each item and accumulate in total.

3

Verification

Dry-run with sample data and compare output with expected value.

Real World Example: Dry Run + Test
javascript
1const cart = [2  { price: 10, quantity: 2 },3  { price: 5, quantity: 3 }4];5 6const total = calculateTotal(cart);7console.log(total); // 35

FAQs

Final Words

When you design with a clear function contract and validate with dry runs, your code becomes easier to trust, test, and extend.

Next Concepts