<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>jaredpar's WebLog : Refactoring</title><link>http://blogs.msdn.com/jaredpar/archive/tags/Refactoring/default.aspx</link><description>Tags: Refactoring</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>If it's not broken, maybe you should fix it anyway</title><link>http://blogs.msdn.com/jaredpar/archive/2008/10/13/if-it-s-not-broken-maybe-you-should-fix-it-anyway.aspx</link><pubDate>Mon, 13 Oct 2008 15:00:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8977484</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/8977484.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=8977484</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=8977484</wfw:comment><description>&lt;p&gt;I know this is goes against conventional wisdom but it's something I believe in.&amp;#160; Every sufficiently large project has that section of code nobody wants to go near.&amp;#160; The mere mention of it causes people to leave the room.&amp;#160; It usually has a couple of properties&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Old and Stagnant &lt;/li&gt;    &lt;li&gt;Critical or important routine &lt;/li&gt;    &lt;li&gt;Fragile &lt;/li&gt;    &lt;li&gt;Written by a developer who doesn't work there anymore &lt;/li&gt;    &lt;li&gt;Undocumented &lt;/li&gt;    &lt;li&gt;Untested &lt;/li&gt;    &lt;li&gt;Poorly understood &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;But it works and usually has for several releases.&amp;#160; By conventional wisdom it is considered &amp;quot;unbroken&amp;quot; and not worthy of changing.&amp;#160; &lt;/p&gt;  &lt;p&gt;I disagree.&amp;#160; Very often I find towards the end of the cycle more and more bugs start popping up in this code.&amp;#160; It's a pattern I've seen in many groups across releases.&amp;#160; These bugs are extremely expensive to fix because they are a) at the end of the cycle and b) being make in a piece of code that no one understands.&amp;#160; Since it's not broken nobody bothered with the code and hence it stayed poorly understood (and likely just as untested).&amp;#160; The bugs are doubly worse because no one wants to go near that code for fear of breaking the world.&amp;#160; &lt;/p&gt;  &lt;p&gt;Why let this code hang around in it's current state?&amp;#160; It's bad and no one understands why.&amp;#160; It's a constant source of pain late in the cycle and often in the middle.&amp;#160; &lt;/p&gt;  &lt;p&gt;I prefer to take a more proactive approach; fix it.&amp;#160; &lt;/p&gt;  &lt;p&gt;At the end of a product cycle I like to identify these pieces of code and attack them in-between releases.&amp;#160; This is a relatively safe point in the release cycle to change poorly understood code.&amp;#160; There is an entire release cycle ahead to catch the bugs introduced (and there will be some).&amp;#160; &lt;/p&gt;  &lt;p&gt;I don't break this code without thought.&amp;#160; I prefer to take a pragmatic approach to the code in the following steps. &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Attempt to understand the intent of the code (easier said than done).&amp;#160; I contact the original author if possible &lt;/li&gt;    &lt;li&gt;Poll the more senior developers on their understanding of what the code does &lt;/li&gt;    &lt;li&gt;Design a new architecture if needed &lt;/li&gt;    &lt;li&gt;Design a test plan (this is the absolute most important step.&amp;#160; Why fix this if you don't bother testing it?) &lt;/li&gt;    &lt;li&gt;Make the change &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I've done this, or overseen someone else, on several pieces of code over the last few years.&amp;#160; Thus far it's been a very big success.&amp;#160; &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8977484" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Patterns/default.aspx">Patterns</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Refactoring/default.aspx">Refactoring</category></item><item><title>C++ Refactoring: Default booleans and pointers</title><link>http://blogs.msdn.com/jaredpar/archive/2007/10/29/c-refactoring-default-booleans-and-pointers.aspx</link><pubDate>Mon, 29 Oct 2007 19:36:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5767556</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/5767556.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=5767556</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=5767556</wfw:comment><description>&lt;p&gt;This is another recount of an experience I had refactoring some C++ code recently.&amp;nbsp; In some ways this is also a follow up of my previous post about why you shouldn't use &lt;a href="http://blogs.msdn.com/jaredpar/archive/2007/01/23/boolean-parameters.aspx"&gt;boolean parameters&lt;/a&gt; (especially default value ones).&lt;/p&gt; &lt;p&gt;I recently refactored a large set of API's in our code base to remove a common parameter.&amp;nbsp; It was a member variable that was constantly being passed as an argument.&amp;nbsp; For many reasons we decide that this was unacceptable and it was better to use the member variable for instance methods and if needed define static methods which took the parameter as a value.&amp;nbsp; &lt;/p&gt; &lt;p&gt;After making the switch I recompiled and starting running regression suites on the code and found that a whole set were failing.&amp;nbsp; It turns out that many of our methods had the following pattern. &lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; Student;

&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; CalculateValue(Student *pStudent, Manager *pManager, &lt;span style="color: rgb(0,0,255)"&gt;bool&lt;/span&gt; cache=&lt;span style="color: rgb(0,0,255)"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;p&gt;The parameter I was removing was "pManager".&amp;nbsp; So now we have a function that used to take 2 or 3 arguments and now takes 1 or 2 arguments.&amp;nbsp; Unfortunately, now all of the places that used to call this function with 2 arguments will now recompile without warning since pointers are implicitly convertible to booleans.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;No matter.&amp;nbsp; Redefined the method with the same arguments and did not specify an implementation.&amp;nbsp; This caused compiler errors where necessary and allowed me to fixup the calling code.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Boolean parameters are troublesome.&amp;nbsp; Defaulted value boolean parameters are much more so. &lt;/p&gt;
&lt;p&gt;Yes you could attribute this to the fact that I didn't fully think through a subset of my changes and that's certainly fair.&amp;nbsp; However if we practiced good coding standards up front, this could have been avoided.&amp;nbsp; If we had originally done any of the following the code would be more resilient to changes.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Chosen an enum instead of a boolean parameter&lt;/li&gt;
&lt;li&gt;Forced the caller to specify the boolean parameter by not providing a default&lt;/li&gt;&lt;/ol&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5767556" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Refactoring/default.aspx">Refactoring</category></item></channel></rss>