<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PeterKellner.net &#187; Refactor</title>
	<atom:link href="http://peterkellner.net/category/refactor/feed/" rel="self" type="application/rss+xml" />
	<link>http://peterkellner.net</link>
	<description>Microsoft Focused, JavaScript,HTML5 (ExtJS, SenchaTouch &#38; Windows 8 Metro)</description>
	<lastBuildDate>Fri, 11 May 2012 16:43:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>A Cool / Unexpected Refactoring around .Net C# Locking Issue</title>
		<link>http://peterkellner.net/2010/07/29/resharper-refactor-locking-linq/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=resharper-refactor-locking-linq</link>
		<comments>http://peterkellner.net/2010/07/29/resharper-refactor-locking-linq/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 14:32:12 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Lock]]></category>
		<category><![CDATA[Refactor]]></category>
		<category><![CDATA[ReSharper]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2010/07/29/resharper-refactor-locking-linq/</guid>
		<description><![CDATA[I’m constantly amazed by the insightfulness of ReSharper’s suggested refactorings (ReSharper is a Visual Studio Addin from JetBrains I use with C#). Today, I’ve been working on a threading problem where I’m getting crashes based on what seems like not proper locking across threads (they usually show up as some type of ugly update object [...]]]></description>
			<content:encoded><![CDATA[<p>I’m constantly amazed by the insightfulness of <a href="http://www.jetbrains.com/resharper/index.html">ReSharper’s</a> suggested refactorings (ReSharper is a <a href="http://www.microsoft.com/visualstudio/en-us/visual-studio-2010-launch?WT.mc_id=SEARCH&amp;WT.srch=1">Visual Studio</a> Addin from <a href="http://www.jetbrains.com/index.html">JetBrains</a> I use with C#). Today, I’ve been working on a threading problem where I’m getting crashes based on what seems like not proper locking across threads (they usually show up as some type of ugly update object or enum error).</p>
<p>My code starts like this:</p>
<p>&#160;</p>
<div>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> List&lt;DbProgressReport&gt; DbProgressReportProperty { get; set; }</pre>
</div>
<div>&#160;</div>
<p><span id="more-1347"></span></p>
<div>
  </div>
<p>Then, I try wrapping the updates with a lock as follows (removing the set because I do it someplace else now)</p>
<p>&#160;</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> List&lt;DbProgressReport&gt; DbProgressReportProperty{    get    {        <span style="color: #0000ff">lock</span> (lockDbProgressReportProperty)        {            <span style="color: #0000ff">return</span> _dbProgressReportList;        }    }}</pre>
<p></div>
<p>&#160;</p>
<p>I then realize that I need to copy the data because it may change while the caller prints it so I decide to return a temporary copy of the data as follows:</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> List&lt;DbProgressReport&gt; DbProgressReportProperty{    get    {        <span style="color: #0000ff">lock</span> (lockDbProgressReportProperty)        {            var dbProgressReportsNew = <span style="color: #0000ff">new</span> List&lt;DbProgressReport&gt;();            <span style="color: #0000ff">if</span> (_dbProgressReportList != <span style="color: #0000ff">null</span>)            {                <span style="color: #008000">// make temp copy to avoid locking issues on read</span>                <span style="color: #0000ff">foreach</span> (var rec <span style="color: #0000ff">in</span> _dbProgressReportList)                {                    dbProgressReportsNew.Add(rec);                }            }            <span style="color: #0000ff">return</span> dbProgressReportsNew;        }    }}</pre>
<p></div>
<p>I then notice that ReSharper has a suggestion.</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/ACoolUnexpectedRefactor.NetCLockingIssue_69E6/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/ACoolUnexpectedRefactor.NetCLockingIssue_69E6/image_thumb.png" width="512" height="268" /></a> </p>
</p>
<p>Taking the suggestion, Resharper changes the code to:</p>
<div>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">lock</span> (lockDbProgressReportProperty){    <span style="color: #008000">// make temp copy to avoid locking issues on </span>    <span style="color: #0000ff">return</span> _dbProgressReportList.ToList();}</pre>
</div>
<div>&#160;</div>
<div>I expected a bunch of AddRange type stuff, but ReSharper figured out that ToList() would do everything I needed!</div>
<div>&#160;</div>
<div>Very cool.</div>
<div>&#160;</div>
<div>Hope this helps.</div>
<div>
  </div>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/07/29/resharper-refactor-locking-linq/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Another Nice DevExpress CodeRush Refactoring</title>
		<link>http://peterkellner.net/2010/07/04/coderush-factorings-introduce-lambda-using-statements/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=coderush-factorings-introduce-lambda-using-statements</link>
		<comments>http://peterkellner.net/2010/07/04/coderush-factorings-introduce-lambda-using-statements/#comments</comments>
		<pubDate>Sun, 04 Jul 2010 16:42:26 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[CodeRush]]></category>
		<category><![CDATA[Refactor]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2010/07/04/coderush-factorings-introduce-lambda-using-statements/</guid>
		<description><![CDATA[For the last few days, I’ve been using DevExpress CodeRush and am finding some very useful refactorings.&#160; Many I’m not blogging about, but there are a few that I really like.&#160; In this post, I’m going to show just two of those refactorings that have been making my code much nicer and easier to write.&#160; [...]]]></description>
			<content:encoded><![CDATA[<p>For the last few days, I’ve been using <a href="http://devexpress.com/">DevExpress</a> <a href="http://devexpress.com/Products/Visual_Studio_Add-in/Coding_Assistance/">CodeRush</a> and am finding some very useful refactorings.&#160; Many I’m not blogging about, but there are a few that I really like.&#160; In this post, I’m going to show just two of those refactorings that have been making my code much nicer and easier to write.&#160; One is the “Introduce Using” refactoring, and the other is “Convert to Lambda Expression”.</p>
<p>Before I go into the details, I’d just like to disclose that when I was first writing the <a href="http://www.siliconvalley-codecamp.com/">Silicon Valley Code Camp</a> web site, I was an asp.net and c# newby.&#160; I’m not claiming wizard status now, but I have to admit that when I go back and look at some of the code I wrote back then (including what I’m showing below before the refactoring), it’s a little embarrassing.&#160; Silicon Valley Code Camp for me as “when I’m not doing real work” web site so I don’t really have the time to go back and clean things up.&#160; Now, with CodeRush, it’s easy to clean things up when I see them with very little effort.</p>
<p> <span id="more-1337"></span><br />
<h2>Introduce Using Statement Refactoring</h2>
<p>Here is the code I wrote 5+ years ago:</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">SqlConnection conn = <span style="color: #0000ff">new</span> SqlConnection(connectionString);conn.Open();

<span style="color: #008000">// First, make a quick list of counts for each id that is unavailable and show</span><span style="color: #008000">// not be shown</span><span style="color: #0000ff">string</span> selectCount = <span style="color: #006080">@&quot;select VistaSlotsId FROM attendees                      WHERE VistaSlotsId &gt;= 2 AND VistaSlotsId &lt;= 5 Group By VistaSlotsId                      HAVING COUNT(*) &gt;= @MaxPerSlot  &quot;</span>;List&lt;<span style="color: #0000ff">int</span>&gt; unavailableSlotsList = <span style="color: #0000ff">new</span> List&lt;<span style="color: #0000ff">int</span>&gt;();SqlDataReader readerUnavailable = <span style="color: #0000ff">null</span>;SqlCommand cmdUnAvailable = <span style="color: #0000ff">new</span> SqlCommand(selectCount, conn);cmdUnAvailable.Parameters.Add(<span style="color: #006080">&quot;@MaxPerSlot&quot;</span>, SqlDbType.Int).Value = maxPerSlot;readerUnavailable = cmdUnAvailable.ExecuteReader();<span style="color: #0000ff">try</span>{    <span style="color: #0000ff">while</span> (readerUnavailable.Read())    {        <span style="color: #0000ff">int</span> id = readerUnavailable.GetInt32(0);        unavailableSlotsList.Add(id);    }}<span style="color: #0000ff">finally</span>{    <span style="color: #0000ff">if</span> (readerUnavailable != <span style="color: #0000ff">null</span>) readerUnavailable.Close();}</pre>
<p></div>
<p>By placing the cursor over the “conn” on the top line of the code above, CodeRush gives us the following result:</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/AnotherNiceDevExpressCodeRushRefactoring_8873/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/AnotherNiceDevExpressCodeRushRefactoring_8873/image_thumb.png" width="507" height="233" /></a> </p>
<p>There are actually quite a few things happening here.</p>
<ol>
<li>It is showing us in the top textbox what the using syntax will be </li>
<li>It is crossing out the lines of code it will change </li>
<li>It is teaching us a little about the using statement </li>
</ol>
<p>If I accept the changes, I get a nice refactoring.&#160; I can continue doing this with “using” for SqlCommand and SqlReader.&#160; I did need to move the SqlReader declaration inside the SqlCommand codeblock for this to work.&#160; Here is the final refactored code that took about 10 seconds.&#160; By hand, I’d say it would have taken me 3 minutes and I may have gotten it wrong which is why I would never do it before.&#160; Now, I’m confident I did not break my code.</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">using</span> (SqlConnection conn = <span style="color: #0000ff">new</span> SqlConnection(connectionString)){    conn.Open();    <span style="color: #008000">// First, make a quick list of counts for each id that is unavailable and show</span>    <span style="color: #008000">// not be shown</span>    <span style="color: #0000ff">string</span> selectCount = <span style="color: #006080">@&quot;select VistaSlotsId FROM attendees                                  WHERE VistaSlotsId &gt;= 2 AND VistaSlotsId &lt;= 5 Group By VistaSlotsId                                  HAVING COUNT(*) &gt;= @MaxPerSlot  &quot;</span>;    List&lt;<span style="color: #0000ff">int</span>&gt; unavailableSlotsList = <span style="color: #0000ff">new</span> List&lt;<span style="color: #0000ff">int</span>&gt;();    <span style="color: #0000ff">using</span> (SqlCommand cmdUnAvailable = <span style="color: #0000ff">new</span> SqlCommand(selectCount, conn))    {        cmdUnAvailable.Parameters.Add(<span style="color: #006080">&quot;@MaxPerSlot&quot;</span>, SqlDbType.Int).Value = maxPerSlot;        <span style="color: #0000ff">using</span> (SqlDataReader readerUnavailable = cmdUnAvailable.ExecuteReader())        {            <span style="color: #0000ff">try</span>            {                <span style="color: #0000ff">while</span> (readerUnavailable.Read())                {                    <span style="color: #0000ff">int</span> id = readerUnavailable.GetInt32(0);                    unavailableSlotsList.Add(id);                }            }            <span style="color: #0000ff">finally</span>            {                <span style="color: #0000ff">if</span> (readerUnavailable != <span style="color: #0000ff">null</span>)                    readerUnavailable.Close();            }        }    }</pre>
<p>&#160; </p></div>
<div>
  </div>
<h2>Compress To Lambda Expression Refactoring</h2>
<p>A lot of the Silicon Valley Code Camp web site was written prior to the introduction of Lamba Expressions.&#160; If you recall, I wrote an MSDN article back in 2006 entitled <a href="http://peterkellner.net/2006/03/13/adding-personalization-via-profiles-to-the-objectdatasource-in-aspnet-20/">Adding Personalization via Profiles to the ObjectDataSource in ASP.NET 2.0.</a>&#160; <a href="http://blogs.tedneward.com/">Ted Neward</a> inspired a construct to cleverly sort the result list using delegates as shown in the screen shot (form that above post).</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/AnotherNiceDevExpressCodeRushRefactoring_8873/image_3.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/AnotherNiceDevExpressCodeRushRefactoring_8873/image_thumb_3.png" width="569" height="353" /></a> </p>
<p>&#160;</p>
<p>The code below is similar to the above but not quite the same.&#160; This is how it looks before the refactoring:</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">Comparison&lt;DataObjectSessionsOverview&gt; comparison = <span style="color: #0000ff">null</span>;    <span style="color: #0000ff">switch</span> (sortDataBase)    {        <span style="color: #0000ff">case</span> <span style="color: #006080">&quot;Userfirstname&quot;</span>:            comparison = <span style="color: #0000ff">new</span> Comparison&lt;DataObjectSessionsOverview&gt;(               <span style="color: #0000ff">delegate</span>(DataObjectSessionsOverview lhs, DataObjectSessionsOverview rhs)               {                   <span style="color: #0000ff">return</span> lhs.Userfirstname.CompareTo(rhs.Userfirstname);               }             );            <span style="color: #0000ff">break</span>;</pre>
<p></div>
<p>CodeRush suggests:</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/AnotherNiceDevExpressCodeRushRefactoring_8873/image_4.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/AnotherNiceDevExpressCodeRushRefactoring_8873/image_thumb_4.png" width="551" height="245" /></a> </p>
<p>Which then give us:</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">switch</span> (sortDataBase){   <span style="color: #0000ff">case</span> <span style="color: #006080">&quot;Userfirstname&quot;</span>:       comparison = (lhs, rhs) =&gt; lhs.Userfirstname.CompareTo(rhs.Userfirstname);       <span style="color: #0000ff">break</span>;</pre>
<p></div>
<p>I like it!</p>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/07/04/coderush-factorings-introduce-lambda-using-statements/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>A Handy Refactoring with CodeRush (InLine Temp)</title>
		<link>http://peterkellner.net/2010/07/02/coderush-refactoring-inline-temp/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=coderush-refactoring-inline-temp</link>
		<comments>http://peterkellner.net/2010/07/02/coderush-refactoring-inline-temp/#comments</comments>
		<pubDate>Fri, 02 Jul 2010 14:34:56 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[CodeRush]]></category>
		<category><![CDATA[Refactor]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2010/07/02/coderush-refactoring-inline-temp/</guid>
		<description><![CDATA[I’ve recently started using CodeRush with Visual Studio 2010 and am so far very impressed with the convenience it adds to coding.&#160; One thing that is very clear is that the creators of CodeRush are real programmers and look very hard for patterns that us developers are constantly doing.&#160; As I run into these things [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve recently started using <a href="http://www.devexpress.com/Products/Visual_Studio_Add-in/Coding_Assistance/">CodeRush</a> with <a href="http://msdn.microsoft.com/en-us/vstudio/default.aspx">Visual Studio 2010</a> and am so far very impressed with the convenience it adds to coding.&#160; One thing that is very clear is that the creators of <a href="http://www.devexpress.com/Products/Visual_Studio_Add-in/Coding_Assistance/">CodeRush</a> are real programmers and look very hard for patterns that us developers are constantly doing.&#160; As I run into these things that get my attention, I plan on blogging them.&#160; Some are just earth shattering, and others, just nice to have.&#160; This particular one is a nice to have.</p>
<p>So, say you have code like this:</p>
<p>&#160;</p>
<div>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">var sessionAttendeeOds =   <span style="color: #0000ff">new</span> SessionAttendeeODS();listSessionAttendees =   sessionAttendeeOds.GetByUsername(Context.User.Identity.Name);</pre>
</div>
<div>&#160;</div>
<p><span id="more-1335"></span></p>
<div>When I first wrote code, I often do it like this thinking that I may have more methods I’m going to call against the instantiated object.&#160; In this case, many years later, I’m looking at the code and want to condense it.&#160; With <a href="http://www.devexpress.com/Products/Visual_Studio_Add-in/Coding_Assistance/">CodeRush</a>, it show me three little dots under the variable sessionAttendeeOds as follows.&#160; When I click on these three dots, I get the following screen.</div>
<p><a href="http://peterkellner.net/FilesForWebDownload/AHandyRefactoringwithCodeRushInLineTemp_6A97/image.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/AHandyRefactoringwithCodeRushInLineTemp_6A97/image_thumb.png" width="405" height="156" /></a> </p>
<p>When I chose “InlineTemp”, the code changes to the following, make it easier to read.</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">listSessionAttendees =                 <span style="color: #0000ff">new</span> SessionAttendeeODS().GetByUsername(Context.User.Identity.Name);</pre>
<p></div>
<p>Again, this is not a huge deal, but small fixes like this will add up over time and make my code cleaner and more readable.&#160; CodeRush makes it so easy that I’m sure I’ll do this kind of thing more and more.</p>
<p>HTH’s.</p>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/07/02/coderush-refactoring-inline-temp/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>ReSharper 5.0 Adds New &#8220;Add Parameter&#8221; Refactoring</title>
		<link>http://peterkellner.net/2010/04/26/resharper-refactor-add-parameter/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=resharper-refactor-add-parameter</link>
		<comments>http://peterkellner.net/2010/04/26/resharper-refactor-add-parameter/#comments</comments>
		<pubDate>Mon, 26 Apr 2010 18:38:40 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[.Net 2.0]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Refactor]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2010/04/26/resharper-refactor-add-parameter/</guid>
		<description><![CDATA[In this post, I’ll show a simple example of how when you add a parameter to C# method, ReSharper gives you a simple prompting to ask if you want to add a parameter to your method, or create an overloaded method that gives you the flexibility to maintain the old method signature and have the [...]]]></description>
			<content:encoded><![CDATA[<p>In this post, I’ll show a simple example of how when you add a parameter to C# method, <a href="http://www.jetbrains.com/resharper/whatsnew/">ReSharper</a> gives you a simple prompting to ask if you want to add a parameter to your method, or create an overloaded method that gives you the flexibility to maintain the old method signature and have the new method.</p>
<p> <span id="more-1281"></span>
<p>In my original code, I had the following code that called the method GetLoadResultsFromInterval.</p>
<p>&#160;</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">List<span style="color: #0000ff">&lt;</span><span style="color: #800000">LoadResult</span><span style="color: #0000ff">&gt;</span> loadResultsForInterval =    GetLoadResultsForInterval(rec.TimePoint,                              rec.IntervalLengthInMs);</pre>
<p></div>
<p>Then, I simply added one more parameter (loadFtpArrivalTimeDictionary) and ReSharper prompted me with the following:</p>
<p>&#160;</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/ReSharpe.0AddsNewAddParameterRefactoring_A3A1/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/ReSharpe.0AddsNewAddParameterRefactoring_A3A1/image_thumb.png" width="627" height="203" /></a> </p>
<p>If I choose the first “Add Parameter, I get what I would expect, which is just the method with an extra parameter.</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">/// <span style="color: #0000ff">&lt;/</span><span style="color: #800000">summary</span><span style="color: #0000ff">&gt;</span>/// <span style="color: #0000ff">&lt;</span><span style="color: #800000">param</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;timePoint&quot;</span><span style="color: #0000ff">&gt;&lt;/</span><span style="color: #800000">param</span><span style="color: #0000ff">&gt;</span>/// <span style="color: #0000ff">&lt;</span><span style="color: #800000">param</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;intervalLengthInMs&quot;</span><span style="color: #0000ff">&gt;&lt;/</span><span style="color: #800000">param</span><span style="color: #0000ff">&gt;</span>/// <span style="color: #0000ff">&lt;</span><span style="color: #800000">param</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;loadFtpArrivalTimeDictionary&quot;</span><span style="color: #0000ff">&gt;&lt;/</span><span style="color: #800000">param</span><span style="color: #0000ff">&gt;</span>/// <span style="color: #0000ff">&lt;</span><span style="color: #800000">returns</span><span style="color: #0000ff">&gt;&lt;/</span><span style="color: #800000">returns</span><span style="color: #0000ff">&gt;</span>private static List<span style="color: #0000ff">&lt;</span><span style="color: #800000">LoadResult</span><span style="color: #0000ff">&gt;</span>    GetLoadResultsForInterval(DateTime timePoint,    int intervalLengthInMs,    Dictionary<span style="color: #0000ff">&lt;</span><span style="color: #800000">string</span>, <span style="color: #ff0000">DateTime</span><span style="color: #0000ff">&gt;</span> loadFtpArrivalTimeDictionary){   throw new NotImplementedException();}</pre>
<p></div>
<p>It even adds the param xml definition automatically.</p>
<p>However, I’m not quite as pleased with the second refactoring, that is, “Create Overload”.&#160; It does almost everything I would expect, but does not implement the method, which I think it could.&#160; Or, at least it could give it a try.&#160; What it does implement is the following:</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/ReSharpe.0AddsNewAddParameterRefactoring_A3A1/image_3.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/ReSharpe.0AddsNewAddParameterRefactoring_A3A1/image_thumb_3.png" width="652" height="141" /></a> </p>
<p>I’m actually including a picture of it rather than the actual code because it created an error.&#160; It should have said DateTime instead of loadFtpArrivalTimeDictionary.&#160; Two other things of interest.</p>
<p>First, it did not add xml comments (which is probably because of the syntax error), and second, I would have expected it to have called the overloaded method as follows instead of what it did.</p>
<p>&#160;</p>
<div>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">private static List<span style="color: #0000ff">&lt;</span><span style="color: #800000">LoadResult</span><span style="color: #0000ff">&gt;</span> GetLoadResultsForInterval(DateTime timePoint,    int intervalLengthInMs,     Dictionary<span style="color: #0000ff">&lt;</span><span style="color: #800000">string</span>, <span style="color: #ff0000">DateTime</span><span style="color: #0000ff">&gt;</span> loadFtpArrivalTimeDictionary){    // - put additional code here    return GetLoadResultsForInterval(timePoint, intervalLengthInMs);}</pre>
</div>
<div>&#160;</div>
<div>(before anyone points out this is the opposite of how you would normally overload a method call, I do know that.&#160; That is, typically, you would have a method with 2 incoming parameters that internally would call a method with 3 internal parameters, and you would simply default the third parameter.&#160; In my case, I’m going the other way, which is unusual, but not unheard of.)</div>
<div>
  </div>
<p>I began this post to brag about yet another awesome ReSharper refactoring because I really just wanted to add a parameter to an existing method call and it worked perfect.&#160; It was not until I decided to show the other refactoring for this blog post did I run into the problem.&#160; </p>
<p>I’m going to post this to the refactor dev’s and see if they can let me know where either I’ve gone astray, or there is a problem in their code.&#160; At any rate, I’m running this on vs2008 and have not tried it on vs2010.</p>
<p>Hope this helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/04/26/resharper-refactor-add-parameter/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using ReSharper, I&#8217;ve Always Wondered Why They Had the &#8220;Invert if&#8221; refactor</title>
		<link>http://peterkellner.net/2009/10/18/refactor-csharp-if-resharper/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=refactor-csharp-if-resharper</link>
		<comments>http://peterkellner.net/2009/10/18/refactor-csharp-if-resharper/#comments</comments>
		<pubDate>Sun, 18 Oct 2009 15:33:06 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Refactor]]></category>
		<category><![CDATA[ReSharper]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2009/10/18/refactor-csharp-if-resharper/</guid>
		<description><![CDATA[ Well, now I know.&#160; Here is an example of some code I just wrote:

if (doDeficitCalc)
{
    throw 
        new ApplicationException(&#34;Need to implement deficit weight rating&#34;);
}
else
{
    // Linear interpolation from top of range to bottom for speed
    double x1 = [...]]]></description>
			<content:encoded><![CDATA[<p> Well, now I know.&#160; Here is an example of some code I just wrote:</p>
<div class="csharpcode">
<pre class="alt"><span class="kwrd">if</span> (doDeficitCalc)</pre>
<pre>{</pre>
<pre class="alt">    <span class="kwrd">throw</span> </pre>
<pre>        <span class="kwrd">new</span> ApplicationException(<span class="str">&quot;Need to implement deficit weight rating&quot;</span>);</pre>
<pre class="alt">}</pre>
<pre><span class="kwrd">else</span></pre>
<pre class="alt">{</pre>
<pre>    <span class="rem">// Linear interpolation from top of range to bottom for speed</span></pre>
<pre class="alt">    <span class="kwrd">double</span> x1 = rateBreakList[0];</pre>
<pre>    <span class="kwrd">double</span> x2 = rateBreakList[rateBreakList.Count - 1];</pre>
<pre class="alt">&#160;</pre>
<pre>    <span class="kwrd">double</span> y1 = rateList[0];</pre>
<pre class="alt">    <span class="kwrd">double</span> y2 = rateList[rateList.Count - 1];</pre>
<pre>&#160;</pre>
<pre class="alt">&#160;</pre>
<pre>    <span class="kwrd">double</span> frac = weight/(x2 - x1);</pre>
<pre class="alt">    <span class="kwrd">double</span> yResult = y1 + (frac*(y2 - y1));</pre>
<pre>    retWeight = yResult;</pre>
<pre class="alt">}</pre>
</div>
<style type="text/css">
<p>.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; }</style>
<p><a href="http://www.jetbrains.com/resharper/">ReSharper</a> correctly warns me that the else statement is redundant as shown below:</p>
<p><span id="more-366"></span></p>
<p><a href="http://peterkellner.net/FilesForWebDownload/UsingResharperIveAlwaysWonderedWhyTheyHa_765C/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/UsingResharperIveAlwaysWonderedWhyTheyHa_765C/image_thumb.png" width="377" height="195" /></a></p>
<p>So, if I hover over the “if” statement, I get the chance to invert the if clause</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/UsingResharperIveAlwaysWonderedWhyTheyHa_765C/image_3.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/UsingResharperIveAlwaysWonderedWhyTheyHa_765C/image_thumb_3.png" width="548" height="156" /></a></p>
<p>When I accept the invert if, I now get:</p>
<div class="csharpcode">
<pre class="alt"><span class="kwrd">if</span> (!doDeficitCalc)</pre>
<pre>{</pre>
<pre class="alt">    <span class="rem">// Linear interpolation from top of range to bottom for speed</span></pre>
<pre>    <span class="kwrd">double</span> x1 = rateBreakList[0];</pre>
<pre class="alt">    <span class="kwrd">double</span> x2 = rateBreakList[rateBreakList.Count - 1];</pre>
<pre>&#160;</pre>
<pre class="alt">    <span class="kwrd">double</span> y1 = rateList[0];</pre>
<pre>    <span class="kwrd">double</span> y2 = rateList[rateList.Count - 1];</pre>
<pre class="alt">&#160;</pre>
<pre>&#160;</pre>
<pre class="alt">    <span class="kwrd">double</span> frac = weight/(x2 - x1);</pre>
<pre>    <span class="kwrd">double</span> yResult = y1 + (frac*(y2 - y1));</pre>
<pre class="alt">    retWeight = yResult;</pre>
<pre>}</pre>
<pre class="alt"><span class="kwrd">else</span></pre>
<pre>{</pre>
<pre class="alt">    <span class="kwrd">throw</span></pre>
<pre>        <span class="kwrd">new</span> ApplicationException(<span class="str">&quot;Need to implement deficit weight rating&quot;</span>);</pre>
<pre class="alt">}</pre>
</div>
<style type="text/css">
<p>.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; }</style>
<p>Which has no warning!!!&#160; Just what I wanted with less key strokes.</p>
<p>Thank you again <a href="http://www.jetbrains.com/resharper/">ReSharper</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2009/10/18/refactor-csharp-if-resharper/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced (User agent is rejected)
Database Caching 30/35 queries in 0.010 seconds using disk: basic
Content Delivery Network via Amazon Web Services: S3: PetersBlogCDN.s3.amazonaws.com

Served from: peterkellner.net @ 2012-05-22 10:54:28 -->
