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
30 de septiembre de 2026
Direct API vs. SDK: Security in Password Recovery
Optimize password recovery in fintech: Direct API or SDK. We analyze pros, cons, and the best programming strategy.
23 de septiembre de 2026
How to deploy Typebot: The open-source chatbot builder
Learn how to deploy your own instance of Typebot, the open-source tool for creating conversational forms with full data control.
16 de septiembre de 2026
CetinLM: The Revolution in Programming and Efficient AI
CetinLM challenges the myth that AI requires multi-million dollar infrastructure, proving that precision engineering outperforms brute force.
9 de septiembre de 2026
Mastering Microsoft Entra: An Essential Guide to the SC-900 Certification
Learn the pillars of cloud identity, from access management to Conditional Access, which are key to passing the SC-900 certification.
Loading comments...