Learn about a simple technique that will dramatically reduce the number of these bugs in your software. It won't reduce the overall number of bugs, at least not at first, but it'll make most defects much easier to find.
Citation:
Jim Shore, "Fail Fast," IEEE Software, vol. 21, no. 5, pp. 21-25, Sep./Oct. 2004, doi:10.1109/MS.2004.1331296