Fundamentals 5 min read

Why Is Programming Hard? Lessons from Buying Apples

The article uses the simple task of buying apples to illustrate why programming is challenging, highlighting the need for thorough analysis, exhaustive testing, handling edge cases, and continuously refining processes to avoid bugs and unexpected failures.

Programmer DD
Programmer DD
Programmer DD
Why Is Programming Hard? Lessons from Buying Apples

Preface

On Zhihu a question was asked: "Where does programming actually become difficult?" The answer by user DJ Hitori received many up‑votes.

The response does not discuss trivial matters like programmers buying Apple products; instead it uses buying apples as a concrete example to show how programmers must analyze problems in depth, consider every detail, anticipate both expected and unexpected situations, list all solution steps, and test comprehensively. Any omission can become a bug, leading from mild criticism to serious financial loss or safety hazards.

Original Answer

Ordinary Person

I want to buy one jin (500 g) of apples today.

Programmer

I want to buy one jin of apples today.

Because I only like Red Fuji apples, I will only buy Red Fuji apples.

The maximum price I can accept is 10 CNY per jin.

Normally one bag can hold a jin of apples, but to be safe I will bring two bags. I know three nearby fruit stores, so I will visit them in order.

Based on these conditions I designed the following apple‑buying process:

This process looks okay? Let me design some test cases to evaluate it.

Testing revealed a problem: if store 0 and store 1 both have Red Fuji apples priced below 10 CNY per jin, and store 1’s price is lower than store 0’s, I would prefer to buy from store 1, but the current process would make me buy from store 0.

To fix this, I should query the prices of all stores first, then purchase from the store with the lowest price.

After the modification I redesigned the process as follows:

Is this process now perfect? No, many issues remain.

If all three stores have Red Fuji apples but each has less than a jin, yet the combined amount reaches a jin, I should not end the process and go home; instead I should buy the apples from all three stores.

If I ask a store about its price and it confirms having Red Fuji apples, but after querying all stores that store runs out of apples, the process would try to handle non‑existent apples.

During the walk I might encounter unexpected events, such as discovering a new fruit store or a bag breaking and the apples falling, which the current process cannot handle.

There are too many problems, so I give up refining the process and decide to buy on X‑Shop instead.

Next, I will design a process for buying Red Fuji apples on X‑Shop.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

testingSoftware Engineeringproblem solvingprogramming fundamentalsProcess Design
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.