nayuki / QR-Code-generator

High-quality QR Code generator library in Java, TypeScript/JavaScript, Python, Rust, C++, C.
https://www.nayuki.io/page/qr-code-generator-library
5.2k stars 1.11k forks source link

Allow the assert to be redefined #165

Closed gaspardpetit closed 1 year ago

gaspardpetit commented 1 year ago

The C code is very clean but it depends on the system assert which can print and abort the process. This is not always acceptable, so this change defines a ASSERT macro that can be redefined to something else to avoid the dependency on the system assert, even while debugging.

nayuki commented 1 year ago

I am using standard library functions for a reason. I feel that defining a custom local ASSERT macro violates the spirit of using a common vocabulary with well-known behaviors.

Yes, assert() can abort the process, and that's the whole point of an assertion! All the asserts in my C port refer to internal conditions where if violated, it means either my algorithm is wrong (very unlikely at this point after 6 language ports and millions of tests), or an explicitly documented precondition is violated (e.g. overlapping input arrays), or there is some memory corruption or undefined behavior caused by the user's mistake (e.g. inappropriate multithreading, smashing buffers).

For debugging, I think the better solution is for a user of this library to custom-patch the assert in their copy, or to define NDEBUG only when compiling this library but undefining NDEBUG for the rest of the application. Either way, I do not see a good justification for promoting these use-cases upstream to the library itself; these should be private patches for unique needs.

For reference: https://en.cppreference.com/w/c/error/assert

gaspardpetit commented 1 year ago

Thank you for taking the time to respond and explain your view. I see the merit and agree with you. I could argue both ways, but I think in this case simplicity wins the argument.