4 ms·
Invariants are for sanity. Debuggers are for when the invariants are found to have been violated.
by voz_ 3y ago
Invariants are for sanity. Debuggers are for when the invariants are found to have been violated.
- _a_a_a_ 3y agoInteresting. That makes sense but in my experience if an assertion is triggered because something went wrong, I tend to add more assertions 'higher up' the call chain to try to catch the cause of the assertion being triggered. Basically, to fail earlier.
- catapart 3y agoYeah, that's how I see it. I try to code small, assertion-like "invariants" into my programs via guard clauses and... assertions. When I get an error, it's usually handled by those, so I pop open the debugger, break on the exception and trace the stack back to see where I should have had a higher-level assertion or guard clause. Invariants to make debugging fast and easy (and to force me to write better code in early, malleable stages), debugger to show me exactly where I wasn't thinking it through.