Learning & research 3 min read

Think in Code Before You Write It

Watch binary search narrow a sorted list, then use the same habit of tracing small steps when you read a programming textbook.

At a glance

Describe the input, the rule and the stopping condition before writing code. A small binary-search example makes those decisions visible and shows why a method’s assumptions matter.

An original illustrated reading scene for Small steps. Powerful ideas.
Original illustration for iReadAll, created with AI assistance.
On this page

A program is easier to understand when you can predict its next move. Before the punctuation and language rules, there is a smaller task: say what information you have, what you will do with it, and how you will know when to stop.

Imagine eight numbered cards in ascending order: 3, 7, 12, 18, 24, 31, 42 and 56. You want to find 31. You could check every card in turn. You could also use the ordering: examine a middle card, compare its value with the target, and continue only in the half that could contain the answer.

The interactive trace uses the lower middle position when there are two middle candidates. That is an implementation choice. The important rule is that the remaining interval keeps every value that could still match.

A visual guideFind 31 in a sorted list
  1. 01Compare with 18

    In [3, 7, 12, 18, 24, 31, 42, 56], choose the lower middle position. 31 is larger, so keep the upper half.

  2. 02Compare with 31

    The remaining candidates are [24, 31, 42, 56]. The lower middle is 31.

  3. 03Stop at the match

    The value is found after two comparisons. A missing value stops when no candidates remain.

Try the ideaExplore at your pace

Change one thing. See what follows. The explanation above remains available as you explore.

An original trace of binary search. Sorted input is required; these comparison counts are not a performance benchmark.

Notice the assumption doing the work

At the first comparison, the middle value is 18. Since 31 is larger, all the values up to and including 18 can be excluded. This conclusion depends on the cards being sorted. If someone shuffled them, discarding that part of the list could discard the answer.

Runestone’s binary-search chapter explains this use of an ordered list. Our eight-card trace is a separate, original teaching example. It shows comparisons rather than timing a real program.

Try an answer that is not there

Select 35 in the example. It lies between two values but matches neither. Keep narrowing the interval until there are no candidates left. “Not found” is a result the procedure must be able to reach deliberately; it should not depend on waiting forever or walking beyond the list.

When you read code, identify the condition that keeps the search running and the statement that changes its boundaries. Try a target at the beginning, at the end, and outside the list. These cases ask different questions about the same rule.

Move from a trace to a textbook

Find the part of the implementation that corresponds to each action: selecting a middle position, comparing values, updating a boundary, returning a match. Read one loop iteration slowly. Write the current values beside the code before reading the next iteration.

If the syntax is unfamiliar, choose a programming introduction first. If the syntax is comfortable but you want to compare ways of organizing data, a data-structures book is a better next step. Data science, statistical thinking and the practice of maintaining software are other branches on our shelf.

Leave with an explanation you can test

  • State what must be true of the input.
  • Explain why each discarded candidate cannot be the answer.
  • Describe both ways the search can end.

That explanation is useful even before you type a line. It gives you something to compare with the program’s behavior. Explore the computing shelf to follow the branch that interests you.

Sources & references

  1. Problem Solving with Algorithms and Data Structures: binary searchRunestone Academy · Checked 2026-11-02

Cite this guide

iReadAll Product & Editorial Team. “Think in Code Before You Write It.” iReadAll, Nov 2, 2026. https://ireadall.ai/blog/think-in-code-guide

Back to all guides