I'm trying to write something similar to the existing Debug class that allows clients to explicitly verify pre- and post-conditions of their methods. For example, to check a precondition that the input variable to a method is not equal to 0, one would use Expects.IsGreaterThan(input, 0), and that code would evaluate in both debug and release builds to throw an exception if input <= 0. If, however, the client only wanted to have the check in debug builds as a defensive programming tactic but didn't want the overhead of evaluating the condition in release builds (e.g. the input is generated internally and not from user data, so passing a bad value is a bug rather than an exceptional circumstance), then the client would use Expects.Debug.IsGreaterThan(input, 0). Note that the exact syntax here is not set in stone, though I would like to be able to have both `Expects` and `Ensures` (for pre- and post-conditions, respectively) plus debug versions of both.
My issue is figuring out how to do this without having massive code duplication. Every approach that I can think of either requires two separate classes with the exact same code (e.g. using the Conditional("Debug") attribute on the Debug version) or doesn't actually elide the invocation (e.g. using some kind of Boolean policy class and checking that first in the evaluation function). I've considered using a code generator of some sort so that the code is duplicated only as a result but not at the point of authorship, but I'd rather not have to write an entire tool/template for this; there are potentially dozens of functions that I want to include in these utility classes, and describing each in a higher-level language for some generator to ingress would be fairly tedious.
So I'm wondering if anyone has any ideas on how I can do this. Hopefully this problem statement made sense; I can follow up with additional details if need be. Thanks!
My issue is figuring out how to do this without having massive code duplication. Every approach that I can think of either requires two separate classes with the exact same code (e.g. using the Conditional("Debug") attribute on the Debug version) or doesn't actually elide the invocation (e.g. using some kind of Boolean policy class and checking that first in the evaluation function). I've considered using a code generator of some sort so that the code is duplicated only as a result but not at the point of authorship, but I'd rather not have to write an entire tool/template for this; there are potentially dozens of functions that I want to include in these utility classes, and describing each in a higher-level language for some generator to ingress would be fairly tedious.
So I'm wondering if anyone has any ideas on how I can do this. Hopefully this problem statement made sense; I can follow up with additional details if need be. Thanks!