Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is one situation I find it useful: TDD.</p> <p>I write my tests, then I create stubs so the tests compile. Those stubs do nothing but <code>throw new NotImplementedException();</code>. This way the tests will fail by default, no matter what. If I used some dummy return value, it might generate false positives. Now that all tests compile and fail because there is no implementation, I tackle those stubs.</p> <p>Since I never use a <code>NotImplementedException</code> in any other situation, no <code>NotImplementedException</code> will ever pass onto release code, since it will always make some test fail.</p> <p>You don't need to catch it all over the place. Good APIs document the exceptions thrown. Those are the ones you should look for.</p> <p>EDIT: I wrote an FxCop rule to find them.</p> <p>This is the code:</p> <pre><code>using System; using Microsoft.FxCop.Sdk; /// &lt;summary&gt; /// An FxCop rule to ensure no &lt;see cref="NotImplementedException"/&gt; is /// left behind on production code. /// &lt;/summary&gt; internal class DoNotRaiseNotImplementedException : BaseIntrospectionRule { private TypeNode _notImplementedException; private Member _currentMember; public DoNotRaiseNotImplementedException() : base("DoNotRaiseNotImplementedException", // The following string must be the assembly name (here // Bevonn.CodeAnalysis) followed by a dot and then the // metadata file name without the xml extension (here // DesignRules). See the note at the end for more details. "Bevonn.CodeAnalysis.DesignRules", typeof (DoNotRaiseNotImplementedException).Assembly) { } public override void BeforeAnalysis() { base.BeforeAnalysis(); _notImplementedException = FrameworkAssemblies.Mscorlib.GetType( Identifier.For("System"), Identifier.For("NotImplementedException")); } public override ProblemCollection Check(Member member) { var method = member as Method; if (method != null) { _currentMember = member; VisitStatements(method.Body.Statements); } return Problems; } public override void VisitThrow(ThrowNode throwInstruction) { if (throwInstruction.Expression != null &amp;&amp; throwInstruction.Expression.Type.IsAssignableTo(_notImplementedException)) { var problem = new Problem( GetResolution(), throwInstruction.SourceContext, _currentMember.Name.Name); Problems.Add(problem); } } } </code></pre> <p>And this is the rule metadata:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;Rules FriendlyName="Bevonn Design Rules"&gt; &lt;Rule TypeName="DoNotRaiseNotImplementedException" Category="Bevonn.Design" CheckId="BCA0001"&gt; &lt;Name&gt;Do not raise NotImplementedException&lt;/Name&gt; &lt;Description&gt;NotImplementedException should not be used in production code.&lt;/Description&gt; &lt;Url&gt;http://stackoverflow.com/questions/410719/notimplementedexception-are-they-kidding-me&lt;/Url&gt; &lt;Resolution&gt;Implement the method or property accessor.&lt;/Resolution&gt; &lt;MessageLevel Certainty="100"&gt;CriticalError&lt;/MessageLevel&gt; &lt;Email&gt;&lt;/Email&gt; &lt;FixCategories&gt;NonBreaking&lt;/FixCategories&gt; &lt;Owner&gt;&lt;/Owner&gt; &lt;/Rule&gt; &lt;/Rules&gt; </code></pre> <p>To build this you need to:</p> <ul> <li><p>reference <code>Microsoft.FxCop.Sdk.dll</code> and <code>Microsoft.Cci.dll</code></p></li> <li><p>Put the metadata in a file called <code>DesignRules.xml</code> and add it as an embedded resource to your assembly</p></li> <li><p>Name your assembly <code>Bevonn.CodeAnalysis</code>. If you want to use different names for either the metadata or the assembly files, make sure you change the second parameter to the base constructor accordingly.</p></li> </ul> <p>Then simply add the resulting assembly to your FxCop rules and take those damned exceptions out of your precious code. There are some corner cases where it won't report a NotImplementedException when one is thrown but I really think you are hopeless if you're actually writing such cthulhian code. For normal uses, i.e. <code>throw new NotImplementedException();</code>, it works, and that is all that matters.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload