Teaching old assert() new Tricks
blog.ngs-lang.org
blog.ngs-lang.org
Being able to use the value asserted is nice sugar though. I will take that :)
By contrast, being able to dump out the inputs to the failing expression is always useful. This is mostly discussed in the context of test-suite asserts, but should probably be generalized.
And the very next thing that will happen is me swearing up and down that I wished I captured more context at the point of error so I could understand the corner case much more easily.
assert, on failure, should dump as much function and stack context as it possibly can generate; and possibly, a string tag for _user_ convenience in reporting on and differentiating for themselves the specific error condition.
mainframes are great for this. You get a custom tagged error message that conveys the subsystem, error level, and error number along with a text error string. This makes cataloging and classifying errors automatic for everyone who sees and conveys them. Then you can use the system record store to go get the context information generated at the site of the error itself and build a much clearer idea of why the particular operation failed.
For stuff I'm just writing by and for myself, yeah, I take your approach. For software that will generate failures I may not be the first person in an organization to have to look at, I add friendly strings.
I asked if the compiler preserved the asserts when not compiling in debug mode.
What was the answer?
I for one prefer to keep asserts in production code to fail fast. Depending on the language and domain of the program you may or may not catch them with logging to keep the program running.
So this is something that one needs to be careful about and perhaps run integration/system tests with release builds.