ifs-at-the-start-of-a-method
Even for a simple if: DRY! You should have put those in reusable functions with nice understandable names ages ago :]
Even for a simple if: DRY! You should have put those in reusable functions with nice understandable names ages ago :]
Typical contracts are 'assert (arg != null)' or 'assert(arg1.length == arg2.length)' or 'assert (hour >= 0 and hour <= 23)'
if( x < 0 || x > 5 )
throw new ArgumentOutOfRange();
if( y == nullptr )
thwrow new NullRefException();
which should imo be something like Contract.AssertInRange( x, 0, 5 );
Contract.AssertNotNull( y ); public void MyFunction([InRange(0, 5)] int x, [NotNull] object y)
{
// ...
}
I just checked, and in fact Fody can do something just like this [1].The only downside I see is contracts like this would be hard to write concisely:
if (useFirstObject == true)
Contract.AssertNotNull(firstObject);
else
Contract.AssertNotNull(secondObject);
[1] https://github.com/Fody/NullGuard