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
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.
1 de septiembre de 2026
Nori Robotics: Democratizing Humanoid Robot Programming
Nori Robotics launches a dual-arm humanoid robot for under $1,700, aiming to democratize robotics research and artificial intelligence.
25 de agosto de 2026
Phonebook: The open-source catalog for mobile UI previews
Discover Phonebook, the open-source tool that turns your SwiftUI and Compose previews into a static visual catalog for your entire team.
18 de agosto de 2026
AI Agents: The New Security Challenge in Modern Programming
Autonomous AI agents are transforming cybersecurity: when the reasoning model becomes the attack vector, the architecture must change.
Loading comments...