[Mono-dev] Faster

Jonathan Pryor jonpryor at vt.edu
Thu Mar 24 14:46:19 EDT 2011

On Mar 24, 2011, at 12:00 PM, Miguel de Icaza wrote:
> Plenty of our class library code has code like this:
> void Foo (Something x)
> {
>    if (x == null)
>        throw new ArgumentNullException ("x");
>    x.DoSomething ();
>    x.AndThenMore ();
> }
> But what if we changed our code in Foo across our class libraries to
> do this instead:
> void Foo (Something x)
> {
>    try {
>        x.DoSomething ();
>    } catch (NullReferenceException e){
>        if (x == null)
>             throw new ArgumentNullException ("x");
>        else
>              throw;
>    }
>    x.AndThenMore ();
> }

I don't think this would be a good idea, for two reasons:

 1. This is counter to years worth of code guidelines. Consequently, it will cause anybody new coming to the code to scratch their head. Clear and understandable code can't be overstated.

 2. This won't mesh at all with Code Contracts. Yes, Code Contracts suck (as currently envisioned), but the idea behind them is reasonable, and their IL rewriter currently supports the current "legacy" null checks for contract definitions, ending contracts at a Contract.EndContractBlock() call [0]:

	void SoCalledLegacyContractMethod (string s)
		if (s == null)
			throw new ArgumentNullException ("s");
		// ...

I don't think it's that unlikely that more of mscorlib.dll will gain contracts support in the future for improved static analysis of assemblies, which will require that argument checking be "up front" as is currently done anyway.

 - Jon

[0] http://msdn.microsoft.com/en-us/library/system.diagnostics.contracts.contract.endcontractblock.aspx

More information about the Mono-devel-list mailing list