No, you can't - and provably so. (http://en.wikipedia.org/wiki/Halting_problem)
Even in the real world, you have user interaction - and their usage of your software isn't predictable, even if your code mostly is. Your codebase might be totally rock-solid, but users have a way of rapidly demonstrating that your assumptions were totally off-base.
There is some art, and some engineering, but the essence of programming is design. Every decision has costs and benefits. Design decisions aren't right or wrong in the abstract, you're simply trading off one thing for another - (Memory vs. CPU, CAP, etc).
The key is figuring out which benefits and which costs are most suitable for what you're developing - or more accurately, how the software will actually be used (not necessarily the same thing).