<?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>while coding &#187; How I Code</title>
	<atom:link href="http://www.youell.com/matt/writing/?cat=18&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.youell.com/matt/writing</link>
	<description>simplify</description>
	<lastBuildDate>Wed, 31 Oct 2018 04:08:22 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A Simple Rewrite (How I Code)</title>
		<link>http://www.youell.com/matt/writing/?p=84</link>
		<comments>http://www.youell.com/matt/writing/?p=84#comments</comments>
		<pubDate>Mon, 20 Feb 2012 02:58:05 +0000</pubDate>
		<dc:creator>matt</dc:creator>
				<category><![CDATA[How I Code]]></category>
		<category><![CDATA[humane]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.youell.com/matt/writing/?p=84</guid>
		<description><![CDATA[Note: I just found this unpublished post from a couple of years ago. I think I wanted to polish it a bit before publishing, but it looks fine to me now.

I&#8217;m taking another shot at the &#8220;How I Code&#8221; series of posts. This isn&#8217;t a preachy &#8220;How everyone should code&#8221; post. Instead, this is a [...]]]></description>
			<content:encoded><![CDATA[<p><em>Note: I just found this unpublished post from a couple of years ago. I think I wanted to polish it a bit before publishing, but it looks fine to me now.<br />
</em><br />
I&#8217;m taking another shot at the &#8220;How I Code&#8221; series of posts. This isn&#8217;t a preachy &#8220;How everyone should code&#8221; post. Instead, this is a snapshot of how I look at code now, at this point in time. Next week I could be doing things differently. </p>
<p>Here&#8217;s an example from Satchmo, the web shopping cart software that is built on the Django framework. A bug involving MySQL was recently patched in the Satchmo code base. The bug fix itself isn&#8217;t very interesting, but the remaining code is. Here&#8217;s the post-fix code:</p>
<pre class="syntax-highlight:python">
def payments_completed(self):
    q = self.payments.exclude(transaction_id__isnull = False, transaction_id = &quot;PENDING&quot;)
    result = [p for p in q if p.amount]
    return result
</pre>
<p>Naturally, I have questions.</p>
<p>Why does the &#8216;result&#8217; variable exist? </p>
<pre class="syntax-highlight:python">
def payments_completed(self):
    q = self.payments.exclude(transaction_id__isnull = False, transaction_id = &quot;PENDING&quot;)
    return [p for p in q if p.amount]
</pre>
<p>What are &#8216;p&#8217; and &#8216;q&#8217;?</p>
<pre class="syntax-highlight:python">
def payments_completed(self):
    query_set = self.payments.exclude(transaction_id__isnull = False, transaction_id = &quot;PENDING&quot;)
    return [payment for payment in query_set if payment.amount]
</pre>
<p>Would a non-programmer understand this?</p>
<pre class="syntax-highlight:python">
def payments_completed(self):
    payments = self.payments.exclude(transaction_id__isnull = False, transaction_id = &quot;PENDING&quot;)
    return [payment for payment in payments if payment.amount]
</pre>
<p>Don&#8217;t we already have a &#8216;payments&#8217; variable?</p>
<pre class="syntax-highlight:python">
def payments_completed(self):
	return self.payments.exclude(transaction_id__isnull = False, transaction_id = &quot;PENDING&quot;, amount__isnull = false)
</pre>
<p>I think that&#8217;s better. What do you think?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.youell.com/matt/writing/?feed=rss2&amp;p=84</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Cat In the Hat</title>
		<link>http://www.youell.com/matt/writing/?p=563</link>
		<comments>http://www.youell.com/matt/writing/?p=563#comments</comments>
		<pubDate>Sat, 26 Sep 2009 19:33:10 +0000</pubDate>
		<dc:creator>matt</dc:creator>
				<category><![CDATA[How I Code]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[programming languages]]></category>

		<guid isPermaLink="false">http://www.youell.com/matt/writing/?p=563</guid>
		<description><![CDATA[I continue to be impressed by the flexibility of list comprehensions, especially in Python. The other day I discovered that I could handle a small but twisty problem quite simply, which I will now share in the style of Dr. Seuss:


[hat[cat] if cat in hat else cat for cat in cats] 

]]></description>
			<content:encoded><![CDATA[<p>I continue to be impressed by the flexibility of list comprehensions, especially in Python. The other day I discovered that I could handle a small but twisty problem quite simply, which I will now share in the style of Dr. Seuss:</p>
<pre class="syntax-highlight:python">

[hat[cat] if cat in hat else cat for cat in cats] 
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.youell.com/matt/writing/?feed=rss2&amp;p=563</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Snippetology</title>
		<link>http://www.youell.com/matt/writing/?p=367</link>
		<comments>http://www.youell.com/matt/writing/?p=367#comments</comments>
		<pubDate>Wed, 13 May 2009 00:51:47 +0000</pubDate>
		<dc:creator>matt</dc:creator>
				<category><![CDATA[30-in-30]]></category>
		<category><![CDATA[How I Code]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[django]]></category>

		<guid isPermaLink="false">http://www.youell.com/matt/writing/?p=367</guid>
		<description><![CDATA[My new release today is called Snippetology which is a little app plugin for Django. 
Do you ever want to allow a non-technical user to edit just a part of a webpage? Just one paragraph? Or just some text over in the corner? That&#8217;s what Snippetology is for. You create a named snippet of text [...]]]></description>
			<content:encoded><![CDATA[<p>My new release today is called <a href="http://code.google.com/p/django-snippetology/">Snippetology</a> which is a little app plugin for Django. </p>
<p>Do you ever want to allow a non-technical user to edit just a part of a webpage? Just one paragraph? Or just some text over in the corner? That&#8217;s what Snippetology is for. You create a named snippet of text in the database by using an editor in the Django Admin panel. Then in the the webpage template you include a little tag that retrieves that snippet by name from the database and displays it. From that point forward your user can edit the text in the Django Admin panel and their changes will appear in the part of the webpage that you let them control.</p>
<p>This is basic CMS stuff. Django, not being a CMS, does not do this for you. (Or at least it did not at the time I created this. Things are moving so fast with Django and the surrounding community that there could easily be a solution I&#8217;m unaware of.)</p>
<p>Like any good tool, there is an &#8220;advanced&#8221; use for it. Snippets can contain all sorts of things: tags for embedded videos or Flash animations, or behind the scenes stuff like CSS and Javascript. So anything can be in a snippet. Cool! (Well, cool as long as you trust your users. You DO trust your users, don&#8217;t you?)</p>
<p>Next I added a randomizing filter. What the filter does is select a random line from the text in a particular snippet. This makes for interesting possibilities. The randomizing feature was originally requested by a client for a tagline generator. But since then it has become clear that more uses exist. For instance, put a banner image tag with a link tag on each line and you&#8217;ve created your own ad rotator. And if you&#8217;ve been around the web for any length of time, you know that ad rotators get used for all sorts of oddball things. Which is kind of fun!</p>
<p>Snippetology is a dirt simple idea. It&#8217;s not a genius breakthrough type of thing. I think that&#8217;s part of why I like it so much. It&#8217;s got that Unix tools feel; it does one thing and does it well. </p>
<p>Actually, I take that back: It is now two tools that each do one thing well! I hope I see more opportunities in the future to create little tools like this. It&#8217;s fun and makes me think in new ways.</p>
<p><em><strong>Update (May 14, 2009):</strong></em><br />
Other people like this idea. In fact, they like it so much that there are at least four other comparable projects out there that do the same thing! Clearly people aren&#8217;t finding solutions easily enough. It&#8217;s odd to think that this is a limitation of a large network. Yes, the Internet is huge, but you only can see what you can find or connect to. Things outside of your network (people you know, things you see, what you&#8217;re capable of finding with search) might as well not exist. How to solve this? I don&#8217;t know. It&#8217;s an interesting problem though. </p>
<p>Here&#8217;s a list of the other projects I&#8217;ve found so far that are like Snippetology:</p>
<ul>
<li>django-flatcontent</li>
<li>django-chunks</li>
<li>django-flatblocks</li>
<li>django-generic-flatblocks</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.youell.com/matt/writing/?feed=rss2&amp;p=367</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Two Releases So Far</title>
		<link>http://www.youell.com/matt/writing/?p=252</link>
		<comments>http://www.youell.com/matt/writing/?p=252#comments</comments>
		<pubDate>Fri, 20 Mar 2009 08:40:51 +0000</pubDate>
		<dc:creator>matt</dc:creator>
				<category><![CDATA[30-in-30]]></category>
		<category><![CDATA[How I Code]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.youell.com/matt/writing/?p=252</guid>
		<description><![CDATA[A quick post about what I&#8217;ve released so far this week. I got started on this 30 in 30 project on the 17th. As I said in my original post, I&#8217;m already behind because I&#8217;m three days into the project and I&#8217;ve only got two releases out. Happily my 30 in 30 goal is an [...]]]></description>
			<content:encoded><![CDATA[<p>A quick post about what I&#8217;ve released so far this week. I got started on this <a href="http://www.youell.com/matt/writing/?p=243">30 in 30</a> project on the 17th. As I said in my original post, I&#8217;m already behind because I&#8217;m three days into the project and I&#8217;ve only got two releases out. Happily my 30 in 30 goal is an average, otherwise I&#8217;d be hosed already! Without further blabbity blah, here&#8217;s what I&#8217;ve released so far:</p>
<p><strong>Release 1: Web app for Client M.</strong></p>
<p>This app was kind of like the MG of projects. Tiny as hell but still kind of fun. The client is a M$ shop, so it&#8217;s a .Net app in C#, and it&#8217;s main job is to handle uploads of large data files. In this case &#8220;large&#8221; means 30 MB to 1GB. Not the biggest files ever, but still enough to keep the browser idling uncomfortably long. I went digging around to see if anything could help. I had visions of end users trying to upload files, watching the spinning icon roll in Firefox and not knowing what the hell was going on. I wanted something friendly and understandable. A little searching found some Flash based solutions and some discussion of a Silverlight approach. I was indecisive as hell for a while. Every option came with a catch and tradeoffs! Then, just as I was about to roll my own Silverlight solution, I found <a href="http://swfupload.org/">SWFUpload</a>. As the name implies, it&#8217;s a Flash-based solution. These days everyone has Flash except the tinfoil hat crowd, so I was game and I gave it a try. Like any other widget, there are learning curves and quirks. Even so, it turned out great and I&#8217;m very happy. Now my client&#8217;s users can see pretty upload bars and can upload a bunch of files at once. An unexpected dose of awesome.</p>
<p><strong>Release 2: New version of Client M&#8217;s existing Windows app</strong></p>
<p>&#8220;Existing Windows app&#8221;? If that sounds boring, you&#8217;re right, it is. This is an internal business app I created from client specs about 4 years ago and I&#8217;ve been maintaining ever since. New releases roll out about once a year. The app is a .Net/C# dealio than ties into a CRM system. This release added a new database reporting tool. Exciting right? Hey, I never promised to excite anyone with my releases. Hopefully the client will be excited. That&#8217;s all I can hope for.</p>
<p>Maybe my software releases would be more exciting if I was focusing on more exciting markets? I wonder what kind of software a nudie bar would need&#8230; Thoughts?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.youell.com/matt/writing/?feed=rss2&amp;p=252</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>30 in 30, a Personal Challenge</title>
		<link>http://www.youell.com/matt/writing/?p=243</link>
		<comments>http://www.youell.com/matt/writing/?p=243#comments</comments>
		<pubDate>Thu, 19 Mar 2009 23:35:32 +0000</pubDate>
		<dc:creator>matt</dc:creator>
				<category><![CDATA[30-in-30]]></category>
		<category><![CDATA[How I Code]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.youell.com/matt/writing/?p=243</guid>
		<description><![CDATA[I&#8217;ll try to step out of my usual wordy style in this post. I&#8217;m trying something new. This is not an experiment, exactly. It&#8217;s more of an endurance trial.
I&#8217;m going to do 30 software releases in 30 days.
To clarify, I&#8217;m not starting 30 companies or shipping 30 products. I&#8217;m shipping 30 software projects. Some of [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ll try to step out of my usual wordy style in this post. I&#8217;m trying something new. This is not an experiment, exactly. It&#8217;s more of an endurance trial.</p>
<p>I&#8217;m going to do 30 software releases in 30 days.</p>
<p>To clarify, I&#8217;m not starting 30 companies or shipping 30 products. I&#8217;m shipping 30 software projects. Some of these projects I&#8217;d like to see pay off in some way. Some are for clients. Others are just for fun.</p>
<p>I have a few reasons for doing this. For starters, I never got my <a href="http://www.youell.com/matt/writing/?p=29">Christmas Wish</a>.</p>
<p>For another thing, I&#8217;ve been so busy with work and family the past few years that every time I get an idea for a project (even a little one) I have to immediately shelve it. That gets depressing after a while. It also blocks further creativity. When you&#8217;ve got a lot of old &#8220;coulda/shoulda&#8221; ideas floating around in your head it is hard to get new ideas to grow. I&#8217;m hoping to exorcise some of those ideas and be free of them.</p>
<p>And frankly, I haven&#8217;t been satisfied with my development velocity lately. My inner prima donna has kicked in and I&#8217;ve been spending way too much time on how things <em>should</em> be instead of how things <em>need</em> to be to get out the door. In my last &#8220;regular job&#8221; I was doing late-stage R&amp;D on a distributed system, so some prima donna action was warranted. But as a self-employed programmer that is bad business!</p>
<p>My software development business has recently slowed in the &#8220;New&#8221; New Economy, so I figured I might as well hone my skills and &#8220;sharpen the saw&#8221;. And the sword. And those axes over there&#8230;</p>
<p>Some thoughts:</p>
<ol>
<li>It&#8217;s not natural to ship software like this. I will very likely fail.</li>
<li>That&#8217;s ok with me. The personal challenge and the experience are the main points.</li>
<li>30 in 30 is an average. Some days I won&#8217;t release anything.</li>
<li>What I release will probably look and work like shit until I can refine and polish. No bitching! <img src='http://www.youell.com/matt/writing/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
<li>These won&#8217;t be cookie cutter projects. I&#8217;ll certainly be reusing code opportunistically, but I intend to mix it up a bit and try some new things out.</li>
<li>I only have about 10 firm project ideas right now.</li>
<li>I will cheat and fight dirty. I reserve the right to redefine terms, be unoriginal, punch your mom, and declare myself successful at any point, even if all signs point to failure.</li>
<li>In fact, I&#8217;ve already cheated. I got a head start on this two days ago. I&#8217;m rolling out my second release tonight. So yeah, I&#8217;m already behind. <img src='http://www.youell.com/matt/writing/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
<li>I&#8217;m going to blog brief descriptions of each thing I ship. If time allows I&#8217;ll also talk about technical hurdles and miscellaneous geekery.</li>
<li>Tempting as it may be, I&#8217;m not going to declare that every single build of a project is a new release. That would be bullshit. Then again I&#8217;m not going to declare hard and fast rules about what constitutes a release. Or a project. Or software, for that matter. Feel free to call bullshit at anytime in the comments. Just remember that my blog is not a democracy and we censor like Pravda around here.</li>
</ol>
<p>Wish me luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.youell.com/matt/writing/?feed=rss2&amp;p=243</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Finally, someone says it</title>
		<link>http://www.youell.com/matt/writing/?p=131</link>
		<comments>http://www.youell.com/matt/writing/?p=131#comments</comments>
		<pubDate>Thu, 22 Jan 2009 13:44:29 +0000</pubDate>
		<dc:creator>matt</dc:creator>
				<category><![CDATA[How I Code]]></category>
		<category><![CDATA[humane]]></category>
		<category><![CDATA[not good enough]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software industry]]></category>

		<guid isPermaLink="false">http://www.youell.com/matt/writing/?p=131</guid>
		<description><![CDATA[Null references are a bad idea. When I try to say things like this, people blow me off. I suppose that is understandable. I&#8217;m just another opinionated programmer, never mind the bruises and scars. 
I think it&#8217;s a little harder to blow off C.A.R. Hoare. Thanks Tony!
]]></description>
			<content:encoded><![CDATA[<p><a href="http://qconlondon.com/london-2009/presentation/Null+References:+The+Billion+Dollar+Mistake">Null references are a bad idea</a>. When I try to say things like this, people blow me off. I suppose that is understandable. I&#8217;m just another opinionated programmer, never mind the bruises and scars. </p>
<p>I think it&#8217;s a little harder to blow off <a href="http://en.wikipedia.org/wiki/C._A._R._Hoare">C.A.R. Hoare</a>. Thanks Tony!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.youell.com/matt/writing/?feed=rss2&amp;p=131</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Injection of Failure &#8211; Breaking your code on purpose</title>
		<link>http://www.youell.com/matt/writing/?p=69</link>
		<comments>http://www.youell.com/matt/writing/?p=69#comments</comments>
		<pubDate>Fri, 19 Dec 2008 19:43:42 +0000</pubDate>
		<dc:creator>matt</dc:creator>
				<category><![CDATA[How I Code]]></category>
		<category><![CDATA[problem solving]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.youell.com/matt/writing/?p=69</guid>
		<description><![CDATA[I&#8217;ve had a post by Kent Beck sitting in a browser window for a few weeks, waiting to be read. That&#8217;s how I roll: I pile up open browser windows and tabs, promising myself I&#8217;ll take a minute to read whatever new shiny thing I&#8217;ve found, and then maybe a few weeks later I&#8217;ll get [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve had a post by Kent Beck sitting in a browser window for a few weeks, waiting to be read. That&#8217;s how I roll: I pile up open browser windows and tabs, promising myself I&#8217;ll take a minute to read whatever new shiny thing I&#8217;ve found, and then maybe a few weeks later I&#8217;ll get to it. Not the best system, I admit. However, it is about the only way I get things read.</p>
<p>So Kent&#8217;s post is titled &#8220;<a href="http://www.threeriversinstitute.org/HitEmHighHitEmLow.html">Hit &#8216;Em High, Hit &#8216;Em Low</a>&#8220;, and in the post he describes a debugging technique he calls the Saff Squeeze. It is very similar to a technique that I was taught when I was a junior programmer by my good friend and mentor John Petts. Up until now I&#8217;ve just called the technique &#8220;break it on purpose&#8221;. If I knew we were honoring people with names for things I would have given it a better name, like &#8220;the Petts Awesomeness&#8221; or something. Still, a name would be good, and Saff Squeeze isn&#8217;t really working for me (no offense to either Saff or Beck). If we continue Kent&#8217;s football metaphor we might just call it &#8220;Intentional Grounding&#8221;, but I&#8217;d prefer to think of it as &#8220;Injection of Failure&#8221;.</p>
<p>In Kent&#8217;s post he describes the technique in unit-testing and refactoring terms: You have a test that doesn&#8217;t work and you can&#8217;t spot the problem. So you inline your code-under-test which allows you to move your assertion(s) up through the inlined code until you find the error.</p>
<p>This technique is not limited to unit tests and doesn&#8217;t require refactoring. It <em>does</em> require a willingness to break things. That&#8217;s about it. Here&#8217;s all there is to it: </p>
<blockquote><p>If you have an error in your code but you can&#8217;t tell exactly where that error is, create your own error that you understand and try to place it in the code before the error you&#8217;re hunting for.</p></blockquote>
<p>When I was first taught this trick I was a C++ developer trying to troubleshoot a complicated build that had started failing after I pulled in code from another project. But this technique works great in any complicated system. I&#8217;ve used this technique recently to diagnose problems with Django, a Python-based web application framework which can sometimes generate very mysterious error messages. But the place that I&#8217;ve had the greatest success with this technique is diagnosing Javascript errors in the browser, where development is often like eating noodles with one chopstick.</p>
<p>The one refinement to this technique that Kent doesn&#8217;t mention is to use a binary search to find the bug. If there are a 100 lines of code and you know the error is in there somewhere, inject your own error right in the middle of that code. If the error message or program result changes, you know that the bug lives in those last 50 lines. Otherwise it is in the first 50. Then you repeat, moving your injected error halfway through the remaining code each time, until eventually you find the offending line(s) of code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.youell.com/matt/writing/?feed=rss2&amp;p=69</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My Opinion of Types over Time</title>
		<link>http://www.youell.com/matt/writing/?p=33</link>
		<comments>http://www.youell.com/matt/writing/?p=33#comments</comments>
		<pubDate>Thu, 27 Nov 2008 21:22:36 +0000</pubDate>
		<dc:creator>matt</dc:creator>
				<category><![CDATA[How I Code]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.youell.com/matt/writing/?p=33</guid>
		<description><![CDATA[
&#160;
]]></description>
			<content:encoded><![CDATA[<p style="text-align: center"><img src="/matt/writing/lib/images/opinion-of-types-over-time-600x378.png" alt="The value of types over time" height="378" width="600" /></p>
<p style="text-align: center">&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.youell.com/matt/writing/?feed=rss2&amp;p=33</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Names Matter (How I Code)</title>
		<link>http://www.youell.com/matt/writing/?p=19</link>
		<comments>http://www.youell.com/matt/writing/?p=19#comments</comments>
		<pubDate>Tue, 05 Aug 2008 00:24:06 +0000</pubDate>
		<dc:creator>matt</dc:creator>
				<category><![CDATA[How I Code]]></category>
		<category><![CDATA[essays]]></category>
		<category><![CDATA[humane]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.youell.com/matt/writing/?p=19</guid>
		<description><![CDATA[I&#8217;ve had this website for about 11 years. I&#8217;ve been aware of blogging, in one form or another for about 8 years. But it wasn&#8217;t until this year that I started blogging. I resisted for many years. There were just so many ass-clowns with blogs. Why be another? Still, when I sit and code there [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve had this website for about 11 years. I&#8217;ve been aware of blogging, in one form or another for about 8 years. But it wasn&#8217;t until this year that I started blogging. I resisted for many years. There were just so many ass-clowns with blogs. Why be another? Still, when I sit and code there are often ideas which will come to me and I want to get them out. And yet the topics I&#8217;ve talked about on this site (so far) are technical in topic but not in detail. There&#8217;s a reason for this. It&#8217;s hard to document the mental process you go through when you&#8217;re programming. I tried once this past spring when I was working on a small project with LINQ. But even though I typed out a lot of material, it was very hard to pull together something readable out of it and I eventually gave up. This time I&#8217;m trying a simpler topic and we&#8217;ll see how it goes. The writing is still rough because I wrote it on the spot and backtracked over code I had just written. Hopefully I&#8217;ll figure out how to do this more smoothly in the future because there are ideas and notions, both small and large, which I&#8217;m uncovering every day and would like to share and discuss.</p>
<p align="center">***</p>
<p>I work with several different technologies on a regular basis. Lately I&#8217;ve been working on a .NET Windows app. It&#8217;s one I created originally in 2004 and have been maintaining for a client ever since. I&#8217;ve been adding some new database searching functionality to the app.</p>
<p>To display the search data in the app I have a series of tabs, all of which contain a customized listview. Each listview has a Display() method which takes in a list of things to display. The difference between each list view is the subset of the data shown and how each column is treated. (There are some columns with special handling.)</p>
<p>After a while I notice this pattern where I&#8217;m going to update only the currently visible list view, so I end up with a dispatch table which abstracts away exactly which Display() method I&#8217;m going to call.<br />
I started off with a List for doing dispatch, with each tab&#8217;s index pointing into the table. But that bothered me.</p>
<p>Mainly, I wondered, what would happen if I added or removed tabs in the future? I didn&#8217;t savor the idea of mixed up tabs or a null pointer exception.</p>
<p>At first I thought a hashtable would have the same problem, but that&#8217;s because I was &#8220;complifying&#8221; it, and thinking about using a string for the lookup &#8211; either the tab name or tab text.</p>
<p>Then, &#8220;Duh&#8221;, I thought, &#8220;just use the tab itself as the key&#8221;.</p>
<p>And it really is a duh, because I&#8217;m pretty sure I&#8217;ve gone through this exact same internal dialog many times in the past. Oy.<br />
So eventually I ended up with this:</p>
<pre class="syntax-highlight:csharp">
protected delegate void ListProcessor(List&lt;Item&gt; list);
protected Dictionary&lt;TabPage, ListProcessor&gt; listViews = new Dictionary&lt;TabPage, ListProcessor&gt;();
</pre>
<p>I&#8217;m still not so happy with the delegate since it is almost the signature for map(), and that seems like a smell. Also bugging me is some vague nag about polymorphism which won&#8217;t quite bubble all the way to the top of my brain.<br />
So, anyway, I go on to set up my event handler which captures a tab change. Except there seems to be some kind of retardation here. If you handle TabIndexChanged, that doesn&#8217;t fire if you change the selected tab. You have to handle SelectedIndexChanged instead. Which, in retrospect, makes total sense. But the naming could be a lot better here. I&#8217;m still not entirely sure what TabIndexChanged does, although I suspect it has to do with tab-reordering. Still, <strong>names matter</strong>, and that one naming choice is confusing. Speaking of how names matter, here&#8217;s the handler I created for SelectedIndexChanged:</p>
<pre class="syntax-highlight:csharp">
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
    UpdateCurrentListView();
}
</pre>
<p>I wish VS would let you enter the function name as soon as you break it out. I&#8217;m going to have to rename it anyway, might as well.<br />
Since I&#8217;m calling UpdateCurrentListView() from another place, without the event signature, I can&#8217;t just point to UpdateCurrentListView(),<br />
but I can give it the same name.</p>
<pre class="syntax-highlight:csharp">
private void UpdateCurrentListView(object sender, EventArgs e)
{
    UpdateCurrentListView();
}
</pre>
<p>Better? Worse? I don&#8217;t know. Ok, yes, I do know. Better. Just not WOW better. Anyway, onto the meat and potatoes of what I&#8217;m doing. Here&#8217;s UpdateCurrentListView():</p>
<pre class="syntax-highlight:csharp">
private void UpdateCurrentListView()
{
    listViews[tabControl1.SelectedTab](foundItems);
}
</pre>
<p>It&#8217;s just one line of code, but even though I *just wrote it*, I knew I wasn&#8217;t going to know what it did in about 5 mins. I try really hard<br />
not to let things like that go by. People have to read this stuff, and more often than not those people are me!</p>
<p>The name &#8220;listViews&#8221; made sense for the dispatch table when I was first setting it up and inserting functions into it. Actually *using* it didn&#8217;t make sense at all: &#8220;listViews&#8221; sounds like a *list* of *views*. In reality this is a list of functions.</p>
<p>I also am not a big fan of the automatically generated names VS gives things. It works well enough, but it doesn&#8217;t scale at all. You can only have so many textBox15&#8217;s before you gouge our your eyes and swear you&#8217;ll stop programming forever.</p>
<p>Then there&#8217;s the list of items that will be displayed. That list is shared among all of the views in the containing class. When I originally created it, &#8220;foundItems&#8221; seemed like a friendly name. But down here (at this level in the code) you&#8217;re not thinking in terms of things being found. You want to have a more generic idea of what you&#8217;re dealing with.</p>
<p>Some refactoring gave me:</p>
<pre class="syntax-highlight:csharp">
private void UpdateCurrentListView()
{
    displayOnTab[tabs.SelectedTab](SearchResults);
}
</pre>
<p>I realized that while my dispatch table was technically a list or collection, the way I&#8217;m using it is as a multi-dimensional function. So why not just name it like a function?</p>
<p>So that&#8217;s what I did. Also, I&#8217;m only using one TabControl. Might as well go with a friendly name like &#8220;tabs&#8221;. And SearchResults is generic enough so that I don&#8217;t stop and think about the data but specific enough so I have a mental picture of what is happening in the UI when this code executes.</p>
<p>This reads fairly well, is only moderately redundant, and it has a decent English flavor: there&#8217;s a subject acting with a verb upon an object.</p>
<p>Good times.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.youell.com/matt/writing/?feed=rss2&amp;p=19</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
