<?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>Code Buddy</title>
	<atom:link href="http://www.codebuddy.co.uk/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codebuddy.co.uk</link>
	<description>Ramblings from the Coding Trenches</description>
	<lastBuildDate>Wed, 23 Dec 2009 13:55:55 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>That&#8217;s Not My Code!</title>
		<link>http://www.codebuddy.co.uk/thats-not-my-code/</link>
		<comments>http://www.codebuddy.co.uk/thats-not-my-code/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 13:53:02 +0000</pubDate>
		<dc:creator>codebuddy</dc:creator>
				<category><![CDATA[Book Reviews]]></category>

		<guid isPermaLink="false">http://www.codebuddy.co.uk/?p=433</guid>
		<description><![CDATA[I&#8217;ve just been reading with my 8 month old son. His favourite book at the moment is That&#8217;s not my Santa.

The book is part of the &#8220;That&#8217;s not my&#8221; series by Rachel Wells. Each book follows the same pattern; 5 examples of the subject, in this case Santa, that are not &#8220;mine&#8221; for some reason.
That&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just been reading with my 8 month old son. His favourite book at the moment is <em>That&#8217;s not my Santa</em>.</p>
<p style="text-align: center;">
<div class="wp-caption aligncenter" style="width: 250px"><a href="http://www.amazon.co.uk/gp/product/0746098359?ie=UTF8&amp;tag=mycafe-21&amp;linkCode=as2&amp;camp=1634&amp;creative=6738&amp;creativeASIN=0746098359"><img class=" " title="Thats Not My Santa" src="http://ecx.images-amazon.com/images/I/51nMP2BYFrL._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA240_SH20_OU02_.jpg" alt="Thats Not My Santa" width="240" height="240" /></a><p class="wp-caption-text">That&#39;s Not My Santa</p></div>
<p>The book is part of the &#8220;That&#8217;s not my&#8221; series by <a href="http://www.amazon.co.uk/exec/obidos/search-handle-url?_encoding=UTF8&amp;search-type=ss&amp;index=books-uk&amp;field-author=Rachel%20Wells">Rachel Wells</a>. Each book follows the same pattern; 5 examples of the subject, in this case Santa, that are not &#8220;mine&#8221; for some reason.</p>
<blockquote><p>That&#8217;s not my Santa. His sleigh is too sparkly.</p></blockquote>
<p>And the final page shows an example of a Santa that is mine.</p>
<blockquote><p>That&#8217;s my Santa! His beard is so fluffy.</p></blockquote>
<p>Each page has a some textures on it to backup the statement being made about Santa; Fluff on Santa&#8217;s beard, shiny paper on his sleigh that kind of thing.  Pre-school kids just love these books. They&#8217;re made of thick card, so can withstand even the most determined of teeth-er!</p>
<p>One theme I&#8217;m sure Rachel hasn&#8217;t thought of is&#8230;<em>code</em>.  If I was author <em>That&#8217;s not my code</em>, what would I use as the examples? How about</p>
<ul>
<li>That&#8217;s not my code. You can&#8217;t see the whole function on a single screen.</li>
<li>That&#8217;s not my code. It contains magic numbers.</li>
<li>That&#8217;s not my code. It doesn&#8217;t have any unit tests.</li>
<li>That&#8217;s not my code. It&#8217;s not been code reviewed.</li>
<li>That&#8217;s not my code. It&#8217;s got duplicated logic.</li>
</ul>
<p>And finally:</p>
<ul>
<li>That&#8217;s my code. It can be read by humans as well as computers.</li>
</ul>
<p>So, come on, get the the Christmas spirit &#8211; If you were to author <em>That&#8217;s not my code</em> how would it read?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codebuddy.co.uk/thats-not-my-code/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>DSCM &#8211; A Move Back to Big Bang Integration</title>
		<link>http://www.codebuddy.co.uk/dscm-a-move-back-to-big-bang-integration/</link>
		<comments>http://www.codebuddy.co.uk/dscm-a-move-back-to-big-bang-integration/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 07:33:36 +0000</pubDate>
		<dc:creator>codebuddy</dc:creator>
				<category><![CDATA[Coding Standards]]></category>

		<guid isPermaLink="false">http://www.codebuddy.co.uk/?p=382</guid>
		<description><![CDATA[Used sensibly the move away from centralised to distributed source control management offers more benefits than it does drawbacks. Used unwisely it could take us back to the days of Big Bang Integration
Branching Privately
Irrespective of the number of meetings attended or PowerPoint slides produced, developers communicate in one language, and one language only &#8211; the [...]]]></description>
			<content:encoded><![CDATA[<p>Used sensibly the move away from centralised to distributed source control management offers more benefits than it does drawbacks. Used unwisely it could take us back to the days of <a href="http://www.testinggeek.com/index.php/testing-types/life-cycle/55-bigbang-integration-testing">Big Bang Integration</a></p>
<p><strong>Branching Privately</strong></p>
<p>Irrespective of the number of meetings attended or PowerPoint slides produced, developers communicate in one language, and one language only &#8211; the language of  <em>code</em>. In many organisations trunk, or centrally hosted branches, give visibility of the work going on. This keeps everyone in the loop as to what is being developed, <em>how</em> its being done, and by <em>whom</em>.</p>
<p>Moving away from this work flow, towards teams working in local branches away from prying eyes, cuts this information flow. The development effort in these branches, now lacking the exposure to more than the team working on it, could miss out on vital feedback from other team members. Team member, that had the work been done centrally, allowing them to see what was happening, could have given feedback. This could have saved the team weeks or months of effort had they known that code already existed over here, or this team requires that module to do that as well, so don&#8217;t duplicate effort. These issues won&#8217;t be discovered until the big bang, the merge back into the mainline.</p>
<p><strong>Large Changesets</strong></p>
<p>Branches, by there very nature result in larger changesets being pushed back into the mainline. This can also be true when working on the main branch, but on a local repository. There can also be a temptation to complete a full feature before pushing the changes to the mainline. Developers will still be commiting their code in small changesets, but without giving it that push upstream to the mainline many benefits are lost.</p>
<p>Committing a feature little by little has many benefits. I&#8217;ve already mentioned peer review when talking about branching. In addition to this it allows detection of regressions to be detected as early as possible, making them easier to locate. Putting code into the mainline in feature size chunks certainly result in making it easier to track which changeset caused a regression, as there will be less of them. But finding which part of that feature sized changeset caused the regression will not be so easy.</p>
<p><strong>Enjoy responsibly</strong></p>
<p>Distributed soruce control management systems give us great power, but remember <a href="http://www.imdb.com/title/tt0145487/quotes">with great power comes great responsilbity</a>.<em><br />
</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codebuddy.co.uk/dscm-a-move-back-to-big-bang-integration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>4 Steps to Painless SVN Branching</title>
		<link>http://www.codebuddy.co.uk/4-steps-to-painless-svn-branching/</link>
		<comments>http://www.codebuddy.co.uk/4-steps-to-painless-svn-branching/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 09:01:52 +0000</pubDate>
		<dc:creator>codebuddy</dc:creator>
				<category><![CDATA[svn]]></category>

		<guid isPermaLink="false">http://www.codebuddy.co.uk/?p=258</guid>
		<description><![CDATA[Doing some work that can&#8217;t be done in trunk in small increments? Then it time to branch.
Say your working on bug &#8220;12345 Wibble&#8221;&#8230;
Step 1: Create the branch
Create a branch of trunk, making a note in the commit message of the revision of trunk the branch is being created from:

svn copy -r 20000
svn://svn/trunk
svn://svn/branches/trunk_wibble/
-m &#34;Bug 12345 Wibble [...]]]></description>
			<content:encoded><![CDATA[<p>Doing some work that can&#8217;t be done in trunk in small increments? Then it time to branch.</p>
<p>Say your working on bug &#8220;12345 Wibble&#8221;&#8230;</p>
<p><strong>Step 1: Create the branch</strong></p>
<p>Create a branch of trunk, making a note in the commit message of the revision of trunk the branch is being created from:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">svn</span> copy <span style="color: #660033;">-r</span> <span style="color: #000000;">20000</span>
<span style="color: #c20cb9; font-weight: bold;">svn</span>:<span style="color: #000000; font-weight: bold;">//</span>svn<span style="color: #000000; font-weight: bold;">/</span>trunk
<span style="color: #c20cb9; font-weight: bold;">svn</span>:<span style="color: #000000; font-weight: bold;">//</span>svn<span style="color: #000000; font-weight: bold;">/</span>branches<span style="color: #000000; font-weight: bold;">/</span>trunk_wibble<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #660033;">-m</span> <span style="color: #ff0000;">&quot;Bug 12345 Wibble -
Creating branch for feature work 
svn copy -r 20000 
svn://svn/trunk 
svn://svn/branches/trunk_wibble/&quot;</span></pre></div></div>

<p>There are now two types of commits you&#8217;ll be doing to this branch: </p>
<p>1 &#8211; Work for the feature<br />
2 &#8211; Resyncing with trunk. </p>
<p><strong>Step 2: Doing the feature work</strong></p>
<p>The feature work should be tracked against a bug as normal:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">svn</span> commit <span style="color: #660033;">-m</span> <span style="color: #ff0000;">&quot;Bug 12345 Wibble
- Added Foo for Wibble&quot;</span></pre></div></div>

<p><strong>Step 3: Merging changes from trunk into your branch</strong></p>
<p>The commit messages for the merges from trunk are crucial for the book keeping of your branch. Above we branched at revision 20000, suppose trunk is now at revision 30000. So to get those changes merged over in your branch, sit in trunk_wibble, with no other local changes:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">svn</span> merge <span style="color: #660033;">-r</span> <span style="color: #000000;">20000</span>:<span style="color: #000000;">30000</span> <span style="color: #c20cb9; font-weight: bold;">svn</span>:<span style="color: #000000; font-weight: bold;">//</span>svn<span style="color: #000000; font-weight: bold;">/</span>trunk .</pre></div></div>

<p>Once this is compiling commit it:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">svn</span> commit <span style="color: #660033;">-m</span> <span style="color: #ff0000;">&quot;Bug 12345 Wibble
- svn merge -r 20000:30000 svn://svn/trunk .&quot;</span></pre></div></div>

<p>Now next time you want to sync your branch with changes that have happned in trunk, all you need to do is look down the list of changesets to see what revision you last synced too.</p>
<p><strong>Step 4: Merging back into trunk</strong></p>
<p>Now your feature is done and dusted, its time to merge to trunk. You need to know 2 things here:</p>
<p>1 &#8211; What revision of your branch that was last synced to trunk, typically this will be the last commit you did you your branch<br />
2 &#8211; What revision of trunk you last synced to, this will be in your commit message for that final commit.</p>
<p>Suppose these are 40000 and 5000.</p>
<p>Sit yourself in trunk at the revision you last synced to:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">svn</span> merge 
<span style="color: #c20cb9; font-weight: bold;">svn</span>:<span style="color: #000000; font-weight: bold;">//</span>svn<span style="color: #000000; font-weight: bold;">/</span>trunk<span style="color: #000000; font-weight: bold;">/@</span><span style="color: #000000;">50000</span> 
<span style="color: #c20cb9; font-weight: bold;">svn</span>:<span style="color: #000000; font-weight: bold;">//</span>svn<span style="color: #000000; font-weight: bold;">/</span>branches<span style="color: #000000; font-weight: bold;">/</span>trunk_wibble<span style="color: #000000; font-weight: bold;">/@</span><span style="color: #000000;">40000</span> .</pre></div></div>

<p>Depending on how many changes you&#8217;ve made this may take a while. Be sure to give the changes a check over to check that match what you think you&#8217;ve changed.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">svn</span> commit <span style="color: #660033;">-m</span> 
<span style="color: #ff0000;">&quot;Bug 12345 Wibble
Merging feature work into trunk
svn merge 
svn://svn/trunk/@50000 
svn://svn/branches/trunk_wibble/@40000 .&quot;</span></pre></div></div>

<p>And your done!</p>
<p><strong>A final note about that last bit</strong></p>
<p>I&#8217;ve seen a <a href="http://designbygravity.wordpress.com/2009/10/19/what-mother-never-told-you-about-svn-branching-and-merging/">lot of confusion about this last step</a>. What your <em>not</em> trying to do is merge each of your feature work changesets into trunk. </p>
<p>You&#8217;re just after a delta between trunk and your branch so you can apply that to trunk to make it the same as your branch, thats it.</p>
<p>So, what happened exactly when we did the merge back into trunk? That merge was really a diff between trunk and your branch, followed by the application of that patch. You could actually achieve a similar result by doing</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">svn</span> <span style="color: #c20cb9; font-weight: bold;">diff</span>
<span style="color: #c20cb9; font-weight: bold;">svn</span>:<span style="color: #000000; font-weight: bold;">//</span>svn<span style="color: #000000; font-weight: bold;">/</span>trunk<span style="color: #000000; font-weight: bold;">/@</span><span style="color: #000000;">50000</span> 
<span style="color: #c20cb9; font-weight: bold;">svn</span>:<span style="color: #000000; font-weight: bold;">//</span>svn<span style="color: #000000; font-weight: bold;">/</span>branches<span style="color: #000000; font-weight: bold;">/</span>trunk_wibble<span style="color: #000000; font-weight: bold;">/@</span><span style="color: #000000;">40000</span> <span style="color: #000000; font-weight: bold;">&gt;</span> wibble.patch</pre></div></div>

<p>And then applying the patch manually.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">patch</span> <span style="color: #660033;">-p0</span> <span style="color: #000000; font-weight: bold;">&lt;</span> wibble.patch</pre></div></div>

<p>However you&#8217;d end up with empty files for those that had been removed in your branch, and files that needed to be svn add&#8217;ed for those that had been added in your branch. Doing it the svn merge way does all the deleting and adding for you. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.codebuddy.co.uk/4-steps-to-painless-svn-branching/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>I Smell Duplication Can You?</title>
		<link>http://www.codebuddy.co.uk/i-smell-duplication-can-you/</link>
		<comments>http://www.codebuddy.co.uk/i-smell-duplication-can-you/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 09:19:44 +0000</pubDate>
		<dc:creator>codebuddy</dc:creator>
				<category><![CDATA[Coding Standards]]></category>

		<guid isPermaLink="false">http://www.codebuddy.co.uk/?p=224</guid>
		<description><![CDATA[Want to know how to find duplicate code in your code base quickly and easily? Want to be able to sniff out the most pungent of code smells in double quick time? You&#8217;ve come to the right place.
Smells 
If your not familiar with the idea of code smells, the be sure to check out Martin [...]]]></description>
			<content:encoded><![CDATA[<p>Want to know how to find duplicate code in your code base quickly and easily? Want to be able to sniff out the most pungent of code smells in double quick time? You&#8217;ve come to the right place.</p>
<p><strong>Smells </strong></p>
<p>If your not familiar with the idea of code smells, the be sure to check out Martin Fowlers excellent book <a title="Refactoring Book" href="http://www.amazon.co.uk/gp/product/0201485672?ie=UTF8&amp;tag=mycafe-21&amp;linkCode=as2&amp;camp=1634&amp;creative=6738&amp;creativeASIN=0201485672">Refactoring: Improving the Design of Existing Code</a>. <a title="Code Smells" href="http://en.wikipedia.org/wiki/Code_smell">Code smells </a>are symptoms in your source code that can indicate problems &#8211; arguably the worst being code duplication which violates the principles of  <a title="DRY" href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a>.</p>
<p>The code maintenance issues are well known,  so I won&#8217;t revisit them here. What I want to do is talk about a tool I found the other day that helps find duplicate code.</p>
<p><a title="CPD" href="http://pmd.sourceforge.net/cpd.html">Copy Paste Detection </a>(CPD) is a great little program that can detect duplicate code in a code base.  Its available under a BSD-style licence, shipped as part of the <a title="PMD" href="http://pmd.sourceforge.net/">PMD</a> static code analyzer for Java. Although PMD is targeted as java, as CPD works using string matching, it can be used on any language.  Java, JSP, C, C++, Fortran and PHP are supported out of the box. It is also possible to <a title="How to add languages to CPD" href="http://pmd.sourceforge.net/cpd-parser-howto.html">add further langugages</a>.</p>
<p><strong>How to use it</strong></p>
<p>Running CPD is very simple:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>cpd.sh ~<span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>source<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p>And thats it. By default the output is in text format, this can be changed to xml or csv. Example output of processing the JDK (reported to take only 4 seconds) can be seen <a title="JDK PMD output" href="http://pmd.sourceforge.net/cpdresults.txt">here</a>. The number of duplicate tokens require for code to be considered copy and pasted can also be configured, this defaults to 100.</p>
<p><strong>My findings</strong></p>
<p>I had to increase the heap size available to java to get the code based I&#8217;m working on parsed. Its about a million lines of C/C++ code. There results were fascinating.  Sure enough, copy and pasted code was found, comments and all. Worse still, code that had been copy and pasted but not quite kept in sync, in most cases straight bugs.</p>
<p>The only real false positives I found were with auto generated code. By default CPD recursively parses the directories (you can supply as many as you like) on the command line, without being able to ignore certain files (eg *_autogen.cpp). As these files are produced as part of the build process, I&#8217;m now running CPD on a clean checkout, without build any build artifacts lying about.</p>
<p><strong>What next?</strong></p>
<p>As always with these things, I&#8217;m left with a bunch of open questions:</p>
<p>I can see this tool can offer some real value, but how do I integrate it into my teams work flow? Its a command line tool only, so there is no administration interface to allow results of various runs to be compared and analysed.</p>
<p>There are plenty of other static code analyzers that do much more than just check for duplication, what are peoples experiences with these?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codebuddy.co.uk/i-smell-duplication-can-you/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Are You Stuck In The Maintaince Programmer Mindset?</title>
		<link>http://www.codebuddy.co.uk/are-you-a-stuck-in-the-maintaince-programmer-mindset/</link>
		<comments>http://www.codebuddy.co.uk/are-you-a-stuck-in-the-maintaince-programmer-mindset/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 19:57:21 +0000</pubDate>
		<dc:creator>codebuddy</dc:creator>
				<category><![CDATA[Coding Standards]]></category>

		<guid isPermaLink="false">http://www.codebuddy.co.uk/?p=256</guid>
		<description><![CDATA[We&#8217;re all very good at pointing out whats wrong with code. There are even websites dedicated to exposing and ridiculing bad code and bad design, such as the daily wtf. We all encounter code that isn&#8217;t &#8220;right&#8221; on a daily basis, but how often do we do something about it and let our actions speak [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re all very good at pointing out whats wrong with code. There are even websites dedicated to exposing and ridiculing bad code and bad design, such as <a title="Daily wft" href="http://thedailywtf.com/">the daily wtf</a>. We all encounter code that isn&#8217;t &#8220;right&#8221; on a daily basis, but how often do we do something about it and let our actions speak louder than our words?</p>
<p>It&#8217;s very easy to point out whats wrong with code. Pointing out how the design and implementation of the code we&#8217;re working with is making our lives hard. Talking with fellow programmers about how our hands are tied and how we &#8220;did what we could&#8221; given that it &#8220;wasn&#8217;t our code&#8221;.</p>
<p>This all too familiar discussion demonstrates what I call the <em>maintenance programmer mindset</em>.</p>
<p>Are you too stuck in this mindset? Its very easy to fall into.</p>
<p>I have to remind myself regularly to break out of this mindset; to be bolder, to have the confidence to change the direction the code is heading. To make a clear statement about the problem domain by introducing a new class. To factor out that bit of repeated code into its own method. To break that monolithic module into two more focused modules to regain control over dependencies. To grab the wheel and make the kind of changes that steer the design down the correct path. The requirements on an active software project change constantly, so the design must also.</p>
<p>It&#8217;s quite easy to see if someone is stuck in this mindset. The following are a list of the actions that make a statement about the direction a code base is heading, in order of impact and are unlikely to be performed by sometime stuck in this mindset.</p>
<ul>
<li>Adding new new module/package</li>
<li>Adding a new class</li>
<li>Adding a new source file</li>
<li>Adding a new function</li>
</ul>
<p>If your not doing most of the above on a regular basis, irrespective of if your maintaining software or doing green field development &#8211; your programming with the maintainance programmers mindset.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codebuddy.co.uk/are-you-a-stuck-in-the-maintaince-programmer-mindset/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Brain Teaser: Streaming An Enumeration In C++</title>
		<link>http://www.codebuddy.co.uk/brain-teaser-streaming-an-enumeration-in-c-plus-plus/</link>
		<comments>http://www.codebuddy.co.uk/brain-teaser-streaming-an-enumeration-in-c-plus-plus/#comments</comments>
		<pubDate>Mon, 05 Oct 2009 20:33:59 +0000</pubDate>
		<dc:creator>codebuddy</dc:creator>
				<category><![CDATA[Brain Teasers]]></category>

		<guid isPermaLink="false">http://www.codebuddy.co.uk/?p=131</guid>
		<description><![CDATA[Streaming an enumeratoin in C++, what could be easier? Can you spot the bug in the following code?

typedef enum &#123;
    SEASON_UNDEF,
    SEASON_SUMMER,
    SEASON_AUTUMN,
    SEASON_WINTER,
    SEASON_SPRING,
    SEASON_NUM_TYPES,
&#125; SEASON;
&#160;
&#160;
std::ostream&#38; operator&#60;&#60;&#40;std::ostream&#38; rOs, 
        [...]]]></description>
			<content:encoded><![CDATA[<p>Streaming an enumeratoin in C++, what could be easier? Can you spot the bug in the following code?</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">enum</span> <span style="color: #008000;">&#123;</span>
    SEASON_UNDEF,
    SEASON_SUMMER,
    SEASON_AUTUMN,
    SEASON_WINTER,
    SEASON_SPRING,
    SEASON_NUM_TYPES,
<span style="color: #008000;">&#125;</span> SEASON<span style="color: #008080;">;</span>
&nbsp;
&nbsp;
std<span style="color: #008080;">::</span><span style="color: #007788;">ostream</span><span style="color: #000040;">&amp;</span> operator<span style="color: #000080;">&lt;&lt;</span><span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">ostream</span><span style="color: #000040;">&amp;</span> rOs, 
                         <span style="color: #0000ff;">const</span> SEASON <span style="color: #000040;">&amp;</span> rRhs<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">switch</span> <span style="color: #008000;">&#40;</span>rRhs<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">case</span> SEASON_UNDEF<span style="color: #008080;">:</span>
        rOs <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;SEASON_UNDEF&quot;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">case</span> SEASON_SUMMER<span style="color: #008080;">:</span>
        rOs <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;SEASON_SUMMER&quot;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">case</span> SEASON_AUTUMN<span style="color: #008080;">:</span>
        rOs <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;SEASON_AUTUMN&quot;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">case</span> SEASON_WINTER<span style="color: #008080;">:</span>
        rOs <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;SEASON_WINTER&quot;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">case</span> SEASON_SPRING<span style="color: #008080;">:</span>
        rOs <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;SEASON_SPRING&quot;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">default</span><span style="color: #008080;">:</span>
        rOs <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Unknown SEASON: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> rRhs<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> rOs<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Scroll down for the answer&#8230;.</p>
<p>&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br /> Nearly there&#8230;&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;</p>
<p>You&#8217;ve got it, its the default case. This makes a recusive call which will never terminate. Now how much stack space do I have&#8230;.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">rOs <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Unknown SEASON: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> rRhs<span style="color: #008080;">;</span> <span style="color: #666666;">// Recusive call!</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codebuddy.co.uk/brain-teaser-streaming-an-enumeration-in-c-plus-plus/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Overloading, Is It really Worth It?</title>
		<link>http://www.codebuddy.co.uk/overloading-is-it-really-worth-it/</link>
		<comments>http://www.codebuddy.co.uk/overloading-is-it-really-worth-it/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 19:52:04 +0000</pubDate>
		<dc:creator>codebuddy</dc:creator>
				<category><![CDATA[Coding Standards]]></category>

		<guid isPermaLink="false">http://www.codebuddy.co.uk/?p=110</guid>
		<description><![CDATA[For a long time I&#8217;ve used overloading, but just recently I&#8217;ve been questioning its uses.
Readability
Looking at the call site when invoking an overloaded function its not immediately obvious which method is being called.

find&#40;&#34;code buddy&#34;&#41;;
find&#40;C_PLUS_PLUS&#41;;

After a quick look through the overloading options available I can always work out which one will be called &#8211; but why [...]]]></description>
			<content:encoded><![CDATA[<p>For a long time I&#8217;ve used overloading, but just recently I&#8217;ve been questioning its uses.</p>
<h3>Readability</h3>
<p>Looking at the call site when invoking an overloaded function its not immediately obvious which method is being called.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">find<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;code buddy&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
find<span style="color: #008000;">&#40;</span>C_PLUS_PLUS<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>After a quick look through the overloading options available I can always work out which one will be called &#8211; but why should I? If code is making me think more than is absolutely neccessary then for my money, there is an issue with it. Wouldn&#8217;t the previous code be clearer if it was written like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">findByName<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;code buddy&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
findByLanguage<span style="color: #008000;">&#40;</span>C_PLUS_PLUS<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<h3>Tags</h3>
<p>My poor tags get confused! I&#8217;m working in a C++ code base on a linux OS. Rightly or wrongly, I&#8217;m using <a href="http://www.gnu.org/software/emacs/">emacs </a>with <a href="http://cscope.sourceforge.net/">cscope </a> as my chosen editing/tagging solution.  For example, if I&#8217;m working with the <a href="http://en.wikipedia.org/wiki/Visitor_pattern">Visitor Pattern </a> that is using overloading for its visit method, as search for:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> visit<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> NodeA<span style="color: #000040;">&amp;</span> node<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>also finds me:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> visit<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> NodeB<span style="color: #000040;">&amp;</span> node<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">void</span> visit<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> NodeC<span style="color: #000040;">&amp;</span> node<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>This maybe a shortcoming of cscope, however I&#8217;ve seen other tagging solutions, notably the one used in <a href="http://www.slickedit.com/">Slick Edit </a>fall at the same hurdle.  I&#8217;d much rather see this visitor interface written like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> visitNodeA<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> NodeA<span style="color: #000040;">&amp;</span> node<span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> visitNodeB<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> NodeB<span style="color: #000040;">&amp;</span> node<span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> visitNodeC<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> NodeC<span style="color: #000040;">&amp;</span> node<span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span></pre></div></div>

<h3>Templates</h3>
<p>It maybe nessary for templatised code that you use overloading, eg:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span>Node<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">void</span> doSomeStuffAndVisit<span style="color: #008000;">&#40;</span>IVisitor<span style="color: #000040;">&amp;</span> visitor,
                        <span style="color: #0000ff;">const</span> Node<span style="color: #000040;">&amp;</span> node<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// some code</span>
    visitor.<span style="color: #007788;">visit</span><span style="color: #008000;">&#40;</span>node<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #666666;">// some more code</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h3>Constructors</h3>
<p>Overloading of constuctors is unavoidable.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// Creates a Foo from the xml in file file</span>
Foo<span style="color: #008080;">::</span><span style="color: #007788;">Foo</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000040;">&amp;</span> file<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// Creates a Foo from the xml node root</span>
Foo<span style="color: #008080;">::</span><span style="color: #007788;">Foo</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> xmlNode<span style="color: #000040;">&amp;</span> root<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Unless of course your using a factory method, in which case, there is no need to overload like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// Creates a Foo from the xml in file file</span>
<span style="color: #000040;">*</span>Foo FooFactory<span style="color: #008080;">::</span><span style="color: #007788;">createFoo</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000040;">&amp;</span> file<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// Creates a Foo from the xml node root</span>
<span style="color: #000040;">*</span>Foo FooFactory<span style="color: #008080;">::</span><span style="color: #007788;">createFoo</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> xmlNode<span style="color: #000040;">&amp;</span> root<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>The comments really are the give away here, taking the comments away things become so much more readable.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">FooFactory<span style="color: #008080;">::</span><span style="color: #007788;">createFooFromFile</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000040;">&amp;</span> file<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
FooFactory<span style="color: #008080;">::</span><span style="color: #007788;">createFooFromXMLNode</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> xmlNode<span style="color: #000040;">&amp;</span> root<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<h3>Conclusion</h3>
<p>These days, unless I&#8217;m working with any code thats using templates, i&#8217;m avoiding overloading, the pros are much outweighed by the cons.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codebuddy.co.uk/overloading-is-it-really-worth-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>4 Things To Consider When Fixing a Bug</title>
		<link>http://www.codebuddy.co.uk/4-things-to-consider-when-fixing-a-bug/</link>
		<comments>http://www.codebuddy.co.uk/4-things-to-consider-when-fixing-a-bug/#comments</comments>
		<pubDate>Fri, 10 Apr 2009 15:59:29 +0000</pubDate>
		<dc:creator>codebuddy</dc:creator>
				<category><![CDATA[Bugs]]></category>

		<guid isPermaLink="false">http://codebuddy.wordpress.com/?p=10</guid>
		<description><![CDATA[So, you&#8217;ve just spent a few hours, days, or maybe weeks(!) tracking that down that bug. You&#8217;ve finally found it &#8211; what do you do? The fix is trivial, its a one line change, you can fix it right now, have it committed to source control, picked up in the next software release and move [...]]]></description>
			<content:encoded><![CDATA[<p>So, you&#8217;ve just spent a few hours, days, or maybe weeks(!) tracking that down that bug. You&#8217;ve finally found it &#8211; what do you do? The fix is trivial, its a one line change, you can fix it right now, have it committed to source control, picked up in the next software release and move on to reading slashdot.</p>
<p>But before you go ahead and change that line of code, there are a few questions you should be asking yourself:</p>
<p><strong>#1 How hard was it to track this bug down?</strong></p>
<p>If you&#8217;ve been crawling over broken glass for a couple of days the chances are you&#8217;ve been adding some extra diagnostics to the system &#8211; these should be committed as part of the bug fix. Do not underestimate the importance of an easily diagnosable system, it will save you lots of time in the long run, not just for bug fixing, but during general development as well.</p>
<p><strong>#2 Can I prevent the bug from being reintroduced in the future?</strong></p>
<p>Now you can reproduce the bug (you have reproduced it before your claiming its fixed right?) &#8211; you need to write some form of test, why?</p>
<ul>
<li>The tests will first expose the bug, then prove the fix.</li>
<li>The test will act as an insurance policy on your fix, as long as no one removes the test, the bug will stay fixed.</li>
</ul>
<p>Ideally you&#8217;ll expose this via a unit test. This is the quickest form of feedback a developer can be given about the state of the system and will allow you to develop the fix with a very short feedback loop. If this is not possible,  then a regression test is your next best bet.</p>
<p><strong>#3 Have I had it code reviewed?</strong></p>
<p>A code review need only take a few minutes of another developers time, and will double the number of people that inspect the code before you commit, not a bad return really. If the code review takes longer, then the changes must be significant, or the problem domain suitable complex that running the change by another devloper really is a no brainer.</p>
<p><strong>#4 Do I have a bug number to commit the change against?</strong></p>
<p>Be sure to reference the bug number when you commit your fix. You don&#8217;t need to detail the symptoms of the bug in the commit, that is what a bug tracking system is for. Not only will this prove useful to other devlopers when they see your changes go in, it will also allow those that raised the bug see that work is happening on it.</p>
<p><strong>Conclusion</strong></p>
<p>These are the questions I go through in my mind before committing a fix &#8211; let me know yours!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codebuddy.co.uk/4-things-to-consider-when-fixing-a-bug/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
