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

<channel>
	<title>Daniel Stutzbach &#187; programming</title>
	<atom:link href="http://www.barsoom.org/tag/programming/feed" rel="self" type="application/rss+xml" />
	<link>http://www.barsoom.org</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Wed, 14 Jul 2010 23:19:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A Scriptable Equity Calculator</title>
		<link>http://www.barsoom.org/scriptable-equity-calculator</link>
		<comments>http://www.barsoom.org/scriptable-equity-calculator#comments</comments>
		<pubDate>Thu, 24 Jun 2010 20:09:42 +0000</pubDate>
		<dc:creator>Daniel Stutzbach</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[poker]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.barsoom.org/?p=891</guid>
		<description><![CDATA[<p>For all of your programming poker players out there, yesterday I made the <a href="http://pokersleuth.com/">Poker Sleuth</a> <a href="http://pokersleuth.com/poker-equity-calculator.shtml">Equity Calculator</a> scriptable with <a href="http://pokersleuth.com/programmable-poker-calculator.shtml">bindings for C/C++, C#, and Python.</a></p>
<p>If you have easy_install or pip set up for Python, you can just do &#8220;easy_install pokersleuth&#8221;.  It requires a 32-bit version of Python for Windows.</p>
<p>As an example, <p style="font-family: serif;">Continue reading &#8220;<a href="http://www.barsoom.org/scriptable-equity-calculator">A Scriptable Equity Calculator</a>&#8221;</p>]]></description>
			<content:encoded><![CDATA[<p>For all of your programming poker players out there, yesterday I made the <a href="http://pokersleuth.com/">Poker Sleuth</a> <a href="http://pokersleuth.com/poker-equity-calculator.shtml">Equity Calculator</a> scriptable with <a href="http://pokersleuth.com/programmable-poker-calculator.shtml">bindings for C/C++, C#, and Python.</a></p>
<p>If you have easy_install or pip set up for Python, you can just do &ldquo;easy_install pokersleuth&rdquo;.  It requires a 32-bit version of Python for Windows.</p>
<p>As an example, to compute the odds of winning when you have a pair of Jacks, your opponent has the Ace and 5 of diamonds, and the board cards are the 3 of diamonds, 5 of clubs, and 9 of diamonds:</p>
<p><code>>>> import pokersleuth<br />
>>> pokersleuth.calculate_equity('3d5c9d', ('JJ', 'Ad5d')<br />
[0.48225, 0.51775]</code></p>
<p>The module can also be used directly from the command line:</p>
<p><code>C:\>python -m pokersleuth 3d5c9d JJ Ad5d<br />
[0.48225, 0.51775]</code></p>
<p>It supports up to 10 players and a quite extensive syntax for specifying hand ranges.  Accuracy is within one percentage point with at least 99.5% probability.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.barsoom.org/scriptable-equity-calculator/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DefaultValue Attribute as Initial Value</title>
		<link>http://www.barsoom.org/defaultvalue-attribute-initial</link>
		<comments>http://www.barsoom.org/defaultvalue-attribute-initial#comments</comments>
		<pubDate>Tue, 14 Jul 2009 22:03:24 +0000</pubDate>
		<dc:creator>Daniel Stutzbach</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.barsoom.org/?p=720</guid>
		<description><![CDATA[




<img align="right" alt="Visual Studio Properties" class="size-full wp-image-729 align-right" src="http://c0348292.cdn.cloudfiles.rackspacecloud.com/wp-content/uploads/2009/07/visual_studio_properties.png" style="width: 323px; height: 105px;" />
<p>C# lets the programming define a default value on a class property, so that GUI widgets can test the property to see if it still has the default value.  Visual Studio, for example, bolds any properties that have a non-default value, <p style="font-family: serif;">Continue reading &#8220;<a href="http://www.barsoom.org/defaultvalue-attribute-initial">DefaultValue Attribute as Initial Value</a>&#8221;</p>]]></description>
			<content:encoded><![CDATA[



<!-- -*- mode: rst -*- -->
<img align="right" alt="Visual Studio Properties" class="size-full wp-image-729 align-right" src="http://c0348292.cdn.cloudfiles.rackspacecloud.com/wp-content/uploads/2009/07/visual_studio_properties.png" style="width: 323px; height: 105px;" />
<p>C# lets the programming define a default value on a class property, so that GUI widgets can test the property to see if it still has the default value.  Visual Studio, for example, bolds any properties that have a non-default value, making them easy to pick out.</p>
<p>Unfortunately, the default value isn't used to actually initialize the property.  The initialization must be done elsewhere in the code, typically in the class's constructor.  Having to type the same value in two separate places in the code is error-prone, since it's easy to inadvertently change the value in one place and forget to change it in the other.</p>
<span id="more-720"></span>
<div class="highlight"><pre><span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Widget</span>
{
<span style="color: #7D9029">    [DefaultValue(5)]</span>
    <span style="color: #008000; font-weight: bold">public</span> <span style="color: #B00040">int</span> WidgetLength { <span style="color: #008000; font-weight: bold">get</span>; <span style="color: #008000; font-weight: bold">set</span>; }

    <span style="color: #008000; font-weight: bold">public</span> <span style="color: #0000FF">Widget</span>()
    {
         WidgetLength = <span style="color: #666666">5</span>;
    }
}
</pre></div>
<p>With only one property in the class, the chances of an error are small.  However, when you have dozens of properties and the constructor is hundreds of lines away from the DefaultValue, mismatches are bound to happen.  Furthermore, <a class="reference external" href="http://stackoverflow.com/questions/40730/how-do-you-give-a-c-auto-property-a-default-value/40734#40734">so</a> <a class="reference external" href="http://www.eggheadcafe.com/software/aspnet/32319410/defaultvalue-attribute-on.aspx">many</a> <a class="reference external" href="http://jalpesh.blogspot.com/2007/03/how-to-set-default-value-of-property-in.html">people</a> assume that setting the DefaultValue will initialize the property that Microsoft issued a <a class="reference external" href="http://support.microsoft.com/kb/311339">Knowledge Base bulletin</a> to clarify the matter.  (Why can't they just make the documentation more explicit?)</p>
<p>Since I hate maintaining error-prone code, I wrote a function that initializes all of an instance's properties using the DefaultValue attributes.  I can set the DefaultValue attribute on each property, call the function once in the constructor, and forget about it.</p>
<div class="highlight"><pre><span style="color: #008000; font-weight: bold">static</span> <span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">void</span> <span style="color: #0000FF">ApplyDefaultValues</span>(<span style="color: #B00040">object</span> self)
{
     <span style="color: #008000; font-weight: bold">foreach</span> (PropertyDescriptor prop <span style="color: #008000; font-weight: bold">in</span> TypeDescriptor.GetProperties(self)) {
         DefaultValueAttribute attr = prop.Attributes[<span style="color: #008000; font-weight: bold">typeof</span>(DefaultValueAttribute)] <span style="color: #008000; font-weight: bold">as</span> DefaultValueAttribute;
         <span style="color: #008000; font-weight: bold">if</span> (attr == <span style="color: #008000; font-weight: bold">null</span>) <span style="color: #008000; font-weight: bold">continue</span>;
         prop.SetValue(self, attr.Value);
     }
}
</pre></div>

]]></content:encoded>
			<wfw:commentRss>http://www.barsoom.org/defaultvalue-attribute-initial/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to debug a program</title>
		<link>http://www.barsoom.org/debug-program</link>
		<comments>http://www.barsoom.org/debug-program#comments</comments>
		<pubDate>Wed, 08 Jul 2009 21:54:27 +0000</pubDate>
		<dc:creator>Daniel Stutzbach</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.barsoom.org/?p=644</guid>
		<description><![CDATA[




<img align="right" alt="Cicada" class="size-full wp-image-672 align-right" src="http://c0348292.cdn.cloudfiles.rackspacecloud.com/wp-content/uploads/2009/07/Cicada-no.jpg" style="width: 144px; height: 125px;" />
<p>Debugging is a fundamental programming skill.  It's also one of the least fun and least glamorous tasks a programmer faces.  In order to reduce time spent debugging, I've invested time in examining my debugging methods.  Below, I've documented the techniques that <p style="font-family: serif;">Continue reading &#8220;<a href="http://www.barsoom.org/debug-program">How to debug a program</a>&#8221;</p>]]></description>
			<content:encoded><![CDATA[



<!-- -*- mode: rst -*- -->
<img align="right" alt="Cicada" class="size-full wp-image-672 align-right" src="/wp-content/uploads/2009/07/Cicada-no.jpg" style="width: 144px; height: 125px;" />
<p>Debugging is a fundamental programming skill.  It's also one of the least fun and least glamorous tasks a programmer faces.  In order to reduce time spent debugging, I've invested time in examining my debugging methods.  Below, I've documented the techniques that most reliably lead me to a solved problem.</p>
<p>I'll avoid getting into the gritty details of particular tools and instead stick to basic principles that can be applied in almost any situation.</p>
<span id="more-644"></span>
<div class="section" id="reproduce-the-problem">
<h2>Reproduce the problem</h2>
<p>The first step is to reproduce the problem.  If you cannot reliably reproduce the problem, you will not know if you have successfully fixed the problem or not.  Some bugs will initially be difficult to reproduce, such as race conditions.  Often, the problem can be flushed into the open by adding extra code that checks assumptions along the way.  In some cases, you will need to implement a verification function to walk a complex data structure and check it for consistency.  Verification checks will slow the program down, but they can easily be commented out later.</p>
</div>
<div class="section" id="question-assumptions">
<h2>Question assumptions</h2>
<p>If a program does not operate in the way you believe it should, one or more of your assumptions about the code is wrong.  Debugging is the art of identifying which of your assumptions is the culprit.  Sometimes you will feel that a particular part of the code is suspect and begin investigating it.  With luck, you're right and will quickly identify and fix the problem.  However, if you start to feel like your banging your head against a wall, you're probably relying on wrong assumptions to guide your debugging methods.  Take a step back and question.</p>
</div>
<div class="section" id="look-at-more-than-code">
<h2>Look at more than code</h2>
<p>Suppose you've implemented a moderately complex algorithm, but it produces the wrong output.  You look over the code, find what you believe to be a problem and change it.  Now the program produces different wrong output.  After you've done this a few times on the same code, you start to get the feeling that you're taking stabs in the dark.  If you find yourself changing your mind about a &quot;fix&quot; and reversing it, you know you're lost.</p>
<div class="sidebar">
<p class="first sidebar-title"><strong>What if my data structures are complicated?</strong></p>
<p class="last">If you need to write additional tools just to visualize your data structures, write additional tools to visualize your data structures.  You'll need them again the next time a bug crops up.</p>
</div>
<p>To make progress, examine the state of the machine as the algorithm executes.  Tracing through the execution with a debugger may be sufficient.  In some cases, you will be best off by implementing a function that prints the relevant state of the program and calling the function at key points in the algorithm.  By viewing the progress of the algorithm on actual data, you can pinpoint where it went wrong.</p>
<p>If you're not sure what the state of the program should be in the middle of the algorithm, step away from the computer and trace through the algorithm with pencil and paper.  You cannot fix a program without a clear vision of how it <em>should</em> work.</p>
</div>
<div class="section" id="do-a-binary-search">
<h2>Do a binary search</h2>
<p>Computer Science undergraduates are often required to implement the binary search algorithm.  The basic idea is to eliminate half of the search space with each step.  Many learn binary search as a children's game:</p>
<blockquote>
<p>Alice: I'm thinking of a number between 1 and 10.  Pick a number and I'll tell you if it's too high or too low.</p>
<p>Bob: 5</p>
<p>Alice: Too high.</p>
<p>Bob: 2</p>
<p>Alice: Too low.</p>
<p>Bob: 3</p>
<p>Alice: That's it!</p>
</blockquote>
<p>Alice could be thinking of a number between 1 and 100,000,000 and we'd still be able to find her number quickly.  That's the beauty of binary search.  It's also a wonderful algorithm for debugging.  The key is to write a verification function that determines if the problem has occurred.  When you insert the function into the program and reproduce the problem, you're essentially asking, &quot;Did the problem occur before or after this line?&quot;  In some cases, in lieu of verification checks you can simply comment out a chunk of the code.</p>
<blockquote>
<p>Computer: I have a bug on a line between 1 and 10,000.  Insert your verification function, and I'll tell you if the bug is before or after it.</p>
<p>You: Line 5,000</p>
<p>Computer: The bug is before.</p>
<p>You: Line 2,500</p>
<p>Computer: The bug is after.</p>
<p>...</p>
</blockquote>
<p>Virtually any reproducible bug can be pinned down by the binary search method.</p>
<p>Often you will have an intuitive sense for where the bug might be and won't need a binary search.  That's great.  However, sometimes your intuition will be fouled up by incorrect assumptions.  When you're stuck, binary search is priceless.</p>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.barsoom.org/debug-program/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!--

Minified using file
Page Caching using apc
Database Caching 125/139 queries in 0.020 seconds using apc
Content Delivery Network via c0348292.cdn.cloudfiles.rackspacecloud.com

Served from: almond.barsoom.org @ 2010-07-31 11:59:40 -->