Re:The error model of Microsoft's new language
by octo_t on Monday December 30, 2013 @08:41AM
(#6984539)
do you know what contracts are?
Typical contracts are 'assert (arg != null)' or 'assert(arg1.length == arg2.length)' or 'assert (hour >= 0 and hour <= 23)'
Re:The error model of Microsoft's new language
by stinos on Monday December 30, 2013 @09:40AM
(#6984759)
yes I know what they are. Use them all the time. But since the post is about if I imagine it's about things like
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 );
Re:The error model of Microsoft's new language
by taspeotis on Monday December 30, 2013 @09:44PM
(#6988634)
I imagine you could do even better with something like PostSharp or Fody.
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
Re:The error model of Microsoft's new language
by adandy on Monday December 30, 2013 @12:03PM
(#6985442)
It is possible to write your own contract abbreviators with Code Contracts so I would hope such functionality gets added to the new language. The post does mention however that object invariants are not supported so I do wonder how much Code Contracts are actually influencing it.
Even for a simple if: DRY! You should have put those in reusable functions with nice understandable names ages ago :]