<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Basildon Coder</title>
	
	<link>http://basildoncoder.com/blog</link>
	<description>Incoherent and disjointed opinionated drivel from somewhere near London</description>
	<lastBuildDate>Fri, 11 Dec 2009 13:40:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/BasildonCoder" /><feedburner:info uri="basildoncoder" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Marshalling a Variable-Length Array From Unmanaged Code In C#</title>
		<link>http://feedproxy.google.com/~r/BasildonCoder/~3/_T49j8GrFpE/</link>
		<comments>http://basildoncoder.com/blog/2009/03/31/marshalling-a-variable-length-array-from-unmanaged-code-in-c/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 19:03:38 +0000</pubDate>
		<dc:creator>russ</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://basildoncoder.com/blog/2009/03/31/marshalling-a-variable-length-array-from-unmanaged-code-in-c/</guid>
		<description><![CDATA[I recently spent time working on some C# code to interact with a simple DNS-SD system. This requires using DNS TXT records, which are not supported in the System.Net.Dns class. After a few google searches failed to turn up a pure .Net client library that met my needs, I settled on an approach based around [...]]]></description>
			<content:encoded><![CDATA[<p>I recently spent time working on some C# code to interact with a simple <a href="http://files.dns-sd.org/draft-cheshire-dnsext-dns-sd.txt">DNS-SD</a> system. This requires using <a href="http://en.wikipedia.org/wiki/List_of_DNS_record_types">DNS TXT records</a>, which are not supported in the <a href="http://msdn.microsoft.com/en-us/library/system.net.dns.aspx">System.Net.Dns</a> class. After a few google searches failed to turn up a pure .Net client library that met my needs, I settled on an approach based around p/invoking the Win32 <a href="http://msdn.microsoft.com/en-us/library/ms682016(VS.85).aspx">DnsQuery</a> function.</p>
<p>And quickly ran into problems.</p>
<p>For DNS TXT records, DnsQuery returns a <a href="http://msdn.microsoft.com/en-us/library/ms682109(VS.85).aspx">DNS_TXT_DATA</a> structure in the Data field of the <a href="http://msdn.microsoft.com/en-us/library/ms682082(VS.85).aspx">DNS_RECORD</a> structure. DNS_TXT_DATA is declared like this:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> <span style="color: #009900;">&#123;</span>
    DWORD dwStringCount<span style="color: #339933;">;</span>
    PWSTR pStringArray<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> DNS_TXT_DATA<span style="color: #339933;">,</span>
    <span style="color: #339933;">*</span>PDNS_TXT_DATA<span style="color: #339933;">;</span></pre></div></div>

<p>Using the very handy <a href="http://clrinterop.codeplex.com/">P/Invoke Interop Assistant</a>, we see that this struct can be represented like this in managed code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>StructLayout<span style="color: #000000;">&#40;</span>LayoutKind.<span style="color: #0000FF;">Sequential</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">struct</span> DNS_TXT_DATA <span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// DWORD-&gt;unsigned int</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">uint</span> dwStringCount<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// PWSTR[1]</span>
    <span style="color: #000000;">&#91;</span>MarshalAs<span style="color: #000000;">&#40;</span>UnmanagedType.<span style="color: #0000FF;">ByValArray</span>,
            SizeConst<span style="color: #008000;">=</span><span style="color: #FF0000;">1</span>,
            ArraySubType<span style="color: #008000;">=</span>UnmanagedType.<span style="color: #0000FF;">SysUInt</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> IntPtr<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> pStringArray<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>There is a problem with pStringArray, unfortunately. The <a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.aspx">System.Runtime.InteropServices.Marshal</a> class cannot marshal a variable length array, as it needs to know in advance how big the array is in order to allocate memory. That&#8217;s why the managed structure needs SizeConst specified in the MarshalAs attribute.</p>
<p>However, if the DNS TXT record data contains multiple quoted strings separated by whitespace, DnsQuery will return a structure with a variable number of elements in pStringArray. Since SizeConst is set at compile-time, when we marshal this into the managed struct defined above, we only get the first element in our single-element array. Rats.</p>
<p>More googling turned up very little info on dealing with this, though I found indications that others had run into the same problem without finding a satisfactory conclusion. DnsQuery is not the only Win32 function that returns variable-length arrays, and p/invoking any of the others has the same issue.</p>
<p>Simply declaring SizeConst to be bigger than we need &#8211; &#8220;hey, I know I&#8217;ll never get more than 10 or so strings back, so why not declare SizeConst to be 128?&#8221; &#8211; is inelegant (hardcoded upper limits, ugh) and doesn&#8217;t work properly anyway. Since the struct layout is sequential the marshaller will copy over (e.g.) 128*sizeof(IntPtr) sequential bytes (a total of 512 bytes, in this case). That much memory was never allocated on the unmanaged side, so we end up with a load of junk in the tail of pStringArray, and more often than not the marshaller chokes on this junk and throws an AccessViolationException. Fun.</p>
<p>There IS a way to get round the problem, though. I&#8217;m not sure it&#8217;s the best way, but it works and seems stable, so I thought I&#8217;d throw it out there in case anyone else can use it (or maybe explain to me why it&#8217;s an unsafe stupid thing to do&#8230;)</p>
<p>Basically, since we&#8217;re dealing with sequential memory, we can use Marshal.PtrToStructure to marshal the DNS_TXT_DATA structure as defined above, then use pointer arithmetic to gain access to any further data that needs marshalling.</p>
<p>Pointer arithmetic? Oh yes, even in the safe and secure world of managed code it&#8217;s sometimes still necessary to get our hands dirty, and situations like this illustrate that it will always be valuable to have some hard-earned Assembly/C/C++ war wounds.</p>
<p>So, assuming we have valid p/invoke declarations and data structures (I&#8217;ve included a complete source program below), DnsQuery is called like so:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var pServers <span style="color: #008000;">=</span> IntPtr.<span style="color: #0000FF;">Zero</span><span style="color: #008000;">;</span>
var ppQueryResultsSet <span style="color: #008000;">=</span> IntPtr.<span style="color: #0000FF;">Zero</span><span style="color: #008000;">;</span>
var ret <span style="color: #008000;">=</span> DnsQuery<span style="color: #000000;">&#40;</span>domain,
        DnsRecordType.<span style="color: #0000FF;">TEXT</span>,
        DnsQueryType.<span style="color: #0000FF;">STANDARD</span>,
        pServers,
        <span style="color: #0600FF;">ref</span> ppQueryResultsSet,
        IntPtr.<span style="color: #0000FF;">Zero</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>ret <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ApplicationException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;DnsQuery failed: &quot;</span> <span style="color: #008000;">+</span> ret<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>If we examine the memory location of ppQueryResultsSet (Ctrl-Alt-M,1 or Debug-&gt;Windows-&gt;Memory-&gt;Memory1 in Visual Studio) we&#8217;ll see something like the following (actual address locations may vary &#8211; just copy the int value of ppQueryResultsSet to the Address bar of the memory window):</p>
<pre>0x049E0878  00 00 00 00  ....
0x049E087C  b8 09 9e 04  ¸.ž.
0x049E0880  10 00 20 00  .. .
0x049E0884  19 30 00 00  .0..
0x049E0888  00 00 00 00  ....
0x049E088C  00 00 00 00  ....
0x049E0890  06 00 00 00  ....
0x049E0894  b8 08 9e 04  ¸.ž.
0x049E0898  d8 08 9e 04  Ø.ž.
0x049E089C  f8 08 9e 04  ø.ž.
0x049E08A0  28 09 9e 04  (.ž.
0x049E08A4  68 09 9e 04  h.ž.
0x049E08A8  88 09 9e 04  ˆ.ž.</pre>
<p>I&#8217;ve set the column size to 4 here, as most of the values we are dealing with are 4 bytes in size. This effectively shows one value per line.</p>
<p>The first 6 rows (24 bytes) correspond to the DNS_RECORD structure up until (but not including) the DNS_TXT_DATA structure in DNS_RECORD&#8217;s Data union. We can marshal this first structure without problem:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var dnsRecord <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>DnsRecord<span style="color: #000000;">&#41;</span> Marshal.<span style="color: #0000FF;">PtrToStructure</span><span style="color: #000000;">&#40;</span>
        ppQueryResultsSet, <span style="color: #008000;">typeof</span> <span style="color: #000000;">&#40;</span>DnsRecord<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>The DNS_TXT_DATA structure starts at address 0x049E0890 in my example. Having already marshalled the DNS_RECORD structure, now I want a pointer to the DNS_TXT_DATA structure. I can do this by creating a new pointer at the address of ppQueryResultsSet plus 24 bytes, and marshalling again:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var ptr <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> IntPtr<span style="color: #000000;">&#40;</span>
        ppQueryResultsSet.<span style="color: #0000FF;">ToInt32</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> Marshal.<span style="color: #008000;">SizeOf</span><span style="color: #000000;">&#40;</span>dnsRecord<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
var txtData <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>DNS_TXT_DATA<span style="color: #000000;">&#41;</span> Marshal.<span style="color: #0000FF;">PtrToStructure</span><span style="color: #000000;">&#40;</span>
        ptr, <span style="color: #008000;">typeof</span> <span style="color: #000000;">&#40;</span>DNS_TXT_DATA<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Because of the definition of DNS_TXT_DATA, this only marshals 8 bytes &#8211; 4 bytes for dwStringCount, and 4 bytes for the single element in pStringArray (an IntPtr). Since we know the memory is sequential, however, this gives us everything we need &#8211; we now know how many strings have been received (6 in this case, as indicated at 0x049E0890), and the location of the pointer to the first string (0x049E0894).</p>
<p>With this info, we can marshal all the pointers into an array with a length of dwStringCount:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">ptr <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> IntPtr<span style="color: #000000;">&#40;</span>ptr.<span style="color: #0000FF;">ToInt32</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #008000;">sizeof</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">uint</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// move to first</span>
var ptrs <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> IntPtr<span style="color: #000000;">&#91;</span>txtData.<span style="color: #0000FF;">dwStringCount</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// dest array</span>
Marshal.<span style="color: #0000FF;">Copy</span><span style="color: #000000;">&#40;</span>ptr, ptrs, <span style="color: #FF0000;">0</span>, ptrs.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>And finally we iterate through those pointers, marshalling the string pointed at by each:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var strings <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span>var i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> ptrs.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> <span style="color: #008000;">++</span>i<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    strings.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>Marshal.<span style="color: #0000FF;">PtrToStringAnsi</span><span style="color: #000000;">&#40;</span>ptrs<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>While the example I&#8217;ve presented here is specific to DnsQuery, the general approach should be applicable to any situation where you need to marshal a data structure containing a variable-length array.</p>
<p><a href="http://basildoncoder.com/marshal-variable-length-array.cs">Source code</a></p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://basildoncoder.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://basildoncoder.com/blog/2009/03/31/marshalling-a-variable-length-array-from-unmanaged-code-in-c/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://basildoncoder.com/blog/2009/03/31/marshalling-a-variable-length-array-from-unmanaged-code-in-c/</feedburner:origLink></item>
		<item>
		<title>Project Euler Problem 8</title>
		<link>http://feedproxy.google.com/~r/BasildonCoder/~3/qhUl-vmfXDI/</link>
		<comments>http://basildoncoder.com/blog/2009/02/09/project-euler-problem-8/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 19:30:13 +0000</pubDate>
		<dc:creator>russ</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://basildoncoder.com/blog/2009/02/09/project-euler-problem-8/</guid>
		<description><![CDATA[Problem 8 &#8220;Find the greatest product of five consecutive digits in the 1000-digit number. 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450&#8243; The first step here is to find a representation for that fairly humungous number. Obviously it&#8217;s not going to fit into [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://projecteuler.net/index.php?section=problems&amp;id=8"><em><strong>Problem 8</strong></em></a></p>
<blockquote><p>&#8220;Find the greatest product of five consecutive digits in the 1000-digit number.</p>
<p>73167176531330624919225119674426574742355349194934<br />
96983520312774506326239578318016984801869478851843<br />
85861560789112949495459501737958331952853208805511<br />
12540698747158523863050715693290963295227443043557<br />
66896648950445244523161731856403098711121722383113<br />
62229893423380308135336276614282806444486645238749<br />
30358907296290491560440772390713810515859307960866<br />
70172427121883998797908792274921901699720888093776<br />
65727333001053367881220235421809751254540594752243<br />
52584907711670556013604839586446706324415722155397<br />
53697817977846174064955149290862569321978468622482<br />
83972241375657056057490261407972968652414535100474<br />
82166370484403199890008895243450658541227588666881<br />
16427171479924442928230863465674813919123162824586<br />
17866458359124566529476545682848912883142607690042<br />
24219022671055626321111109370544217506941658960408<br />
07198403850962455444362981230987879927244284909188<br />
84580156166097919133875499200524063689912560717606<br />
05886116467109405077541002256983155200055935729725<br />
71636269561882670428252483600823257530420752963450&#8243;</p></blockquote>
<p>The first step here is to find a representation for that fairly humungous number. Obviously it&#8217;s not going to fit into a paltry 32-bit int&#8230;but then we don&#8217;t need it to. The problem description requires us to think in terms of smaller (5-digit) numbers, not one giant 1000-digit number.</p>
<p>So, it is sufficient for us to consider the number as an enumerable stream of single digits, which we can conveniently represent as IEnumerable<int>. I could use a macro to convert the number into a collection initialiser, but it&#8217;s much easier to treat the string as an IEnumerable<char>&lt;char&gt; and let LINQ do the heavy lifting.</char></int></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var nums <span style="color: #008000;">=</span> Enumerable.<span style="color: #0000FF;">AsEnumerable</span><span style="color: #000000;">&#40;</span>
        <span style="color: #666666;">&quot;73167176531330624919225119674426574742355349194934&quot;</span> <span style="color: #008000;">+</span>
        <span style="color: #666666;">&quot;96983520312774506326239578318016984801869478851843&quot;</span> <span style="color: #008000;">+</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// ..... etc etc ......</span>
&nbsp;
        <span style="color: #666666;">&quot;05886116467109405077541002256983155200055935729725&quot;</span> <span style="color: #008000;">+</span>
        <span style="color: #666666;">&quot;71636269561882670428252483600823257530420752963450&quot;</span>
        <span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> Convert.<span style="color: #0000FF;">ToInt32</span><span style="color: #000000;">&#40;</span>x.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>This gives us an IEnumerable<int>&lt;int&gt; containing every digit in the 1000-digit number. Now, the &#8216;obvious&#8217; way to solve the problem is to iterate through the collection, and at each index multiply the value against the next four indexes. A simple loop should deal with it:</int></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">int</span> SimpleSolver<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> ints<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">int</span> max <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> ints.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">4</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #FF0000;">int</span> tmp <span style="color: #008000;">=</span> ints<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #008000;">*</span> ints<span style="color: #000000;">&#91;</span>i <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">*</span> ints<span style="color: #000000;">&#91;</span>i <span style="color: #008000;">+</span> <span style="color: #FF0000;">2</span><span style="color: #000000;">&#93;</span>
            <span style="color: #008000;">*</span> ints<span style="color: #000000;">&#91;</span>i <span style="color: #008000;">+</span> <span style="color: #FF0000;">3</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">*</span> ints<span style="color: #000000;">&#91;</span>i <span style="color: #008000;">+</span> <span style="color: #FF0000;">4</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
        max <span style="color: #008000;">=</span> Math.<span style="color: #0000FF;">Max</span><span style="color: #000000;">&#40;</span>max, tmp<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">return</span> max<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>As ever, though, that&#8217;s pretty ugly &#8211; the loop condition and product calculation is tied to the sequence size of 5, and messing with an index variable is tedious.</p>
<p>An alternative approach is to take advantage of LINQ&#8217;s Skip and Take methods to split the problem domain into overlapping &#8216;slices&#8217;. Similar to the for loop above, the core of the approach is to iterate through the digits, and at each digit grab a number of subsequent digits and calculate the product.</p>
<p>Lets look at the 5-digit slices available from the first 10 digits:</p>
<pre>
  7   3   1   6   7   1   7   6   5   3
|       73167       |
    |       31671       |
        |       16717       |
            |       67176       |
                |       71765       |
                    |       17653       |</pre>
<p>We can use Skip to progressively move the starting index forward, and Take to grab the 5 digits we need. So, starting with i=0, each successive slice can be sliced from the whole with:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var slice <span style="color: #008000;">=</span> ints.<span style="color: #0000FF;">Skip</span><span style="color: #000000;">&#40;</span>i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Take</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">5</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>To calculate the product of the digits in the slice, we can use the Aggregate operation:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">slice.<span style="color: #0000FF;">Aggregate</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span>, <span style="color: #000000;">&#40;</span>curr, next<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> curr<span style="color: #008000;">*</span>next<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>We&#8217;ve met Aggregate before &#8211; it&#8217;s basically a fold, which collapses a sequence to a single item by repeatedly applying an operation to an accumulating result.</p>
<p>This can all be wrapped up as an iterator block, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> EnumerateSlices<span style="color: #000000;">&#40;</span>
        IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> ints, <span style="color: #FF0000;">int</span> sliceSize<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        var slice <span style="color: #008000;">=</span> ints.<span style="color: #0000FF;">Skip</span><span style="color: #000000;">&#40;</span>i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Take</span><span style="color: #000000;">&#40;</span>sliceSize<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>slice.<span style="color: #0000FF;">Count</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&lt;</span> sliceSize<span style="color: #000000;">&#41;</span>
            yield break<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// end</span>
&nbsp;
        yield <span style="color: #0600FF;">return</span> slice.<span style="color: #0000FF;">Aggregate</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span>,
                <span style="color: #000000;">&#40;</span>curr, next<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> curr<span style="color: #008000;">*</span>next<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Note the termination condition &#8211; when we have enumerated every slice, our next slice will contain only 4 elements (3, 4, 5, and 0 from the end of the sequence) &#8211; that&#8217;s our cue to exit the loop.</p>
<p>Also note that this approach makes the algorithm trivial to parameterize &#8211; it will work just as well with slice sizes other than 5.</p>
<p>This iterator will produce an IEnumerable<int> containing the products of all slices, so the final step is to select the largest:</int></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var result <span style="color: #008000;">=</span> EnumerateSlices<span style="color: #000000;">&#40;</span>nums, <span style="color: #FF0000;">5</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Max</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://basildoncoder.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://basildoncoder.com/blog/2009/02/09/project-euler-problem-8/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://basildoncoder.com/blog/2009/02/09/project-euler-problem-8/</feedburner:origLink></item>
		<item>
		<title>Killing and Reviving an Aspire One</title>
		<link>http://feedproxy.google.com/~r/BasildonCoder/~3/zmpaCERC5ZI/</link>
		<comments>http://basildoncoder.com/blog/2009/02/08/killing-and-reviving-an-aspire-one/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 21:01:23 +0000</pubDate>
		<dc:creator>russ</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://basildoncoder.com/blog/2009/02/08/killing-and-reviving-an-aspire-one/</guid>
		<description><![CDATA[I just spent 2 hours reviving my Aspire One netbook after inadvertently killing it whilst fiddling about configuring dropbox. I found the whole process unnecessarily fiddly and information on the interwebs to be a bit scarcer than I would have liked, so I&#8217;m documenting it here in case I need it in the future. Hopefully [...]]]></description>
			<content:encoded><![CDATA[<p>I just spent 2 hours reviving my Aspire One netbook after inadvertently killing it whilst fiddling about configuring <a href="http://www.getdropbox.com">dropbox</a>. I found the whole process unnecessarily fiddly and information on the interwebs to be a bit scarcer than I would have liked, so I&#8217;m documenting it here in case I need it in the future. Hopefully it&#8217;ll be useful to someone else too.</p>
<p>So, the cause of death was a typo when trying to set up the dropboxd daemon to start automatically on boot. I&#8217;m not running nautilus so couldn&#8217;t use one of the prepackaged releases, and it&#8217;s completely my fault that I made a mess of installing the vanilla x86 build.</p>
<p>After making the fatal change and rebooting, the system would only boot up to a blank black screen with a default X mouse cursor. This is because the system was trying to run my broken command, failing, and therefore never getting to the main desktop.</p>
<p>In the world of normal linux, there&#8217;s all sorts of ways of dealing with this, but despite plenty of googling I couldn&#8217;t find a way to use run-level 2 or 3 on an Aspire One, and the Ctrl+Alt+F1-F6 key combos for switching away from X to a terminal don&#8217;t work either. There seems to be no way of preventing the system following the same doomed process over and again if you break X.</p>
<p>Frustrated, I thought about using the restore disk, but that&#8217;s a nuclear option &#8211; it re-paves the whole machine, so bye-bye data. That seemed a bit drastic when all I needed to do was edit a single text file to fix the system.</p>
<p>Ironically, this was happening as a result of me trying to install a file sync system as a simple backup. Grr.</p>
<p>Still, like countless thousands before me, I was saved by a live linux distro &#8211; in this case, a USB bootable one (since the Aspire One has no optical drive). Following the <a href="http://www.pendrivelinux.com/feather-linux-on-usb/">instructions</a><sup>[1]</sup> at <a href="http://www.pendrivelinux.com/">pendrivelinux</a> I created a bootable Feather Linux USB drive, and booted the netbook from it by hitting F12 on the post screen and selecting to boot from the USB stick.</p>
<p>At the boot prompt, I used &#8216;knoppix 3&#8242; to boot the system up to a command line, mounted /dev/hdc1 as an ext2 filesystem, and fixed my typo. Reboot, and tada! Everything was working again (well, after hitting Fn-F7 to reenable the touchpad, which I had accidentally disabled whilst mashing the keyboard in frustration at the sight of a blank screen about an hour earlier, heh).</p>
<p><sup>[1]</sup> Note that I had to use a newer version of syslinux than the one referenced on pendrivelinux. <a href="http://www.kernel.org/pub/linux/utils/boot/syslinux/Old/syslinux-3.36.zip">This one </a>worked for me.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://basildoncoder.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://basildoncoder.com/blog/2009/02/08/killing-and-reviving-an-aspire-one/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://basildoncoder.com/blog/2009/02/08/killing-and-reviving-an-aspire-one/</feedburner:origLink></item>
		<item>
		<title>Macros: You Oughta Know</title>
		<link>http://feedproxy.google.com/~r/BasildonCoder/~3/HGgDlQNKHaE/</link>
		<comments>http://basildoncoder.com/blog/2009/02/06/macros-you-oughta-know/#comments</comments>
		<pubDate>Fri, 06 Feb 2009 15:03:06 +0000</pubDate>
		<dc:creator>russ</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://basildoncoder.com/blog/2009/02/06/macros-you-oughta-know/</guid>
		<description><![CDATA[One of the most useful tools available in any decent text editor is the macro recorder, but it&#8217;s criminally underused. It seems most people either don&#8217;t know the functionality exists, or simply ignore it. This is a shame, since it&#8217;s a great timesaver. I don&#8217;t know why macros are so underused. It might be a [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most useful tools available in any decent text editor is the macro recorder, but it&#8217;s criminally underused. It seems most people either don&#8217;t know the functionality exists, or simply ignore it. This is a shame, since it&#8217;s a great timesaver.</p>
<p>I don&#8217;t know why macros are so underused. It might be a mindset thing &#8211; it can take a little while to develop the ability to spot repetitive editing tasks quickly (i.e. not when you&#8217;re 75% of the way through thinking <em>dang, I have to do this again?</em>), so maybe many people never quite make the leap.</p>
<p>It&#8217;s worth it though, because once you get your eye in you see chances to use macros everywhere.</p>
<p>I had a useful example just yesterday, in which I needed to make a change to a colossal switch statement (220 branches! Run the cyclomatic-complexity doohickey on THAT!) and had no unit tests to fall back on.</p>
<p>If I had to modify (and hopefully refactor) such a huge construct I wanted to be able to compare before-and-after test results, but I didn&#8217;t much fancy hand-cranking a few hundred unit tests.</p>
<p>By recording a temporary macro, however, it took just a couple of minutes to cover every branch. I&#8217;ve decided to post a detailed walkthrough of the process here in the hopes that a fairly simple example will be illustrative for those that don&#8217;t already lean heavily on macros.</p>
<p>Note that <em>this is not an advanced tutorial</em>. Please refrain from leaving snarky comments about how macros are so much more powerful than this &#8211; I&#8217;m just doing some introductory material here <img src='http://basildoncoder.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Here is a representative snippet of the C# source. It&#8217;s part of a legacy permissioning system that, under certain circumstances, needs to check for the existence of a permission represented by an enum against a permission table containing a string-based hierarchy (application/role/permission). The code I was modifying did the appropriate conversion:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF;">case</span> PermissionKey.<span style="color: #0000FF;">SecurityParameterManagementAdd</span><span style="color: #008000;">:</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span><span style="color: #666666;">&quot;Security&quot;</span>, <span style="color: #666666;">&quot;ParameterManagement&quot;</span>, <span style="color: #666666;">&quot;Add&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">case</span> PermissionKey.<span style="color: #0000FF;">SecurityRoleManagementAdd</span><span style="color: #008000;">:</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span><span style="color: #666666;">&quot;Security&quot;</span>, <span style="color: #666666;">&quot;RoleManagement&quot;</span>, <span style="color: #666666;">&quot;Add&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">case</span> PermissionKey.<span style="color: #0000FF;">SecurityRoleManagementModify</span><span style="color: #008000;">:</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span><span style="color: #666666;">&quot;Security&quot;</span>, <span style="color: #666666;">&quot;RoleManagement&quot;</span>, <span style="color: #666666;">&quot;Modify&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">case</span> PermissionKey.<span style="color: #0000FF;">SecurityUserManagementDelete</span><span style="color: #008000;">:</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span><span style="color: #666666;">&quot;Security&quot;</span>, <span style="color: #666666;">&quot;UserManagement&quot;</span>, <span style="color: #666666;">&quot;Delete&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">case</span> PermissionKey.<span style="color: #0000FF;">SecurityPermissionManagementAdd</span><span style="color: #008000;">:</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span><span style="color: #666666;">&quot;Security&quot;</span>, <span style="color: #666666;">&quot;PermissionManagement&quot;</span>, <span style="color: #666666;">&quot;Add&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span></pre></div></div>

<p>I needed to add a couple of branches to this, but I also wanted to tidy up the code by removing the superfluous braces, as a precursor to converting it into something a bit more robust and maintainable. I wanted unit test coverage to give me confidence that I hadn&#8217;t mucked up some logic and inadvertantly granted admin access to the helpdesk trainee role or something.</p>
<p>So, I copied the entire switch body into Notepad++ (well, vim really, but I&#8217;ll pretend it&#8217;s Notepad++ for the sake of making this post a bit more accessible) and set to work<sup>[1]</sup>.</p>
<p>Before recording my macro, I needed to do a bit of preprocessing to trim the code down to just the data I wanted to work with. The following steps show the &#8216;find&#8217; regexes I used (in each case, the value of the replace field was empty, so these are effectively deletes), and the effect on the first switch branch from the list above:</p>
<p>1) Remove opening and closing braces from every switch branch:</p>
<pre>^\s+[\{\}]$</pre>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF;">case</span> PermissionKey.<span style="color: #0000FF;">SecurityParameterManagementAdd</span><span style="color: #008000;">:</span>
&nbsp;
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span><span style="color: #666666;">&quot;Security&quot;</span>, <span style="color: #666666;">&quot;ParameterManagement&quot;</span>, <span style="color: #666666;">&quot;Add&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span></pre></div></div>

<p>2) Remove blanks &#8211; TextFX/Edit/Delete Blank Lines</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF;">case</span> PermissionKey.<span style="color: #0000FF;">SecurityParameterManagementAdd</span><span style="color: #008000;">:</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span><span style="color: #666666;">&quot;Security&quot;</span>, <span style="color: #666666;">&quot;ParameterManagement&quot;</span>, <span style="color: #666666;">&quot;Add&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span></pre></div></div>

<p>3) Remove case statements and leading whitespace:</p>
<pre>^\s+case\s+</pre>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">PermissionKey.<span style="color: #0000FF;">SecurityParameterManagementAdd</span><span style="color: #008000;">:</span>
PermissionKey.<span style="color: #0000FF;">SecurityParameterManagementAdd</span><span style="color: #008000;">:</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span><span style="color: #666666;">&quot;Security&quot;</span>, <span style="color: #666666;">&quot;ParameterManagement&quot;</span>, <span style="color: #666666;">&quot;Add&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span></pre></div></div>

<p>4) Remove colon from end of case statement:</p>
<pre>:$</pre>
<p>5) Remove return statement and leading whitespace:</p>
<pre>^\s+return new string\[\]\s*</pre>
<p>I ended up with a sequence of couplets looking similar to this one:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">PermissionKey.<span style="color: #0000FF;">SecurityParameterManagementAdd</span>
<span style="color: #000000;">&#123;</span><span style="color: #666666;">&quot;Security&quot;</span>, <span style="color: #666666;">&quot;ParameterManagement&quot;</span>, <span style="color: #666666;">&quot;Add&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Now the fun starts &#8211; lets walk through the process.</p>
<p>We want to convert the first couplet into a simple unit test fixture, and record the process. This will be our macro &#8211; the instructions for converting one couplet into one unit test. We can then play the macro multiple times to convert all the others effortlessly.</p>
<p>Start by moving the cursor to the start of the line, before the &#8216;P&#8217; of PermissionKey. This is the start point of the macro, so for the macro to be repeatable we must make sure that we finish recording the macro in perfect position to run it again, i.e. before the &#8216;P&#8217; of PermissionKey for the next couplet (column 0 line 3). Hit Ctrl-Shift-R to start recording.</p>
<p>It is important not to use the mouse when editing &#8211; stick to the keyboard. It&#8217;s also important not to record keystrokes that are too specific to one bit of code. For instance, don&#8217;t use the arrow keys to move left and right character-by-character, because it won&#8217;t work on longer or shorter lines.</p>
<p>Instead, use the Home and End keys to jump to the start or end of the line, and hold Ctrl whilst arrowing left or right to move a word at a time instead of a character at a time (this is one of the areas where vim&#8217;s movement commands really differentiate it from wannabes like Notepad++&#8230;but I digress). See the &#8216;Detailed Instructions&#8217; section below for more information.</p>
<p>Assume the original switch body is in a method called &#8216;LookupEnumPermission&#8217;. The couplet should be edited to look like this (without the linewrap&#8230;):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> TestSecurityParameterManagementAdd<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> result <span style="color: #008000;">=</span> LookupEnumPermission<span style="color: #000000;">&#40;</span>
            PermissionKey.<span style="color: #0000FF;">SecurityParameterManagementAdd</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Security&quot;</span>, result<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ParameterManagement&quot;</span>, result<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Add&quot;</span>, result<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Make sure you finish by moving the cursor into position for the next couplet, and hit Ctrl-Shift-R again to stop recording.</p>
<p>Now, hit Ctrl-Shift-P to play back the macro. If you&#8217;ve done everything right, the next couplet should magically format itself into a unit test. Hit Ctrl-Shift-P again, and the next couplet will change too. Under the Macro menu, select &#8216;Run a macro multiple times&#8230;&#8217; and you can enter a fixed number of iterations, or just apply the macro over and over again until the end of the file is reached.</p>
<p>Finally, you can copy the unit tests into a new or existing test fixture, and you&#8217;re done! In much less time (hopefully) and with fewer errors than if the tests had been written one-by-one.</p>
<p><em><strong>Detailed Instructions:</strong></em></p>
<p>These are ley-by-key instructions in Notepad++, in case something in the description above is unclear. Visual Studio should be similar. Vim will be faster once you&#8217;ve learned how, but I&#8217;ll assume if you use vim you&#8217;re already au fait with this sort of editing <img src='http://basildoncoder.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<ol>
<li>Type [Test], and hit enter to start a new line.</li>
<li> Type &#8216;public void Test&#8217; and hit Enter.</li>
<li> Type &#8216;{&#8216; and hit Enter, then Tab.</li>
<li> Hold Ctrl and tap the right arrow twice to jump over a couple of words and place the cursor at the start of the word SecurityParameterManagementAdd, then hold Ctrl-Shift and right arrow again to select the word. Ctrl-C to copy, then arrow up two lines and paste it after the word &#8216;Test&#8217; to create the full function name TestSecurityParameterManagementAdd. Type () for the empty parameter list.</li>
<li> Arrow down two lines and hit Home to jump to the start of the line. Type &#8216;string[] result = LookupEnumPermission(&#8216;, then hit End to jump to the end of the line and type &#8216;);&#8217;.</li>
<li> Arrow down one line, hit Home, then Tab. Type &#8216;Assert.Equals(&#8216; then hit Delete to remove the &#8216;{&#8216;. Hold Ctrl and move right three times (to move the cursor just past the comma) and type &#8216;result[0]);&#8217; and hit Enter.</li>
<li> Repeat variations of step 7 a couple of times to convert the next two lines. Remember to use the correct indexes (result[1] and result[2]). Hit Enter after the last line and type &#8216;}&#8217; to close the function body.</li>
<li> Arrow down one line and hit Home to place the cursor at the correct start position for the next couplet, and end the macro by hitting Ctrl-Shift-R again.</li>
</ol>
<p><sup>[1]</sup>I could have just done this in a new file in Visual Studio, but for some reason I find VS intolerably slow at running macros once recorded. So slow, in fact, that you can watch the cursor laboriously complete each step &#8211; I wind up thinking it would have been quicker to do it manually. That might just be something odd about my VS installation though, as no-one else seems to think it&#8217;s slow.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://basildoncoder.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://basildoncoder.com/blog/2009/02/06/macros-you-oughta-know/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://basildoncoder.com/blog/2009/02/06/macros-you-oughta-know/</feedburner:origLink></item>
		<item>
		<title>OOD – The Second Coming?</title>
		<link>http://feedproxy.google.com/~r/BasildonCoder/~3/wIMGvX-qNk4/</link>
		<comments>http://basildoncoder.com/blog/2009/01/16/ood-the-second-coming/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 16:00:39 +0000</pubDate>
		<dc:creator>russ</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://basildoncoder.com/blog/2009/01/16/ood-the-second-coming/</guid>
		<description><![CDATA[Over the last couple of months I&#8217;ve been burning up my free time on a pet project (hence the scarcity of posting here). This particular project is a web application, and since I&#8217;ve always been a desktop or middle-tier dude in my day job, it&#8217;s a bit of a step out of my normal environment [...]]]></description>
			<content:encoded><![CDATA[<p>Over the last couple of months I&#8217;ve been burning up my free time on a pet project (hence the scarcity of posting here). This particular project is a web application, and since I&#8217;ve always been a desktop or middle-tier dude in my day job, it&#8217;s a bit of a step out of my normal environment to grapple with browser compatibility and suchlike.</p>
<p>Still, I wanted it to be a decent learning experience, so after a brief dalliance with Rails I scrapped the idea of using any <a href="http://basildoncoder.com/blog/2007/12/09/coding-by-convention/">framework sorcery</a> and decided to write everything in plain ol&#8217; <a href="http://php.net/">PHP</a>, and lean heavily on <a href="http://developer.yahoo.com/yui/">YUI</a> and <a href="http://jquery.com/">jQuery</a> to sort out the browser stuff.</p>
<p>This probably isn&#8217;t an approach I&#8217;d use in future projects, but I bet I&#8217;ll appreciate those frameworks a lot more once I&#8217;ve encountered and understood the problems they attempt to solve.</p>
<p>So, having strayed from the comfort of <a href="http://rubyonrails.org/">Rails</a> and its clones, I had to think about lots of things like security, validation, data access, and how to organise my code. Just because I&#8217;d abandoned the training wheels I had no intention of falling over all the time &#8211; I still wanted a nice, maintainable app with sensible  abstractions, properly decoupled, and resilient to failure. Time to start reading articles and the odd open source project, obviously.</p>
<p>It&#8217;s at this point I noticed something interesting. Since I regularly read plenty of development websites it could scarcely have escaped my notice that the trendy framework players (e.g. Rails, Django, Cake, ASP.NET MVC) strongly advocate the <a href="http://en.wikipedia.org/wiki/Model-view-controller">MVC</a> pattern and <a href="http://en.wikipedia.org/wiki/Class-based_programming">class-based object-oriented design</a>. What I hadn&#8217;t really realised until now is how endemic that viewpoint had become.</p>
<p>In fact, beyond a few admirably out-there frameworks like <a href="http://www.seaside.st/">Seaside</a>, it&#8217;s almost universal. OOD = good, EVERYTHING ELSE = bad. MVC = good, EVERYTHING ELSE = bad. No shades of grey, no room for dissenting opinion.</p>
<p>Go anywhere where best-practices are discussed and mention you&#8217;re writing some procedural code, and watch the fireworks. It doesn&#8217;t matter if your application has fewer lines of code than a newly-created Rails app has source files &#8211; if you haven&#8217;t structured it with models, views, and controllers you may as well have written it in Visual Basic for all the bile you&#8217;re going to have thrown at you.</p>
<p>If you say you&#8217;re writing functional code, you might get away with it, since functional programming is still widely misunderstood and you&#8217;ll likely be classified as some weird LISPer or Schemer doing something arcane and thus ignored.</p>
<p>Ironically, of course, if you grab a random Rails/Django/Cake app from <a href="http://github.com/">github</a> or <a href="http://code.google.com/hosting/">Google Code</a>, there&#8217;s a pretty fair chance that what you&#8217;ll find isn&#8217;t particularly object-oriented anyway. Hint &#8211; usage of the &#8216;class&#8217; keyword does not an object-oriented design make. And sweet zombie Jesus, I&#8217;ve never seen such abuse of the singleton pattern. That&#8217;s a sure sign someone doesn&#8217;t &#8216;get&#8217; OO &#8211; the <a href="http://c2.com/cgi/wiki?SingletonsAreEvil">singleton pattern is evil</a> and basically a way to <a href="http://steve.yegge.googlepages.com/singleton-considered-stupid">shoehorn globals into an application</a> without admitting it to your friends.</p>
<p>So, we have massive fanatical advocacy of a technique that will allegedly solve all your problems, coupled with large-scale real-world misunderstandings and misapplication. Does this remind anyone of anything? Say, for example, the last time OOD swept the world, panacea to all programming woes, about 20 or so years ago?</p>
<p>Don&#8217;t get me wrong, I&#8217;m not arguing that class-based OO itself is just a fad &#8211; it deserves its place as a paradigm alongside procedural, functional, parallel, and numerous others. It&#8217;s the heralding of OO as the one true way that seems faddish.</p>
<p>So, in the very best &#8220;bah, humbug&#8221; traditions I&#8217;ve written my app in unashamedly procedural PHP code. I don&#8217;t mix my presentation and content. My data layer is decoupled and unit tested. Every bit of SQL is a parameterised query, to guard against injection. I don&#8217;t have a single echo() statement containing any html tags. All my errors are exception based, and I don&#8217;t have a single die() call anywhere. My average function size is about 10 lines, and my longest is about 20. I&#8217;ve no doubt that there&#8217;s a legion of 15-year-old self-appointed geniuses ready to accuse me of inflicting yet more spaghetti code junk on the world just because &#8220;find . -iname &#8216;*php&#8217; | xargs grep class&#8221; comes up empty, but hey I&#8217;m OK with that.</p>
<p>I&#8217;m writing my next app in <a href="http://en.wikipedia.org/wiki/Brainfuck">brainf*ck</a> using ed.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://basildoncoder.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://basildoncoder.com/blog/2009/01/16/ood-the-second-coming/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://basildoncoder.com/blog/2009/01/16/ood-the-second-coming/</feedburner:origLink></item>
		<item>
		<title>Project Euler Problem 7</title>
		<link>http://feedproxy.google.com/~r/BasildonCoder/~3/mGVoTsT9PEY/</link>
		<comments>http://basildoncoder.com/blog/2008/10/26/project-euler-problem-7/#comments</comments>
		<pubDate>Sun, 26 Oct 2008 21:58:14 +0000</pubDate>
		<dc:creator>russ</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://basildoncoder.com/blog/2008/10/26/project-euler-problem-7/</guid>
		<description><![CDATA[Problem 7 By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10001st prime number? Ah, what a nice, straightforward, unambiguous spec! If only business software specifications were so precise. Way back in problem 3, I took a bit [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong><a href="http://projecteuler.net/index.php?section=problems&amp;id=7">Problem 7</a></strong></em></p>
<blockquote><p>By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6<sup>th</sup> prime is 13.</p>
<p>What is the 10001<sup>st</sup> prime number?</p></blockquote>
<p>Ah, what a nice, straightforward, unambiguous spec! If only business software specifications were so precise.</p>
<p><a href="http://basildoncoder.com/blog/2008/04/07/project-euler-problem-3/">Way back in problem 3</a>, I took a bit of a wander off-topic and built a prime generator in .Net using the <a href="http://en.wikipedia.org/wiki/Sieve_of_eratosthenes">Sieve of Eratosthenes</a>. Armed with this, problem 7 should be easy, right? The sieve implementation generates an IEnumerable<long>, which is non-indexable (i.e. I can&#8217;t just say Primes()[10001]), but I can take the first 10,001 and then ask for the last element, which will be the answer to the problem.<br />
</long></p>
<p>There&#8217;s a problem with this, however. The sieve requires an upper bound during initialisation. This means it&#8217;s great for solving problems like &#8220;generate all the primes less than 10,001&#8243;, but not so great at answering questions like &#8220;what is the 10,001st prime number?&#8221;, since it requires foreknowledge of the upper bound.</p>
<p>To illustrate the problem, I&#8217;ll take a wild guess at the upper bound. I&#8217;m going to guess that the 10,001st prime number is less than 99,999. What happens?</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var sieve <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SieveOfEratosthenes<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">99999</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
var result <span style="color: #008000;">=</span> sieve.<span style="color: #0000FF;">Primes</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Take</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">10001</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Last</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>This generates an answer of 99,991. If I enter this into the Project Euler website, however, it tells me the answer is wrong. Gah! What went wrong? A simple test reveals the problem:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var sieve <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SieveOfEratosthenes<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">99999</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
var primes <span style="color: #008000;">=</span> sieve.<span style="color: #0000FF;">Primes</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Take</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">10001</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
var count <span style="color: #008000;">=</span> primes.<span style="color: #0000FF;">Count</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>There&#8217;s only 9,592 primes generated! As the <a href="http://msdn.microsoft.com/en-us/library/bb503062.aspx">docs for Take()</a> state (emphasis mine):</p>
<blockquote><p>Take&lt;TSource&gt;<tsource> enumerates source and yields elements until count elements have been yielded <em>or source contains no more elements</em>.</tsource></p></blockquote>
<p>Damn. So, looks like my 99,999 guess was too small &#8211; with that as an upper bound, the sieve only finds 9,592 primes, and I need the 10,001st. OK, I&#8217;ll bump it up by an order of magnitude:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var sieve <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SieveOfEratosthenes<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">999999</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
var result <span style="color: #008000;">=</span> sieve.<span style="color: #0000FF;">Primes</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Take</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">10001</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Last</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>This gives me the correct answer. Not exactly a wonderful solution though; the idea of having to guess the upper bound is pretty horrendous, and if this was real code it wouldn&#8217;t be particularly maintainable &#8211; what if the requirements changed and we had to find the <em>n</em>th prime, which happened to be &gt;99,999? We&#8217;d have to guess again. Ugh.</p>
<p>Worse, the sieve algorithm precomputes all the primes up to the specified upper bound, meaning that in the above approach I&#8217;ve asked the sieve to generate primes up to 999,999 (all 78,498 of them!) despite only needing 10,001. Not very efficient.</p>
<p>Fortunately, the upper bound can be calculated separately. <a href="http://primes.utm.edu/howmany.shtml" aiotarget="false" aiotitle="Where n &gt;8601, as in this case, we can use the following equation">Where <em aiotitle="n">n</em>&gt;8601, as in this case, we can use the following equation</a>:</p>
<pre>p(<em>n</em>) <u>&lt;</u> <em>n</em> (log<sub>e</sub> <em>n</em> + log<sub>e</sub> log<sub>e</sub> <em>n</em> - 0.9427)</pre>
<p>where p(<em>n</em>) is the <em>n</em>th prime number.</p>
<p>Alternatively, for flexibility in handling <em>n</em>&lt;8601, we can use the less accurate</p>
<pre>p(<em>n</em>) &lt; <em>n</em> log<sub>e</sub> log<sub>e</sub> <em>n</em></pre>
<p><a href="http://en.wikipedia.org/wiki/Prime_number_theorem#Approximations_for_the_nth_prime_number">which works for <em>n</em>&gt;5</a>. We can easily precompute the answers for <em>n</em>&lt;=5, or simply calculate on demand.</p>
<p>The formula can be implemented on the sieve class, with a factory method to help when we want to use it:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> SieveOfEratosthenes
    CreateSieveWithAtLeastNPrimes<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> n<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> SieveOfEratosthenes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">long</span><span style="color: #000000;">&#41;</span>
            Math.<span style="color: #0000FF;">Ceiling</span><span style="color: #000000;">&#40;</span>UpperBoundEstimate<span style="color: #000000;">&#40;</span>n<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">double</span> UpperBoundEstimate<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> n<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> n <span style="color: #008000;">*</span> Ln<span style="color: #000000;">&#40;</span>n<span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> n <span style="color: #008000;">*</span> <span style="color: #000000;">&#40;</span>Ln<span style="color: #000000;">&#40;</span>Ln<span style="color: #000000;">&#40;</span>n<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">double</span> Ln<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">double</span> n<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> Math.<span style="color: #0000FF;">Log</span><span style="color: #000000;">&#40;</span>n, Math.<span style="color: #0000FF;">E</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This leaves us with an overall solution like so:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var sieve <span style="color: #008000;">=</span> SieveOfEratosthenes.<span style="color: #0000FF;">CreateSieveWithAtLeastNPrimes</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">10001</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
var result <span style="color: #008000;">=</span> sieve.<span style="color: #0000FF;">Primes</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Take</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">10001</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Last</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>This generates a total 10,018 primes, cutting the wasted effort from almost 70,000 superfluous primes to just 17, and takes around 20ms to execute on my machine. Plenty fast enough, I think.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://basildoncoder.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://basildoncoder.com/blog/2008/10/26/project-euler-problem-7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://basildoncoder.com/blog/2008/10/26/project-euler-problem-7/</feedburner:origLink></item>
		<item>
		<title>Look Before You Look Before You Leap</title>
		<link>http://feedproxy.google.com/~r/BasildonCoder/~3/OC-zubakPDA/</link>
		<comments>http://basildoncoder.com/blog/2008/10/24/look-before-you-look-before-you-leap/#comments</comments>
		<pubDate>Fri, 24 Oct 2008 14:32:36 +0000</pubDate>
		<dc:creator>russ</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://basildoncoder.com/blog/2008/10/24/look-before-you-look-before-you-leap/</guid>
		<description><![CDATA[Generally, I try to avoid turning this blog into some sort of snark-fest about other programmers or blogs. I&#8217;ve disagreed with Jeff Atwood once or twice though, and so by posting this I&#8217;m probably straying a little close to the edge&#8230;but what the hell. A couple of days ago Coding Horror carried a fluff piece [...]]]></description>
			<content:encoded><![CDATA[<p>Generally, I try to avoid turning this blog into some sort of <a href="http://www.google.co.uk/search?q=define%3Asnark">snark</a>-fest about other programmers or blogs. I&#8217;ve disagreed with Jeff Atwood <a href="http://basildoncoder.com/blog/2008/02/22/code-can-be-beautiful/">once</a> or <a href="http://basildoncoder.com/blog/2008/01/29/freedom-zero-the-all-or-nothing-fallacy/">twice</a> though, and so by posting this I&#8217;m probably straying a little close to the edge&#8230;but what the hell.</p>
<p>A couple of days ago Coding Horror carried a <a href="http://www.codinghorror.com/blog/archives/001177.html">fluff piece</a> about how all developers should be marketers too. Predictably, the article soon got posted to <a href="http://www.reddit.com/r/programming/">proggit</a> where it was <a href="http://www.reddit.com/r/programming/comments/78vq3/jeff_atwood_finally_jumps_the_shark/">ripped on by reddit&#8217;s resident Jeff-haters</a>, and even more predictably the comments were a mix of interesting insight and barely-concealed hate.</p>
<p>Apparently some of them got up Jeff&#8217;s nose a bit, and today he <a href="http://www.codinghorror.com/blog/archives/001178.html">responded</a>. The core of his rebuttal seems to be that you shouldn&#8217;t trust what you read on blogs, and should verify everything yourself. True enough, I guess, if perhaps a bit impractical given the sheer amount of information out there.</p>
<p>Then, however, Jeff goes on to give an example by referencing a <a href="http://blog.madskristensen.dk/post/Compression-and-performance-GZip-vs-Deflate.aspx">compression benchmark</a> he&#8217;d read on a blog and providing counter-analysis to show that the benchmark was wrong in claiming Deflate is faster than gzip. In doing so, much knowledge was gained.</p>
<p>Or so we are told.</p>
<p>The comment thread quickly becomes a goldmine of humour. Bugs in Jeff&#8217;s benchmarking code (not resetting the stopwatch) meant that the durations were cumulative, not independent, with inevitable distortion of the results. Another commenter pointed out that <a href="http://en.wikipedia.org/wiki/Gzip">gzip</a> cannot possibly be faster than Deflate, since the gzip algorithm IS the Deflate algorithm plus some additional computation.</p>
<blockquote><p>“gzip” is often also used to refer to the gzip file format, which is:</p>
<ul>
<li>a 10-byte header, containing a magic number, a version number and a timestamp</li>
<li>optional extra headers, such as the original file name,</li>
<li>a body, containing a DEFLATE-compressed payload</li>
<li>an 8-byte footer, containing a CRC-32 checksum and the length of the original uncompressed data</li>
</ul>
<p align="right"><a href="http://en.wikipedia.org/wiki/Gzip">http://en.wikipedia.org/wiki/Gzip</a></p>
</blockquote>
<p>With the benchmarking code fixed, we see that Deflate is indeed slightly faster than gzip.</p>
<p>All of which leads to repeated quotations from Jeff about the community being smarter than him, and some drastic toning down of language in post-publication edits to the article. I read a cached version of the RSS feed, which is markedly different to the article currently live on codinghorror.com &#8211; &#8220;on my box, GZip is twice as fast as Deflate&#8221; becomes &#8220;on my box, GZip is just as fast as Deflate&#8221;, &#8220;Deflate is way slower. It&#8217;s not even close&#8221; becomes &#8220;Deflate is nowhere near 40% faster&#8221;, etc.</p>
<p><img src="/images/codinghorror01.png" align="middle" /></p>
<p>Anyone who&#8217;s tackled a major performance problem will likely agree that profiling is a tremendously valuable technique that should always be applied before attempting to optimise (i.e. look before you leap). I think this little episode has highlighted a couple of important things to bear in mind, however:</p>
<ol>
<li>Profiling isn&#8217;t a magic wand &#8211; if you use buggy profiling code, you are leading yourself up the garden path.</li>
<li>Profiling is less useful when you can reason (in the mathematical sense) about the code. That involves <em>understanding the algorithms you are dealing with</em>. Gzip is Deflate plus a bit more processing &#8211; so unless that extra processing has a negative duration gzip must necessarily take longer. You don&#8217;t need a profiler to work that out. Look <em>before </em>you look before you leap.</li>
</ol>
<p>Anyway, enough hatcheting from me, normal service will be resumed shortly.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://basildoncoder.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://basildoncoder.com/blog/2008/10/24/look-before-you-look-before-you-leap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://basildoncoder.com/blog/2008/10/24/look-before-you-look-before-you-leap/</feedburner:origLink></item>
		<item>
		<title>Project Euler Problem 6</title>
		<link>http://feedproxy.google.com/~r/BasildonCoder/~3/slSlYitA3vA/</link>
		<comments>http://basildoncoder.com/blog/2008/08/14/project-euler-problem-6/#comments</comments>
		<pubDate>Thu, 14 Aug 2008 00:00:26 +0000</pubDate>
		<dc:creator>russ</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://basildoncoder.com/blog/2008/08/14/project-euler-problem-6/</guid>
		<description><![CDATA[Onwards to&#8230; Problem 6 The sum of the squares of the first ten natural numbers is, 12 + 22 + &#8230; + 102 = 385 The square of the sum of the first ten natural numbers is, (1 + 2 + &#8230; + 10)2 = 552 = 3025 Hence the difference between the sum of [...]]]></description>
			<content:encoded><![CDATA[<p class="problem_content">Onwards to&#8230;</p>
<p><em><strong><a href="http://projecteuler.net/index.php?section=problems&amp;id=6">Problem 6</a></strong></em></p>
<blockquote><p>The sum of the squares of the first ten natural numbers is,</p>
<p style="text-align: center">1<sup>2</sup> + 2<sup>2</sup> + &#8230; + 10<sup>2</sup> = 385</p>
<p>The square of the sum of the first ten natural numbers is,</p>
<p style="text-align: center">(1 + 2 + &#8230; + 10)<sup>2</sup> = 55<sup>2</sup> = 3025</p>
<p>Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 <img src="http://projecteuler.net/images/symbol_minus.gif" style="vertical-align: middle" border="0" width="9" height="3" /> 385 = 2640.</p>
<p>Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.</p></blockquote>
<p>Bit of a disappointment, problem 6; it&#8217;s too easy. <a href="http://projecteuler.net/index.php?section=problems&amp;sort=difficulty">It&#8217;s rated as the third-easiest</a>, i.e. easier than problems <a href="http://basildoncoder.com/blog/2008/04/07/project-euler-problem-3/">3</a>, <a href="http://basildoncoder.com/blog/2008/04/21/project-euler-problem-4/">4</a>, and <a href="http://basildoncoder.com/blog/2008/06/10/project-euler-problem-5/">5</a> which I&#8217;ve already covered. In fact, for my money it&#8217;s easier than problem <a href="http://basildoncoder.com/blog/2008/03/22/project-euler-problems-1-and-2/">2</a> as well. Ah well, the difficulty ramps up soon enough, trust me.  Here&#8217;s the very simple python solution:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">sum_sq = <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span> x<span style="color: #66cc66;">*</span>x <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">101</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
sq_sum = <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span><span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">101</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">**</span> <span style="color: #ff4500;">2</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> sq_sum - sum_sq</pre></div></div>

<p>As you can see, it&#8217;s pretty intuitive. You sum the squares, square the sum, and calculate the difference. The answer is basically in the description, you just have to scale up a little.</p>
<p>There&#8217;s not much else to say about this one. Even if I abandon the functional approach and write a straightforward imperative solution it&#8217;s still very straightforward. In (deliberately non-idiomatic, so don&#8217;t whine at me) ruby:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">sum_of_squares = <span style="color:#006666;">0</span>
sum = <span style="color:#006666;">0</span>
&nbsp;
1.<span style="color:#9900CC;">upto</span> <span style="color:#006666;">100</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span>
    sum_of_squares <span style="color:#006600; font-weight:bold;">+</span>= x <span style="color:#006600; font-weight:bold;">*</span> x
    sum <span style="color:#006600; font-weight:bold;">+</span>= x
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#006600; font-weight:bold;">&#40;</span>sum <span style="color:#006600; font-weight:bold;">*</span> sum<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">-</span> sum_of_squares</pre></div></div>

<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://basildoncoder.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://basildoncoder.com/blog/2008/08/14/project-euler-problem-6/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://basildoncoder.com/blog/2008/08/14/project-euler-problem-6/</feedburner:origLink></item>
		<item>
		<title>Magic Numbers and Other Numerical Nightmares</title>
		<link>http://feedproxy.google.com/~r/BasildonCoder/~3/9rFVlKi2itg/</link>
		<comments>http://basildoncoder.com/blog/2008/08/13/magic-numbers-and-other-numerical-nightmares/#comments</comments>
		<pubDate>Wed, 13 Aug 2008 12:48:18 +0000</pubDate>
		<dc:creator>russ</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://basildoncoder.com/blog/2008/08/13/magic-numbers-and-other-numerical-nightmares/</guid>
		<description><![CDATA[There are many coding practices that are near-universally regarded as &#8216;bad&#8217;, yet somehow keep cropping up over and over again. Conditional-branch abuse (including, yes, gotos). Deep nesting. Cryptic variable names. Global variables. Tight coupling. Entangled business/presentation logic. I could go on. Why do we keep doing it? Convenience? Laziness? Tiredness? Is unreadable spaghetti code some [...]]]></description>
			<content:encoded><![CDATA[<p>There are many coding practices that are near-universally regarded as &#8216;bad&#8217;, yet somehow keep cropping up over and over again. Conditional-branch abuse (including, yes, gotos). Deep nesting. Cryptic variable names. Global variables. Tight coupling. Entangled business/presentation logic. I could go on.</p>
<p>Why do we keep doing it? Convenience? Laziness? Tiredness? Is unreadable spaghetti code some sort of steady-state/equilibrium for code? Is it a natural consequence of the vague and squidgy limitations of our evolved monkey-brains? Or is well-designed code abhorred like a vacuum and naturally atrophies into the sort of shambles you dread seeing on your first day at a new job, unless well-intentioned and dedicated people actively work to clean and polish it, like the <a href="http://en.wikipedia.org/wiki/Forth_Railway_Bridge#Maintenance">Forth Bridge</a>?</p>
<p>I don&#8217;t have the time or wit to give this subject the treatment it deserves, but I do want to rant a bit about another symptom of this disease, which has given me a couple of sleepless nights recently. I refer, as the title might suggest, to <a href="http://en.wikipedia.org/wiki/Magic_number_(programming)">magic numbers</a>.</p>
<p>Magic numbers are constants, <a href="http://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constant">unnamed</a> in the most pathological cases, that represent an assumption or a limit in a piece of code. They often cause problems because soon they are forgotten about or their meaning is lost &#8211; and then something happens to invalidate the assumption, the code breaks, and all hell breaks loose.</p>
<p>Magic numbers, to stretch the definition a bit, can also be implicit. If you are using a 32-bit integer, your magic number is 2,147,483,647 &#8211; that&#8217;s the biggest number you can store in that type. Often, movement up to and beyond these ranges can trigger long-dormant bugs that are no fun at all to diagnose.</p>
<p>Three times in recent history I&#8217;ve been bitten by bugs of this class, triggered by auto-incrementing sequences in database. These are they:</p>
<ol>
<li>A table in a database had a 32-bit integer primary key. At the time this seemed like a perfectly reasonable default, but insanely fast growth in usage of the system meant that the ~2.1billion upper limit of that data type was quickly reached. The DB column was switched to a 64-bit integer, but some of the client applications reading that table were not identified as at-risk. When the sequence generator left the 32-bit range, those applications overflowed. This happened at 4:30pm on a Friday afternoon. Saturdays were peak-times for system usage. You can imagine the frantic hacking that ensued.</li>
<li>A sequence generator for a particular entity was started at 20,000,000, so as not to clash with the ID sequence of a related entity (that had started at 0 a good few years earlier). The similarity between the entities and the need to not have the IDs overlap had valid business justification, but the magic number was selected arbitrarily and promptly forgotten. Inevitably, the latter sequence surpassed that number, causing bizarre and difficult-to-trace entity relationship corruption that manifested as strangely-disappearing data on the front-end.</li>
<li>A stored procedure parameter was incorrectly declared as an OracleType.Float, when it should have been an OracleType.Int32. This resulted in the value being cast from an integer to a floating-point and back again. For the first 16,777,216 integers, this happens to work OK. For the value 16,777,217, however, the loss in precision means that the number changes during casting. This simple bit of (heavily contrived) code shows the problem:

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">17000000</span><span style="color: #008000;">;</span> <span style="color: #008000;">++</span>i<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>i <span style="color: #008000;">!=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">float</span><span style="color: #000000;">&#41;</span>i<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;{0} != {1}&quot;</span>, i, <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">float</span><span style="color: #000000;">&#41;</span>i<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            break<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>There are many numbers above 16,777,217 that have this characteristic; 16,777,217 just happens to be the first, for reasons you can probably divine if you think the IEEE floating-point spec is a riveting read. A couple of weeks after the launch of a fairly major internal application, this time-bomb exploded due to a sequence reaching the magic number. The bug was nothing to do with the new application, but of course fingers were pointed at it since a long-running and stable system had mysteriously choked very shortly after deployment of the new application.</li>
</ol>
<p>Now, unquestionably, all these problems are avoidable, and a strong argument could be made that none of them should ever have been allowed to happen. Yet, for many reasons, they do. For example, first-mover advantage can mean the opportunity cost of taking the time to do things right first time is greater than the cost of fixing problems later.</p>
<p>Also, people make assumptions. The issue underlying the <a href="http://en.wikipedia.org/wiki/Millennium_bug">Millennium Bug</a> hysteria was caused by well-meaning developers who knew that two-digit dates wouldn&#8217;t work after 1999 (effectively another magic number), but assumed the software would have been replaced or upgraded by then. No doubt that seemed a totally reasonable assumption in the 1970s, and it had genuine technical benefits (storage space was so tight that every byte saved was a battle won).</p>
<p>Anyway, I don&#8217;t have a magic bullet solution for this, I&#8217;m just venting spleen. Unit tests can help, but won&#8217;t magically eliminate this class of bug (no matter what some of the more extreme TDD fanatics might tell you), so I suppose the lesson to take from this is the importance of being able to recognise and diagnose potential magic number issues. Pay close attention to data types, type conversions, and current values of sequences in your database. Keeping a sacrifical goat on hand might pay dividends too, in case any blood-thirsty deities with a head for binary arithmetic are watching.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://basildoncoder.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://basildoncoder.com/blog/2008/08/13/magic-numbers-and-other-numerical-nightmares/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://basildoncoder.com/blog/2008/08/13/magic-numbers-and-other-numerical-nightmares/</feedburner:origLink></item>
		<item>
		<title>Ubuntu, Xmonad, and an Ode to Apt</title>
		<link>http://feedproxy.google.com/~r/BasildonCoder/~3/wxNjwd_aF8s/</link>
		<comments>http://basildoncoder.com/blog/2008/08/10/ubuntu-xmonad-and-an-ode-to-apt/#comments</comments>
		<pubDate>Sun, 10 Aug 2008 22:34:42 +0000</pubDate>
		<dc:creator>russ</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://basildoncoder.com/blog/2008/08/10/ubuntu-xmonad-and-an-ode-to-apt/</guid>
		<description><![CDATA[This weekend I finally got around to updating my main Linux box from Ubuntu 7.10 to 8.04 (yes, I know, 4 months late &#8211; but moving fast!). The highly excellent xmonad has made it into the main Ubuntu repositories, so I discarded my own build and grabbed the packaged version &#8211; which promptly didn&#8217;t work [...]]]></description>
			<content:encoded><![CDATA[<p>This weekend I finally got around to updating my main Linux box from Ubuntu 7.10 to 8.04 (yes, I know, 4 months late &#8211; but moving fast!). The highly excellent <a href="http://xmonad.org/">xmonad</a> has made it into the main Ubuntu repositories, so I discarded my own build and grabbed the packaged version &#8211; which promptly didn&#8217;t work as expected on my dual-head setup. Gah.</p>
<p><a href="https://bugs.launchpad.net/debian/+source/haskell-x11/+bug/203594">A bit of googling suggested</a> that the problem lay with the upstream debian package, which contained a build of libghc6-x11-dev that was compiled without xinerama support. This left me with a choice of either waiting for the package to get sorted out, or to do the build myself again. I decided to do my own build rather than live without xmonad, but rather than mucking about with tarballs I could at least now get the source from the package repository.</p>
<p>The appropriate steps, for anyone interested or having the same problem, are:</p>
<ol>
<li>Make sure libxinerama-dev is installed</li>
<li>Recompile libghc6-x11-dev and install it</li>
<li>Recompile libghc6-xmonad-dev and libghc-xmonad-contrib-dev against the new X11 lib</li>
</ol>
<p>The apt-get incantations are:</p>
<pre>
sudo apt-get install libxinerama-dev
<span class="Statement">cd</span> /tmp
sudo apt-get source <span class="Special">--compile</span> libghc6-x11-dev
sudo dpkg <span class="Special">-i</span> libghc6-x11-dev_1.<span class="Constant">4</span>.<span class="Constant">1</span>-1_i386.deb
sudo apt-get build-dep libghc6-xmonad-dev
sudo apt-get source <span class="Special">--compile</span> libghc6-xmonad-dev
sudo dpkg <span class="Special">-i</span> libghc6-xmonad-dev
sudo apt-get build-dep libghc6-xmonad-contrib-dev
sudo apt-get source <span class="Special">--compile</span> libghc6-xmonad-contrib-dev
sudo dpkg <span class="Special">-i</span> libghc6-xmonad-contrib-dev_0.<span class="Constant">6</span>-4_i386.deb</pre>
<p>A quick alt-q restart, and all is well.</p>
<p>I only mention all this because it&#8217;s so easy in this day and age to take something like apt for granted, and every so often it&#8217;s worth taking a moment to appreciate just how spectacularly good it really is. Where I work, deployments are an endless source of headaches and grief, yet the complexity of those deployments absolutely pales against the task of updating literally millions of systems, all slightly different to each other, thousands of times a day. It&#8217;s just a joy to be able to say to apt &#8220;hey, go get me everything I need to build package x, then build package x, then install it for me. And get it right first time!&#8221;.</p>
<p>In most cases, it does just that. It&#8217;s an astonishing piece of software.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://basildoncoder.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://basildoncoder.com/blog/2008/08/10/ubuntu-xmonad-and-an-ode-to-apt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://basildoncoder.com/blog/2008/08/10/ubuntu-xmonad-and-an-ode-to-apt/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic Page Served (once) in 1.022 seconds -->
