A dangerous mindset I’ve seen—and been guilty of—is assuming code doesn't change.
Or when it changes, the next person will understand the original context.
Reality check, they wont.
The next person (future you) has no idea what you were thinking.
🔎 A Simple Example:
You are building a service that receives JSON requests.
You write a method that takes in an array from the request and accesses index 2.
The request handler has already validated the array length and content.
So you don't need to recheck it before accessing index two, right?
It's just less efficient to check it twice, right?
Wrong.
Your original implementation might work fine, but fast forward to years later, when someone else (perhaps yourself) uses that method.
Will they always ensure the array has the right length? 🤷♂️
If they don't, your method is a ticking time bomb.
🧠 Fix the Mindset:
Embrace defensive programming, where you expect that your methods will be misused.
Recheck the array's length before you use it, even if something else has previously checked.
Expect bad inputs, expect errors to occur, and have a path to do something about it.
💡 Expect the Unexpected
If you assume the following person:
- Won't read your docs or code comments
- Will reuse your code in a different context
- Will misuse your code for things you've never designed it for
You will write safer, more resilient code.