feO2x / Light.GuardClauses

A lightweight .NET library for expressive Guard Clauses.
MIT License
85 stars 8 forks source link

added Check.InvalidArgument #65

Closed ironcev closed 5 years ago

ironcev commented 5 years ago

Description

Implements #64.

Remarks

Documentation

I had a bit of difficulty to meaningfully describe the parameter parameter in this overload:

InvalidArgument<T>(bool condition, T parameter, Func<T, Exception> exceptionFactory)

In all other cases where we have a parameter value passed to the exceptionFactory this parameter value is as well checked by the guard method. In this case, the parameter value is not checked within the guard, but just the condition. Of course, we assume that the condition checks the parameter. Therefore I went for the following formulation:

<param name="parameter">The value that is checked in the <paramref name="condition"/>. This value is passed to the <paramref name="exceptionFactory"/>.</param>

Performance

On my machine, the benchmarks are showing that the overloads with exceptionFactory are not inlined. In general, I had difficulties getting stable execution times in tests. But the ASM shows clear inlining in the first overload (without exceptionFactory). On the other hand, the Inlining Analyzer shows that all the overloads should be inlined in benchmarks. Attached are results of two runs done on my laptop, in case you want to take a look. Benchmarks.zip

feO2x commented 5 years ago

Thanks for your pull request. Unfortunately, I'm currently pretty busy, thus it might be several more days until I'm able to have a look at your PR.

ironcev commented 5 years ago

No problem, please take your time. If anyone fully understands the meaning of "being pretty busy", that's me :-)

feO2x commented 5 years ago

Your code is flawless, thanks for the PR.

BTW: I've never seen assertions with exception factories being inlined. That's one of the reasons why I split assertions into two overloads, one that throws the default exception, and one that uses an exception factory. Before Light.GuardClauses 4.0, everything was done in one static method.

ironcev commented 5 years ago

Thanks for the merge and for the explanation!