lecaillon / Preconditions

Preconditions.NET provides convenience static methods to help check that a method or a constructor is invoked with proper parameter or not.
MIT License
25 stars 6 forks source link

Enable conditional compilation #5

Open starkos opened 6 years ago

starkos commented 6 years ago

I would like to have the ability to turn off the checks in certain conditions or areas of the code. Would you be open to a pull request that adds [Conditional("PRECONDITIONS")] to the methods, so they could be turned on and off by a define?

It would require all users to define the symbols of course, which isn't ideal.

lecaillon commented 6 years ago

Can you be more specfic please ?

What is your purpose for doing it ? How a ConditionnalAttribute would help ?

starkos commented 6 years ago

I have some inner loop code, running on mobile devices, that needs to execute as quickly and efficiently as possible. During development, I would like the checks in place to catch logic errors. At runtime, I want to disable them, but only for these inner loop sections of code.

(And yes, I measured, and removing the checks makes a difference in performance and energy usage.)

If you add the ConditionalAttribute, it would then be possible to disable the checks for a specific code file with #undef PRECONDITIONS at the top of the file. Or actually, more like:

#if RELEASE
#undef PRECONDITIONS
#endif

Right now I have to put that ugly #if … #endif around all of the checks in those files, which is not ideal.

The downside to adding the attribute, as I mentioned, is that the checks will only be evaluated if you define PRECONDITIONS somewhere, i.e. in your project file.

Figured it didn't hurt to ask…

ryancerium commented 5 years ago

I think that's a great feature @starkos