I'm tying to understand Code Contracts advantages. I've wrote following code (from PEX + Code Contract introduction) to dig it.
public static string TrimAfter(string value, string suffix)
{
// <pex>
Contract.Requires(suffix != (string)null);
Contract.Requires
(value.IndexOf(suffix) >= 0 && value.Length >= value.IndexOf(suffix));
Contract.Requires(value != (string)null);
// </pex>
int index = value.IndexOf(suffix);
if (index < 0)
return value;
return value.Substring(0, index);
}
I called this method with nulls arguments and it was compiled. So it is not clear for me why it is better then throwing Exceptions. Could you guys explain me if Code Contracts really has any additional features? :) Thanks in advance.
On the other hand it shouldn't be hard to parse the IL recognizing the old standard pattern for pre-condition checks and use that generate documentation.