Problem Solving Fundamentals · Approaching a Problem

Plan before you code

planning before coding

Plan before you code

An architect does not start stacking bricks. They draw the plan first — how many rooms, where the doors go, how the roof sits — and only then hand it to the builders. The drawing is cheap; moving a wall later is not.

Code works the same way. The plan is a few lines in plain words, and it costs you nothing to change before you write a single function.

Take this task: given a list of transactions, return the final balance. Each transaction carries a signed amount, so a deposit is positive and a withdrawal is negative. Skip the syntax for a moment and say the steps out loud:

Start the balance at zero. Walk the transactions in order. Add each transaction's signed amount. When the list ends, return the balance.

That is the whole solution, and you can check it by eye. Only now do you reach for code:

let balance = 0;
for (const transaction of transactions) {
  balance = balance + transaction.amount;
}
return balance;

Writing the steps first forces out the decisions a blank editor hides: where the total starts, which direction each item moves it, and what happens when the list is empty. A plan you can read is a plan you can fix, long before it is buried in code.

You are about to answer a short quiz on planning before coding.

  1. Writing the steps down in plain words before coding can expose decisions a blank editor hides.
  2. A few lines of plain words written before the code is a ___.
  3. A plan is cheap to change before you write any code.
  4. Planning first forces you to decide where a running total ___.
  5. You should reach for syntax before you have decided the steps.
  6. Deciding what happens when the list is ___ is a question the plan makes you answer.