<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Code Analysis</title>
        <link>http://geekswithblogs.net/terje/category/9068.aspx</link>
        <description>Code Analysis</description>
        <language>en-US</language>
        <copyright>terje</copyright>
        <managingEditor>terje@osiris.no</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>Team System 2010:  Static Code Analysis, easier to set rules</title>
            <link>http://geekswithblogs.net/terje/archive/2009/07/12/team-system-2010--static-code-analysis-easier-to-set.aspx</link>
            <description>&lt;p&gt;In Visual Studio the settings for static analysis is done on the project property page, a tab called Code Analysis. You can set which code analysis rules you want to be active.  The default in Visual Studio 2008 is to use all.   If you run with this default setting you will generate a lot of "noise", since there are a large set of rules.  You need to create a set containing the rules you and your team find are suitable for your organization and project.  This set you have to apply to every C# project in your solution.  In Visual Studio 2008 there is no easy way to do this.  At Osiris we made an Addin to Visual Studio to ease this.  We defined the set in a separate file, and used the Addin to apply that to all projects in a solution. &lt;/p&gt;
&lt;p&gt;With Visual Studio 2010 this will be more easy, because in 2010 you can define sets of rules.  It comes with a default list of sets.   You can also define your own sets, based on any of the others. &lt;/p&gt;
&lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/terje/WindowsLiveWriter/TeamSystem2010StaticCodeAnalysis_5770/image_2.png"&gt;&lt;img style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" border="0" alt="Code Analysis page" width="559" height="351" src="http://geekswithblogs.net/images/geekswithblogs_net/terje/WindowsLiveWriter/TeamSystem2010StaticCodeAnalysis_5770/image_thumb.png" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;To make your own rule set, just open any of the default ones, make any changes you like to it, and save it under a suitable name.  A rule set file is created, which can be checked in together with the other files of the project.  This file [rule set] can now be used by any other project you have.  So you only need to define it once.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/terje/WindowsLiveWriter/TeamSystem2010StaticCodeAnalysis_5770/image_4.png"&gt;&lt;img style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" border="0" alt="image" width="560" height="408" src="http://geekswithblogs.net/images/geekswithblogs_net/terje/WindowsLiveWriter/TeamSystem2010StaticCodeAnalysis_5770/image_thumb_1.png" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;You still have to select it for all the projects in the solution, but this has been made easier by having solution properties, where Code Analysis is one of these setting pages:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/terje/WindowsLiveWriter/TeamSystem2010StaticCodeAnalysis_5770/image_6.png"&gt;&lt;img style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" border="0" alt="image" width="565" height="397" src="http://geekswithblogs.net/images/geekswithblogs_net/terje/WindowsLiveWriter/TeamSystem2010StaticCodeAnalysis_5770/image_thumb_2.png" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;It's then easy to select the rule set you want for all projects within your solution.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=133435"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=133435" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/terje/aggbug/133435.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>terje</dc:creator>
            <guid>http://geekswithblogs.net/terje/archive/2009/07/12/team-system-2010--static-code-analysis-easier-to-set.aspx</guid>
            <pubDate>Sun, 12 Jul 2009 16:41:01 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/terje/comments/133435.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/terje/archive/2009/07/12/team-system-2010--static-code-analysis-easier-to-set.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/terje/comments/commentRss/133435.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/terje/services/trackbacks/133435.aspx</trackback:ping>
        </item>
        <item>
            <title>Hiding generated code from Code Analysis, Metrics and Test Coverage</title>
            <link>http://geekswithblogs.net/terje/archive/2008/11/10/hiding-generated-code-from-code-analysis-metrics-and-test-coverage.aspx</link>
            <description>&lt;p&gt;When I do either Code Analysis, Code Metrics or looking at Code Coverage results, I don't want to have any generated code affecting the results.  It just confuses the numbers, and I do not really care how generated code looks - it should just be invisible.  &lt;/p&gt;
