| Why are you giving your functions uncertain data? The first point your code sees uncertain data is the point that it needs to clarify what it has. The only way you get the errors you're talking about is if you ignore the above practice. Garbage in. Garbage out. For code i write, there is usually only one option for what is in a variable, the type of the data i out in there. This is true for dynamic languages as much as static. In some situations I will also allow a null value. All that means is that there was no data, and no default is wanted. Usually, I want a default. Closing off all entry points for uncertain data you need to make assumptions about is the first port of call when dealing with other people's or legacy code. It's the way to reason about code without a debugger. If you can't reason about code without a big question mark above every piece of data you need a debugger. |
If your mental model of how the code that you're interfacing with works is wrong, then it won't help you. Your data validation will error out and you might find that it was too much too early. Not only do you have logic based on flawed assumptions but check code as well.
Understanding the code base you're programming against, that's a skill that can be improved but hoping to guard against all misunderstandings is probably unrealistic. Given that, following a trace may help you identify disagreements with your model more quickly.