Defensive programming: Lessons from a toilet and the isProcessing flag
Discover how defensive programming and the use of flags in JavaScript can protect the integrity of your applications, based on real-world logic.

The logic behind a robust interface
What do a toilet control panel in Japan and a state variable in JavaScript have in common? While it may seem like an unusual comparison, both systems solve the same fundamental problem: managing uncertainty. As a developer in training and a truck driver, I have learned that effective software design is often based on programming principles that already exist in our daily lives.
While developing my application DPT (Driver Personality Test), I noticed that if a user pressed the answer buttons rapidly, the application would behave erratically. To prevent this, I implemented a simple flag called isProcessing.
The power of defensive programming
The implementation is simple but vital. By setting isProcessing = true at the start of an event and returning it to false upon completion, we block any additional input during the process. This not only prevents duplicate records but also ensures that the interface does not break under stress. It is the digital equivalent of a blinking toilet light that, when activated, signals that the system is busy and will not accept further input.
"Do not assume that the user will behave perfectly. Design for the possibility that the unexpected will happen."
From the road to the code
As a professional driver, I apply this same philosophy on the road. The safety distance I maintain from other vehicles is, in essence, a safety buffer to react to unpredictable events. In software development, defensive programming does the same: it creates a margin of maneuver for network errors, multiple clicks, or API failures. If you are interested in learning more about how modern architecture helps manage these complex flows, you can check out our guide on Desarrollo de Agentes de IA con el nuevo Go ADK: Guía 2026.
Best practices: Beyond the boolean flag
Although a boolean is sufficient for small tasks, larger-scale projects require a more structured approach. Here are some tips to improve your code:
- Use
try...finally: Ensure that theisProcessingstate returns tofalseeven if an error occurs, preventing the interface from becoming permanently locked. - State modeling: Instead of simple flags, consider a state object (
IDLE,SAVING,ERROR) to have full control over the application's lifecycle. - Visual feedback: As I learned with ripple animations, clear visual feedback reduces user anxiety and prevents them from pressing the button repeatedly out of pure uncertainty.
Conclusion
Excellence in development lies not only in writing elegant code, but in anticipating how the real world—with its impatient users and unstable connections—will interact with your creation. Like truck maintenance or using a control panel, software must be resilient by design. The next time you face an interface problem, ask yourself: what blinking light or what flag do I need to protect my system?
Original source: Dev.to
Related articles
27 de julio de 2026
Programació defensiva: lliçons d'un vàter i el flag isProcessing
Descobreix com la programació defensiva i l'ús de flags en JavaScript poden protegir la integritat de les teves aplicacions, basant-se en la lògica del món real.
27 de julio de 2026
Programación defensiva: lecciones de un inodoro y el flag isProcessing
Descubre cómo la programación defensiva y el uso de flags en JavaScript pueden proteger la integridad de tus aplicaciones, basándose en la lógica del mundo real
26 de julio de 2026
Desenvolupament d'Agents d'IA amb el nou Go ADK: Guia 2026
Explorem les actualitzacions clau del Go Agent Development Kit (ADK) v1.5.1, optimitzant la creació i desplegament d'agents intel·ligents al núvol.
26 de julio de 2026
Developing AI Agents with the new Go ADK: 2026 Guide
We explore the key updates in the Go Agent Development Kit (ADK) v1.5.1, optimizing the creation and deployment of intelligent agents in the cloud.
Loading comments...