&lt;p&gt;Generated code appears several places, code is generated by any of the multitude of wizards and designers in Visual Studio, or it may be generated by a 3rd part tool or generated by a self-written tool.&lt;/p&gt;
&lt;p&gt;There exist an attribute which, if attached to the class or method, is intended to hide the code from these analyses. However, it fails to do so for the Code Coverage.  The attribute is the GeneratedCodeAttribute, and it is intended to be used for any tools generated code.  See &lt;a href="http://blogs.msdn.com/fxcop/archive/2007/04/27/correct-usage-of-the-compilergeneratedattribute-and-the-generatedcodeattribute.aspx"&gt;http://blogs.msdn.com/fxcop/archive/2007/04/27/correct-usage-of-the-compilergeneratedattribute-and-the-generatedcodeattribute.aspx&lt;/a&gt; for more information on the correct usage.  It can be used both at class level and method level.&lt;/p&gt;
&lt;p&gt;However, the Code Coverage doesn't abide by these rules, as also reported in a two bug reports &lt;a title="http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=338895" href="http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=338895"&gt;http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=338895&lt;/a&gt; and &lt;a href="http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=349243"&gt;http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=349243&lt;/a&gt;.  The latter indicate they will not fix this bug,  so please go in a vote for a fix!&lt;/p&gt;
&lt;p&gt;As a workaround there are two more attributes which can be used, the DebuggerNonUserCode and DebuggerHidden attributes. The former is a combination of the latter and the DebuggerStepThrough attribute.  The DebuggerNonUserCode attribute can be used on both class and method level.&lt;/p&gt;
&lt;p&gt;By using this attribute in addition on generated code, it will not show up in the Code Coverage either.&lt;/p&gt;
&lt;p&gt;The table below summarizes this:&lt;/p&gt;
&lt;table cellspacing="2" cellpadding="2" width="400" border="1"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td valign="top" width="72"&gt; &lt;/td&gt;
            &lt;td valign="top" width="166"&gt;GeneratedCodeAttribute&lt;/td&gt;
            &lt;td valign="top" width="152"&gt;DebuggerNonUserCode&lt;br /&gt;
            &lt;p&gt;DebuggerHidden&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td valign="top" width="72"&gt;Code Metrics&lt;/td&gt;
            &lt;td valign="top" width="166"&gt;Excludes code&lt;/td&gt;
            &lt;td valign="top" width="152"&gt;No Effect&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td valign="top" width="72"&gt;Code Analysis&lt;/td&gt;
            &lt;td valign="top" width="166"&gt;Excludes code&lt;/td&gt;
            &lt;td valign="top" width="152"&gt;No Effect&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td valign="top" width="72"&gt;Code Coverage&lt;/td&gt;
            &lt;td valign="top" width="168"&gt;No Effect&lt;/td&gt;
            &lt;td valign="top" width="154"&gt;Excludes code&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt; &lt;/p&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
]]&gt;&lt;/style&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Further, Microsoft seems to have "forgotten" to add these attributes to some of their own generated code, among them is the Winform generated code. The MSDataSetGenerator adds the GeneratedCode attrbute, but not the DebuggerNonUserCode attribute. (If the Code Coverage had abided by the rules, this would not have been a problem) &lt;/p&gt;
&lt;p&gt;For Winform generated code, it is safe to add these attributes afterwards. The generated file will not be rewritten, just changed, when you add more or edit the controls. Just add them as shown below:&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;pre class="csharpcode"&gt;        [DebuggerNonUserCode]
        [GeneratedCode(&lt;span class="str"&gt;"Winform Designer"&lt;/span&gt;,&lt;span class="str"&gt;"VS2008 SP1"&lt;/span&gt;)]
        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; InitializeComponent()&lt;/pre&gt;
&lt;pre class="csharpcode"&gt; &lt;/pre&gt;
&lt;p&gt;The MSDataSetGenerator is not so forgiving, since the file easily can be rewritten. However, all the classes are partial, so by adding another manual file, and using the attributes there, most of the DataSet can be removed from the Code Coverage.  Adding one dataset with one table generates a lot of classes, the code below shows what is needed to eliminate a dataset and its tableadapter for a table named ErrorLog.&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; WindowsFormsApplication10&lt;/pre&gt;
&lt;pre&gt;{&lt;/pre&gt;
&lt;pre class="alt"&gt;    [DebuggerNonUserCode]&lt;/pre&gt;
&lt;pre&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; DataSet1&lt;/pre&gt;
&lt;pre class="alt"&gt;    {&lt;/pre&gt;
&lt;pre&gt;        [DebuggerNonUserCode]&lt;/pre&gt;
&lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ErrorLogDataTable&lt;/pre&gt;
&lt;pre&gt;        {&lt;/pre&gt;
&lt;pre class="alt"&gt;        }&lt;/pre&gt;
&lt;pre&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;        [DebuggerNonUserCode]&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ErrorLogRow&lt;/pre&gt;
&lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;pre&gt;        }&lt;/pre&gt;
&lt;pre class="alt"&gt; &lt;/pre&gt;
&lt;pre&gt;    }&lt;/pre&gt;
&lt;pre class="alt"&gt; &lt;/pre&gt;
&lt;pre&gt;    &lt;span class="kwrd"&gt;namespace&lt;/span&gt; DataSet1TableAdapters&lt;/pre&gt;
&lt;pre class="alt"&gt;    {&lt;/pre&gt;
&lt;pre&gt;        [DebuggerNonUserCode]&lt;/pre&gt;
&lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ErrorLogTableAdapter&lt;/pre&gt;
&lt;pre&gt;        {&lt;/pre&gt;
&lt;pre class="alt"&gt;        }&lt;/pre&gt;
&lt;pre&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;        [DebuggerNonUserCode]&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; TableAdapterManager&lt;/pre&gt;
&lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;pre&gt;        }&lt;/pre&gt;
&lt;pre class="alt"&gt;    }&lt;/pre&gt;
&lt;pre&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt; &lt;/p&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
]]&gt;&lt;/style&gt;
&lt;p&gt;In my little test case, code coverage changed from 2,54% to 10% just by removing the DataSet1 alone. &lt;/p&gt;
&lt;p&gt;Under the Properties folder (for a WinForm app), the Resources class is decorated with both attributes (Hurrah!) (luckily since it's not partial....) whereas the Settings class only have the GeneratedCode attribute. (Why are these two different ?).  The Settings.Designer.cs file is regenerated each time you change anything in the settings designer, but it is partial, so the same trick as above with the DataSet can be used - create a new file, with the same partial class declaration, and add the DebuggerNonUserCode to it.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;In a later post I'll try to make an even more comprehensive list of which generators work and which doesn't, in this respect, and outline the workarounds for these. &lt;/p&gt;
&lt;p&gt;Hopefully - all of these will be fixed in Visual Studio 2010.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=126911"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=126911" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/terje/aggbug/126911.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>terje</dc:creator>
            <guid>http://geekswithblogs.net/terje/archive/2008/11/10/hiding-generated-code-from-code-analysis-metrics-and-test-coverage.aspx</guid>
            <pubDate>Mon, 10 Nov 2008 09:09:41 GMT</pubDate>
            <comments>http://geekswithblogs.net/terje/archive/2008/11/10/hiding-generated-code-from-code-analysis-metrics-and-test-coverage.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/terje/comments/commentRss/126911.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/terje/services/trackbacks/126911.aspx</trackback:ping>
        </item>
    </channel>
</rss>