<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
        <title>Removing All Doubt</title>
        <description>Removing All Doubt - Chuck Jazdzewski</description>
        <link>http://removingalldoubt.com</link>
        <link>http://removingalldoubt.com</link>
        <lastBuildDate>2025-08-01T04:12:51+00:00</lastBuildDate>
        <pubDate>2025-08-01T04:12:51+00:00</pubDate>
        <ttl>1800</ttl>


        <item>
                <title>A touchy subject</title>
                <description>
&lt;p&gt;&lt;a href=&quot;http://removingalldoubt.com/programming/2015/07/08/the-delphi-event/&quot;&gt;Visual Foo&lt;/a&gt;, the project that eventually led to Delphi, had one directive; 
create a development environment more visual than &lt;a href=&quot;https://en.wikipedia.org/wiki/Visual_Basic&quot;&gt;Visual Basic&lt;/a&gt;. We took 
that mandate very seriously. During the height of the project we produced some 
lovely prototypes that resembled the soon to be discontinued &lt;a href=&quot;https://pipes.yahoo.com/pipes/&quot;&gt;Yahoo Pipes&lt;/a&gt; or 
Microsoft’s long since discontinued &lt;a href=&quot;https://www.cloudave.com/1843/microsoft-kills-yahoo-pipes-competitor/&quot;&gt;Popfly&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What we learned, and I can only assume that Yahoo and Microsoft learned much 
later, was that such approaches are not better, they are just different. Making
something more “visual” doesn’t, by itself, make it better it just makes it more
visual.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;I put Visual Foo and projects like it on a list of mistakes that every 
generation will make. I am convinced this model will come back with as much or
more fanfare than Pipes and Popfly did and will eventually die just as they did.
There are some mistakes that are so appealing they will be made over and over
again.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Delphi was a step back from the radical approaches that we experimented with
in &lt;a href=&quot;http://removingalldoubt.com/programming/2015/07/08/the-delphi-event/&quot;&gt;Visual Foo project&lt;/a&gt;. It competed directly with &lt;a href=&quot;https://en.wikipedia.org/wiki/Visual_Basic&quot;&gt;Visual Basic&lt;/a&gt; instead
of trying to be more visual than Visual Basic. We believed Delphi brought a 
better compiler, a better language, and a system that was much easier to extend.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;I have since developed much more respect for the design of the Visual Basic
language than I had at the time. It is still not among my favorite languages but
I no longer run away screaming.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When you look closely at Visual Basic you realize that it is neither Visual nor
BASIC. It is not really BASIC; it is a long, long way from &lt;a href=&quot;https://en.wikipedia.org/wiki/Dartmouth_BASIC&quot;&gt;Dartmouth BASIC&lt;/a&gt; 
or even &lt;a href=&quot;https://en.wikipedia.org/wiki/Microsoft_BASIC&quot;&gt;Microsoft BASIC&lt;/a&gt;. It shared some of the characteristics of BASIC and 
kept some of the syntax but a Visual Basic program looks much different than one 
in its older cousins. It also isn’t Visual. The only “visual” part of Visual 
Basic is that it has a form designer. The Visual Basic form designer appeals 
more to &lt;a href=&quot;https://en.wikipedia.org/wiki/WYSIWYG&quot;&gt;WYSIWYG&lt;/a&gt; than visual programming. All the actual programming is done 
in a text editor, not the form designer. Delphi is the same.&lt;/p&gt;

&lt;p&gt;One of the things that made Delphi’s model easier was that you didn’t have to 
visualize in your head where a thing like the file open dialog was, you could 
point to it, “It is right there next to the button that opens it.” You can move 
it around; reach out and touch it so to speak. I never expect this to gain 
coinage, but this is why I sometimes refer to Delphi’s programming model as 
tactile programming, not visual programming. Simplicity was helped more by the 
illusion that you could touch the component, moving it where you want it, not 
just that you could see it. To keep this illusion it should act as close to a 
physical object as was reasonable. It needed to feel like you could pick it up.&lt;/p&gt;

&lt;p&gt;It is this concept that helps explains why I never created a “gutter” for 
non-visual components. Any restrictions on a component’s movement breaks the 
illusion. This also explains why the Data Module looks and acts like a form; it 
helps reinforce the illusion. If the component can only appear “off screen” or 
in a restricted area, it no longer feels like a physical thing, the illusion 
breaks down. Even though non-visual components can get in the way of the forms
layout, I felt maintaining the illusion was more important than the irritation 
the real estate they consume causes.&lt;/p&gt;

&lt;p&gt;Making something “visual” helps simplicity when it reinforces the conceptual 
module of the underlying system. It hurts when it tries to visualize something 
better left in code.&lt;/p&gt;

</description>
                <link>http://removingalldoubt.com/programming/2015/07/30/a-touchy-subject</link>
                <guid>http://removingalldoubt.com/programming/2015/07/30/a-touchy-subject</guid>
                <pubDate>2015-07-30T00:00:00+00:00</pubDate>
        </item>

        <item>
                <title>Simplicity isn't simple</title>
                <description>
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Button.Caption := &apos;OK&apos;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This was first line of “Delphi” code ever written. As I mentioned in &lt;a href=&quot;http://removingalldoubt.com/programming/2015/07/05/properties-on-purpose/&quot;&gt;Properties
on purpose&lt;/a&gt; Anders wrote this on his whiteboard during one of our earliest
design discussion and we needed to get this to work. I already discussed what we
added to get this to work, but just as significant is what we removed.&lt;/p&gt;

&lt;p&gt;I have participated in, reviewed, read about, or heard about many things 
purporting to make programming easier. The few that actually did make things 
easier had one thing in common, they reduced the number of concepts the 
programmer had to keep in their head.&lt;/p&gt;

&lt;p&gt;One of the primary influences to OWL (the ObjectWindows Library), the 
predecessor to the VCL, was a little known now, but wildly heralded at the time,
language called &lt;a href=&quot;https://en.wikipedia.org/wiki/Actor_(programming_language)&quot;&gt;Actor&lt;/a&gt;. It was a &lt;a href=&quot;https://en.wikipedia.org/wiki/Smalltalk&quot;&gt;Smalltalk&lt;/a&gt;-like language that made 
programming Windows a lot easier. However, one way it demonstrated that 
simplicity in its advertising was to show how you could write a one line editor 
application. That ad stuck with me, but not for the reason the marketing person
who came up with that ad intended. It lead me to this guiding principle,&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;I don’t mind requiring two lines of code to accomplish what could be 
accomplished in one line if I can understand both lines.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The line of code, through powerful, was completely incomprehensible and couldn’t 
be reproduced independently without complete understanding of the underlying 
framework of &lt;a href=&quot;https://en.wikipedia.org/wiki/Actor_(programming_language)&quot;&gt;Actor&lt;/a&gt;. Counting lines as a judge of simplicity is a fallacy. 
It is just an indicator, not an arbiter, of simplicity. The above principle is 
an informal observation of the fact that sometimes two lines are simpler than 
one.&lt;/p&gt;

&lt;p&gt;Actor made programming in Windows much easier than its predecessors but being
able to utter a string hieroglyphics to produce an editor was &lt;em&gt;not&lt;/em&gt; the way it
did it. It did it by reducing the number of concepts the user had to learn and
remember and it did it by reducing the bookkeeping which was, up till then, 
required.&lt;/p&gt;

&lt;p&gt;The first line of “Delphi” code ever written as an appeal to a reduction in
concept count. If that was to be written in Turbo Pascal it would have had to
look like,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Button^.SetCaption(&apos;OK&apos;);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;^&lt;/code&gt; operator, in Pascal, is a pointer dereference operator. First, 
linguistically, in this context, it is redundant. The member selection operator 
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt; is not legal on a pointer so the only reasonable interpretation of 
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Button.SetCaption&lt;/code&gt; would be for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SetCaption&lt;/code&gt; to be a member of the type 
referenced by the pointer. Second, it requires the understanding when and, when 
not to, use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;^&lt;/code&gt; operator. We realized that removing the need for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;^&lt;/code&gt; 
operator allowed us to defer the need for the user to understand pointers. If we 
allocated the button on the behalf of the user, they wouldn’t need to know &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;New&lt;/code&gt; 
and, later, if we disposed of the memory on their behalf, they wouldn’t need to 
understand &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dispose&lt;/code&gt; either. All this was accomplished simply by deleting one 
character from the line &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Button^.SetCaption(&apos;OK&apos;)&lt;/code&gt; to form 
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Button.SetCaption(&apos;OK&apos;)&lt;/code&gt;; properties allowed us to further reduce this to 
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Button.Caption := &apos;OK&apos;&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;This actually brought us more in-line with the original &lt;a href=&quot;https://en.wikipedia.org/wiki/Object_Pascal&quot;&gt;Object Pascal&lt;/a&gt;
as defined by Apple in consultation with &lt;a href=&quot;https://en.wikipedia.org/wiki/Niklaus_Wirth&quot;&gt;Niklaus Wirth&lt;/a&gt; which also did not
require the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;^&lt;/code&gt; in this context.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the VCL we took nearly every opportunity we could find to reduce the number 
of concepts the programmer was required to learn and/or maintain in their head 
when using Delphi as a &lt;a href=&quot;http://removingalldoubt.com/programming/2015/07/10/a-component-of-the-story/&quot;&gt;component user&lt;/a&gt;. This showed up in everything from 
the way menus were defined to how Windows messages were routed.&lt;/p&gt;

&lt;p&gt;Reducing concepts can also lead you into trouble. We always kept this maxim in
mind (commonly attributed to &lt;a href=&quot;https://en.wikipedia.org/wiki/Albert_Einstein&quot;&gt;Albert Einstein&lt;/a&gt;):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Everything should be made as simple as possible, but not simpler.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;I found it ironic that Microsoft chose the name “Einstein” for their, now
abandoned, user persona they used for a user who could handle a lot of 
complexity.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I always interpreted this, when applied to programming, as an appeal against 
&lt;a href=&quot;https://en.wikipedia.org/wiki/Magic_(programming)&quot;&gt;magic&lt;/a&gt;, like the Actor incantation for a one line application. The 
additional requirement of not only simplifying programming for component users, 
but also for component writers, kept us honest. Any magic we introduced for the 
component user needed to be fully understood by the component writer; which 
meant, in our case, magic only postponed, it did not not eliminate, complexity.&lt;/p&gt;

</description>
                <link>http://removingalldoubt.com/programming/2015/07/23/simplicity-isnt-simple</link>
                <guid>http://removingalldoubt.com/programming/2015/07/23/simplicity-isnt-simple</guid>
                <pubDate>2015-07-23T00:00:00+00:00</pubDate>
        </item>

        <item>
                <title>The Biggest Bang</title>
                <description>
&lt;p&gt;Valentine’s Day, February 14, 1995.&lt;/p&gt;

&lt;p&gt;I was sitting the front row at the main hall in the Moscone Center. The lights 
had just dimmed and a hush fell over the crowd. Several thousand people suddenly 
in wrapped attention all looking at the stage; flashes of cameras going off, and 
murmurs of anticipation. Then Anders took the stage and started &lt;a href=&quot;http://edn.embarcadero.com/article/32977&quot;&gt;the demo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I had never experienced anything like it before or since. I was nervous and
excited at the same time. We had built up the expectations of everyone, I was
hoping we didn’t disappoint them.&lt;/p&gt;

&lt;p&gt;Apparently we didn’t. It seemed that after every feature Anders showed people 
cheered and when Anders showed that Delphi itself was a Delphi app we received a 
standing ovation. I knew we had done something remarkable; something I will 
always be proud of.&lt;/p&gt;

&lt;p&gt;From the &lt;a href=&quot;http://removingalldoubt.com/programming/2015/07/08/the-delphi-event/&quot;&gt;yes!&lt;/a&gt; we received to that amazing night was about 2 years. That was 
the longest I had every spent working on one release and over a year longer than 
we had originally planned but we were finished and it felt very, very good.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Well, almost finished. The first versions of Delphi were not due to hit the
streets until March 1st, 2 weeks after the 14th and we were in final stages of
signing-off the product but we were not finished just yet. There were a couple 
of “stop ship” bugs that needed to be fixed and, as I remember it, Marc 
Cousineau was the last man standing. He  missed that night. We missed him there 
but he took the last few bullets so the rest of us could enjoy ourselves. Thanks 
Marc!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;How did we know the features we chose were the right ones? How did we know we
were done?&lt;/p&gt;

&lt;p&gt;The biggest challenge of building a product is often not what to do, but 
figuring out what you don’t have to do. Of all the features you can build, which
should you build? Which can wait and which cannot? Even in `95, 6 months before 
the earth shake that was Windows ‘95, the Windows API was large and complicated.
What parts of the API should we wrap, which should we ignore?&lt;/p&gt;

&lt;p&gt;What we did was an application of &lt;a href=&quot;https://en.wikipedia.org/wiki/Pareto_principle&quot;&gt;the 80/20 rule&lt;/a&gt;, or as I learned learned
later was called the &lt;a href=&quot;https://en.wikipedia.org/wiki/Pareto_principle&quot;&gt;Pareto principle&lt;/a&gt;. Applying this to something like the
Windows API means that roughly only 20% of the API is used over 80% of the time.
This meant we didn’t have to wrap everything, we only had to figure out that 20%.
Or, as Anders likes to say, we needed to go for the biggest bang for the buck.&lt;/p&gt;

&lt;p&gt;But what about the other 80% of the API that is used 20% of the time? We knew
two things. First, we would never have time to wrap it all. We didn’t have the
people and we couldn’t keep up producing wrappers for things from a company as 
large an prolific as Microsoft. Second, no application could be written that was
worth much that didn’t, in some way, use part of that 80%. This lead to the 
principles,&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Make simple things simple.&lt;/li&gt;
  &lt;li&gt;Make the hard things possible.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The most straightforward example of this principle in action is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Graphics&lt;/code&gt;
unit with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TCanvas&lt;/code&gt; and its related classes. All of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Graphics&lt;/code&gt; classes that
wrap an underlying Windows API handle expose that handle through an aptly named 
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Handle&lt;/code&gt; property. This was unique at the time for a high-level encapsulation. 
The user is allowed to do just about anything with the handle and the underlying 
object would adjust accordingly. Anders and I spend a considerable amount of 
time enabling this; making the easy things easy, and hard  things possible. 
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TCanvas&lt;/code&gt; made using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DC&lt;/code&gt;s easy, exposing the underlying handle we used made 
calling APIs we didn’t wrap possible. You can see this principle applied 
throughout the VCL, the underlying handles are exposed to make the hard things 
possible, even though doing so wasn’t exactly easy.&lt;/p&gt;

&lt;p&gt;As I mentioned &lt;a href=&quot;http://removingalldoubt.com/programming/2015/07/10/a-component-of-the-story/&quot;&gt;previously&lt;/a&gt;, we intentionally made the VCL extensible. We 
didn’t have the people to wrap everything and wanted our users to be able to 
build or buy the components they needed to create their applications. Allowing 
components to be written in the same language they used to write the rest of 
the application means we lowered the bar considerably for application specific 
encapsulations.&lt;/p&gt;

&lt;p&gt;We began Delphi in early 1993 and we were locked and loaded for a release 
sometime in that winter. In fact, it didn’t take us that long to get something 
that looked substantially like Delphi up and running. I remember a demo we did 
in July of 1993 to a group of engineers showing them what we had at the time. It 
included the &lt;a href=&quot;http://removingalldoubt.com/programming/2015/07/07/first-class-class/&quot;&gt;component palette&lt;/a&gt;, form designer, and a rudimentary editor and 
even included what we later called two-way editing.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The editor was actually just a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TEdit&lt;/code&gt; so very rudimentary! By July it wasn’t 
just me and Anders anymore. We had decided to cancel work on what was going to 
be Borland Pascal 8.0 and focus completely on Delphi. Allen Bauer and Alex 
Shtaygrud began working on the debugger and editor (explaining why we were using 
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TEdit&lt;/code&gt;) and Dave Scofield began making my chicken scratches of an IDE look 
presentable. Ironic that a developer that spent most of his career building UI 
libraries and frameworks cannot create a decent looking UI to save his life.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After the meeting, I remember one of the engineers saying we should ship it and 
ship it now! We were encouraged but knew we had a lot of work to get us to a 
shippable state.&lt;/p&gt;

&lt;p&gt;Then came Visual Basic 3.0 and with it the 80% changed dramatically.&lt;/p&gt;

</description>
                <link>http://removingalldoubt.com/programming/2015/07/17/the-biggest-bang</link>
                <guid>http://removingalldoubt.com/programming/2015/07/17/the-biggest-bang</guid>
                <pubDate>2015-07-17T00:00:00+00:00</pubDate>
        </item>

        <item>
                <title>Will the true component please stand up</title>
                <description>
&lt;p&gt;There are only four words in computer science and component is one of them.&lt;/p&gt;

&lt;h1 id=&quot;components&quot;&gt;Components&lt;/h1&gt;

&lt;p&gt;When we used the term component in VCL we meant a class that was primarily 
defined by its properties, its methods, and its events. I believe it was Zack
Urlocker I heard stated it this clearly first but once I heard it I knew it was 
true. That is exactly what we meant by component.&lt;/p&gt;

&lt;h2 id=&quot;properties&quot;&gt;Properties&lt;/h2&gt;

&lt;p&gt;The state of the component is surfaced exclusively through its properties. The 
some of the state of the properties should reflect the complete state of the 
component (that is, little to no hidden state). The properties can be changed 
directly and the state of the component should immediately reflect that state or 
it should throw an exception, rejecting the change; however, it should only do 
the latter infrequently. Ideally the type of the property should be the only 
filter on the values of the property.&lt;/p&gt;

&lt;p&gt;All properties should be independent, or as independant as possible. Changing 
a property should rarely affect the value of another property. Sometimes it is
unavoidable but those cases should be minimized.&lt;/p&gt;

&lt;p&gt;This principle is enforced by the serialization system. A Delphi form is the
serialized state of a set of components, serializing the values of their 
properties. They are deserialized by constructing an instance and setting the 
properties, one by one, from the form file. Any state not surfaced as a property
will not be serialized. Coupling of property values can cause the deserialized
version of the component to be different from the originally serialized version.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Note this is not literally true as components, such as an image, have always
been able to store binary data not surfaced in a property but I am not one to
let the facts get in the way of a good story.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;By state above, I mean specifically the initalization state, or the 
configuration state, of the component; not the running state. Components can and 
should use encapsulation, just like any other well designed object.&lt;/p&gt;

&lt;h2 id=&quot;events&quot;&gt;Events&lt;/h2&gt;

&lt;p&gt;An event is just a property with a method pointer type. It is called out 
independently because the way the user interact with it is much different that 
other properties, since it is how the component signals the user of important 
events, but, deep down, it is just a property. There are special principles 
around events that make them easier to use, however.&lt;/p&gt;

&lt;p&gt;Events should be contract-less. This is hyperbole but, as demonstrated above,
I am a big fan of hyperbole. What I mean is that, in an event handler, the user
should be able to do anything they want; change focus, terminate the application,
destroy the component sending the event, anything. Any of these should never 
raise an exception or cause unexpected or undefined behavior. The event should
also never expect the user to do anything. That is, an empty event handler 
should always be legal. Logically, this means that event method pointers should
always be procedures, not functions (functions require a result, violating this
principle). An empty event handler must be treated semantically identically to 
having no event handler assigned. There are always going to be exceptions to 
these general principles (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OnFilter&lt;/code&gt; being one obvious example) but any event
should try to adhere to these general principles.&lt;/p&gt;

&lt;p&gt;This is the place where the contrast between a framework and a component library
is the most stark. A framework uses its virtual methods to ask questions the 
user is expected to supply answers to, such as, “What color would you like your 
hints today?”. A component, on the other hand, just tells you about interesting 
things you might want to know about such as, “By the way, the user pressed the 
letter A. Just thought you would like to know.” What you do with that 
information is strictly up to you.&lt;/p&gt;

&lt;h2 id=&quot;methods&quot;&gt;Methods&lt;/h2&gt;

&lt;p&gt;There is not much to say about methods, methods are verbs that tell the
component to do something. There is not much restriction on what that something
is.&lt;/p&gt;

&lt;h1 id=&quot;learning-after-the-fact&quot;&gt;Learning after the fact&lt;/h1&gt;

&lt;h2 id=&quot;properties-should-be-changeable&quot;&gt;Properties should be changeable&lt;/h2&gt;

&lt;p&gt;Sometimes you can really only learn what assumptions you are making when 
presented with a contrasting example. One assumption I had that I never wrote 
down or even consciously thought about was that the state of a component after a 
property change should be the same as had the component been born with that 
state. It is a bit subtle but important. For example, there should be no one-way 
trap-doors in a component’s state. If you add a border to a control, should just 
as easily remove it and it should appear as if it never had one. This was 
enforced by the form designer. The form designer creates real instances of the 
controls you are editing, not fakes. If you violate this principle the control 
will have weird behavior in the form designer.&lt;/p&gt;

&lt;p&gt;After leaving Borland I went to work at Microsoft on Avalon, later called the 
WPF (Windows Presentation Foundation) and even more recently, simply XAML. In 
contrast to the VCL, once a property is set in XAML, it is rarely ever changed. 
Once the view was created and displayed on the screen (that is, added to the
visual tree), properties would often become immutable. You can make changes in 
XAML that cannot be made at runtime. This made the framework perform well in a 
lot of situation but it made writing a designer for it, my job, hard. I 
stubbornly reported each one of these immutable properties as bugs and continued 
on my merry way writing the designer. It wasn’t until later, when all my bugs 
were marked “working as intended” that I realized my mistake. They rightly 
rejected my attempt to slow WPF down to make my life easier. XAML is not the VCL 
and I shouldn’t have treated it as if was.&lt;/p&gt;

&lt;h2 id=&quot;delphi-events-are-single-plex&quot;&gt;Delphi events are single-plex&lt;/h2&gt;

&lt;p&gt;Events are commonplace now and are seen in just about every user interface 
library and framework. It is only in Delphi, however, they are single-plex. That 
is, in Delphi an event has one and only one handler. That is because the type of 
the event is a method pointer, a pointer to one method.&lt;/p&gt;

&lt;p&gt;After using multiplex events in several frameworks, I don’t see them as an 
improvement. In Delphi, at least in 32-bit Delphi, a method pointer is 8 bytes, 
two pointers. It is a value, not an object. An just like other values is 
immutable. This allows the event to just be a property, as noted above. In 
systems with multi-plex events, they are treated quite differently from
properties. In .NET you have the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add_Event&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;remove_Event&lt;/code&gt; methods (which 
is what C#’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+=&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-=&lt;/code&gt; is translated into calling). In JavaScript you have
the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;addEventHandler&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;on&lt;/code&gt; or any number of patterns that mean the same 
thing. Each event must be able to handle a list of handlers and be able to 
dispatch to that list. In JavaScript, for example, there is an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EventEmitter&lt;/code&gt; 
class whose sole job is to track these values and make emitting events 
tolerable. One thing all these ignore is that having more than one 
handler for an event is rare to the point of non-existence. All add significant 
overhead to the simple event handler just to handle this one case, dispatching
to multiple handlers. There are better ways to handle multi-plexing events than 
to make every event a boat anchor. Single-plex events have their own problems
but I still believe multi-plex events are not worth their cost.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/2015/07/13/will-the-true-component-please-stand-up</link>
                <guid>http://removingalldoubt.com/programming/2015/07/13/will-the-true-component-please-stand-up</guid>
                <pubDate>2015-07-13T00:00:00+00:00</pubDate>
        </item>

        <item>
                <title>A component of the story</title>
                <description>
&lt;p&gt;The VCL, the Visual &lt;em&gt;Component&lt;/em&gt; Library, is not an object oriented framework.
It never has been. In fact, when I designed it, I made sure it wasn’t because
I had lost my taste for them having written two, Turbo Vision and OWL. I loved
Turbo Vision but, as Jeff Duntemann told us, we put the front door on the second
floor. You had to understand everything before you could understand anything. It 
was great at what it did but if you needed to learn and buy into every 
assumption we made or it would fight you the whole way.&lt;/p&gt;

&lt;p&gt;In many ways, OWL (ObjectWindows Library) was even worse. We tried to learn from
our mistakes with Turbo Vision but we made too many new ones. OWL was intended 
as a thin wrapper on the Windows API that would simplify and highlight the 
underlying object oriented nature of Windows with language support. 
Unfortunately, approached that way, Windows is a terrible example of 
object-oriented design and all we ended up doing was making the flaws more 
apparent. The C++ version of OWL did a better job of hiding the flaws but, by
the time Borland started work on that, I was already in the middle of the VCL.&lt;/p&gt;

&lt;p&gt;After two frameworks I saw the flaw in the very nature of frameworks. A 
framework is like a fill-in-the-blanks form with a lot of little places where
the framework designer allows the user to fill in code. If the framework 
resembles the finished application you want, you are golden. If it doesn’t, pick
another framework. It was that all or nothing, the framework is king, model that
I found distasteful. The user of a framework extends the framework through 
inheritance and the blanks they fill in are virtual methods. If you think of the 
framework as a tree data structure, users are extending at the leaves, at the 
bottom of the tree. The framework is on top, the user extends at the bottom at 
whatever leaves the framework designer deemed worthy.&lt;/p&gt;

&lt;p&gt;With the VCL we wanted to start fresh and that meant I was not doing a 
framework. I didn’t want give the user a set of blanks to fill in, but,
rather, give them a toolbox full of useful tools and supplies they could fit 
together or ignore completely as they saw fit. I wanted the user writing 
the application to be in charge, not me. I wanted the user on top of the tree 
and the components at the leaves.&lt;/p&gt;

&lt;p&gt;Some principles of the design of the VCL I came up with are,&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The users should feel they are in control at all times.&lt;/li&gt;
  &lt;li&gt;Components should appear independent and self-contained.&lt;/li&gt;
  &lt;li&gt;Inheritance should not be required to construct an application.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To do this, in my mind I separated users into two camps; component writers and 
component users. Often they were the same person but it was a useful separation. 
Component users should be able to be successful constructing a full application 
with just the components they have and writing code to stitch them together. 
They should never have to know or care about object-oriented concepts like 
encapsulation, inheritance, or polymorphism.&lt;/p&gt;

&lt;p&gt;The component writer, however, would care very deeply about object-oriented
programming. A component, after all, is a class that inherits from one of the
fundamental base classes. But once the component is complete, the inheritance 
it used for implementation should be a bit of interesting trivia, not the core 
of its being. From my perspective, the user of a component shouldn’t need to 
know or care what its ancestor is, just what properties, methods and events it 
has. The fact it had an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OnMouseDown&lt;/code&gt; event, just like the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OnMouseDown&lt;/code&gt; event 
of some other component was a happy accident not because they both inherited 
from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TControl&lt;/code&gt;. In reality, they both needed to inherit from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TControl&lt;/code&gt; but 
that should only be important to the component writer, not the component user. 
The Delphi 1.0 documentation reflected this philosophy; something it lost over 
time, unfortunately.&lt;/p&gt;

&lt;p&gt;The name of the VCL, Visual &lt;em&gt;Component&lt;/em&gt; Library, is no accident. It is a 
library of &lt;em&gt;components&lt;/em&gt; not a framework. It contains a framework, a framework to
enable writing components, but it is not a framework itself.&lt;/p&gt;

&lt;p&gt;The focus on components also made the third-party ecosystem around Delphi very
clear. The plan was for users to be able to purchase third-party sets of 
components that would fit into Delphi like they were natives; which, in a way, 
they were. There is little difference between a first- and third-party component 
in Delphi as they are both built with the same tools. This was intentional from 
the very start and we believed would be the key to Delphi’s success.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/2015/07/10/a-component-of-the-story</link>
                <guid>http://removingalldoubt.com/programming/2015/07/10/a-component-of-the-story</guid>
                <pubDate>2015-07-10T00:00:00+00:00</pubDate>
        </item>

        <item>
                <title>The Delphi Event</title>
                <description>
&lt;p&gt;During the design of Delphi, one of the early lines of code we wrote on a 
whiteboard that we wanted to get working was,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Button.OnClick := ClickHandler;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That seems natural enough now but Turbo Pascal didn’t have method pointers
before Delphi; where did they come from?&lt;/p&gt;

&lt;p&gt;The advent of Visual Basic was quite a shot across the bow for Borland. At the
time, we were the hallmark of innovation and the champions of the developer.
Visual Basic changed that. It made what we were working on at the time, Turbo
Pascal for Windows, look clunky and dated. When TPW was released we received 
strong feedback from our users that Visual Basic was better and much easier to
use. It was back to the drawing board for us.&lt;/p&gt;

&lt;p&gt;Our manager at the time, Rick Schell, gave us a mandate, “Create Visual Foo that
would be more Visual than Visual Basic!” and set both the C++ and Pascal teams
on that mission. We worked several months trying to come up with something that
would fit the bill but nothing really gelled. I learned a lot during that time
but after months of trying stuff that was more visual, certainly, but never 
easier to use than Visual Basic, we weren’t left with much. What we did have
was a Pascal compiler that had method pointers because one of our prototypes
needed it.&lt;/p&gt;

&lt;p&gt;I distinctly remember one day in early 1992, Anders and I were out in the 
parking lot behind Borland discussing the project and Visual Basic. We talked 
about how method pointers were all we needed to do everything that Visual 
Basic did. I listened as he sketched a design of something that would eventually 
be Delphi but we called it Visual Pascal at the time. We floated the idea around 
and Gary Whizen got very excited about, as I recall; but he came back from a 
meeting and was told, in no uncertain terms, that we should not build such a 
thing. He tried to float the idea several times after that and was shot down 
every time.&lt;/p&gt;

&lt;p&gt;After that I got a bit frustrated with our progress and left the effort and
convinced Gary that we should put out another version of Turbo Pascal to help
pay the bills. He agreed and we started working on combining the DOS and Windows
versions of Turbo Pascal, plus the extended memory features from Borland C++, 
into what became Borland Pascal 7.0. Anders was still working on the Visual Foo 
project but I cajoled him into adding some features and fixing some bugs in the 
compiler; work which we kept hidden from everyone above Gary. I believe Borland
Pascal 7.0 was the best DOS development environment ever written. We released it
just in time for nobody to care as Windows became the only game in town.&lt;/p&gt;

&lt;p&gt;In the spring of 1993 things changed. During a big layoff, Rick Schell left the 
company for a small startup company called Netscape and the Visual Foo mandate
left with him. In the midst of the shake-up, in a “get to know the new boss” 
meeting we asked the new VP if we could build Visual Pascal. He said yes! and 
off we went. We were not sure if he knew what we were asking but we didn’t want 
to look a gift horse in the mouth.&lt;/p&gt;

&lt;p&gt;As Anders surmised a year earlier, the method pointer, or, more correctly, the 
bound method pointer, was exactly what we needed to build Delphi. It wasn’t the 
only thing we needed but was one of the fundamental changes we made to Turbo 
Pascal that enabled building the VCL.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/2015/07/08/the-delphi-event</link>
                <guid>http://removingalldoubt.com/programming/2015/07/08/the-delphi-event</guid>
                <pubDate>2015-07-08T00:00:00+00:00</pubDate>
        </item>

        <item>
                <title>First-class class</title>
                <description>
&lt;p&gt;When building the first prototypes of what became the Delphi form designer I
needed to solve a very simple problem, a palette of controls. When one of the 
palette items was selected and the form design was clicked, I needed to create 
control on the form in the location clicked. The problem was, how do you create 
such a list?&lt;/p&gt;

&lt;p&gt;The solution we choose was to allow a reference to a class be treated as a 
first class value; a class reference. If you have a class called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; then the
identifier &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; is considered a constant reference to the class of type 
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;class of A&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Class references follow inheritance substitution rules. For example, if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt; 
derives &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; then a reference to class &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt; can be passed when a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;class of A&lt;/code&gt;
reference is expected. This allowed us to create &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TComponent&lt;/code&gt;, the base class
of all Delphi components, and the corresponding class reference type 
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TComponentClass&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With a class reference you can call any class method or constructor declared on 
that class. If the constructor or method is virtual, the most derived 
constructor or method is called. Given the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt; classes above, the 
statements:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;var
  SomeClass: class of A;
  SomeA: A;
begin
  SomeClass := B;
  SomeA := SomeClass.Create();
end;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;create an instance of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt; class which derives from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt;. This allowed us to
use an array of class references as the palette data structure. Something like,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;type
  TPaletteItems = array of TComponentClass;

var
  PaletteItems: TPaletteItems = [TButton, TEdit, TCheckBox, TLabel];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Selecting an item in the palette was simply selecting an index into that array 
which selected the cooresponding class reference of which to construct an 
instance.&lt;/p&gt;

&lt;p&gt;The early prototypes and early pre-release versions of Delphi I just hard-coded 
the array of classes as I was the only one writting them at the time. Eventually 
we allowed the array to be modified by calling a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Register&lt;/code&gt; procedure.&lt;/p&gt;

&lt;p&gt;The class reference was also useful for form deserialization as it allowed us to
construct the correct class based on a registered class name but the genisis
of the class reference was the palette of the form designer.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/2015/07/07/first-class-class</link>
                <guid>http://removingalldoubt.com/programming/2015/07/07/first-class-class</guid>
                <pubDate>2015-07-07T00:00:00+00:00</pubDate>
        </item>

        <item>
                <title>Properties on purpose</title>
                <description>
&lt;p&gt;The development of the Visual Class Library started rather simple, it began with 
a few simple statements on the white board in Anders’ office. The details are a
bit fuzzy after so much time but they looked something like this,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Button.Caption := &apos;OK&apos;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;we then added,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Button.OnClick := OkClickHandler;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;to be able to be able to do something when the button was clicked.&lt;/p&gt;

&lt;p&gt;Later we added things like,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Button.Font.Name := &apos;Arial&apos;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;to change the font of the button.&lt;/p&gt;

&lt;p&gt;We wanted this code to work. This is usually how I like to design things. Start 
by writting some code down that should work and then begin brainstorming ways it 
can; poking holes in those designs along the way. Anders and I began to do just 
that.&lt;/p&gt;

&lt;p&gt;We wanted to avoid requiring an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Update&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Realize&lt;/code&gt;, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Invalidate&lt;/code&gt; method to 
be called to make the changes show up on the screen. That is, we wanted the 
caption of the button to change immediately when the assignment was made with no 
futher intervention.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;As a side note, we wanted to avoid the state problem we had introduced in OWL,
our previous class library. In OWL objects had several states they could
be in from constructed, to initialized, to having a window class, to having a 
window handle, etc. Each state enabled different operations, all of this was 
rather difficult to keep in your head. With the VCL, we wanted no states. We 
wanted the button object to be the button, not a remote control of a button. In
other words, the button’s caption property was the caption of the button not a 
copy of it that will eventually be the caption of the window handle the object 
was wrapping.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This implied that the assignment had to have a side-effect; it would eventually 
need to call the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SetText()&lt;/code&gt; Windows function. Side-effects are not generally 
considered kosher for assignments (other than the asignment itself); but we knew 
it would solve our problem.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;I now know of several ways we could have avoided side-effects but either none 
occurred to us at the time or I just don’t remember discussing them.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We then began brainstorming some syntax to represent it. We decided that we
would create a new object member called a property that would then have a 
read and a write method associated with it. It would look something like,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;property Caption: String read GetCaption write SetCaption;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;where the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SetCaption&lt;/code&gt; method would be called on assignment and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GetCaption&lt;/code&gt;
method would be called when retrieving the caption. This allowed us to call
a function to get the caption from the window handle intead of being forced to
keep a copy of the string just for the get method. Later, we relaxed the read
clause to take a field name if the data was actually in the object, such as,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;property Name: String read FName write SetName;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This allowed more consise read clauses as well as allowed us to put off 
inlining simple get methods which would have complicated the compiler.&lt;/p&gt;

&lt;p&gt;Later we realized the serialization of the objects can be through their
properties; but that is propbably a story best left for another time.&lt;/p&gt;

&lt;p&gt;In adding properties to Pascal, I am sure we had some influences but don’t 
recall any other language having a similar construct. The closest at the time 
was Visual Basics component properties, which is probably the genisis of the 
name. However, you could not declare them in Basic, you could only access them 
from Basic.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/2015/07/05/properties-on-purpose</link>
                <guid>http://removingalldoubt.com/programming/2015/07/05/properties-on-purpose</guid>
                <pubDate>2015-07-05T00:00:00+00:00</pubDate>
        </item>

        <item>
                <title>Moving Again</title>
                <description>&lt;p&gt;I have moved my blog once again. It has been hosted on an &lt;a href=&quot;http://azure.microsoft.com/&quot;&gt;Azure&lt;/a&gt; instance
for about 2 years now. In that time I have had exactly one post, the one
describing that I was now running my blog on the Azure instance using
BlogEngine.net.&lt;/p&gt;

&lt;p&gt;I have now moved once again, but this time I am using &lt;a href=&quot;http://jekyllrb.com/&quot;&gt;Jeykll&lt;/a&gt;. There are
several advantages to running on &lt;a href=&quot;http://jekyllrb.com/&quot;&gt;Jeykll&lt;/a&gt; and &lt;a href=&quot;https://github.com/&quot;&gt;GitHub&lt;/a&gt;. One, it is
free. Second, the blog entires are under version control. Third, I can write
my posts in Markdown instead of of authoring them in raw html. Fourth, I can
preview changes to my blog locally before pushing them live.&lt;/p&gt;

&lt;p&gt;I might have been able to do these things with BlogEngine.net, I don’t know. I
never bothered to find out. I never found the blog engine convienent to author
posts in and it posed another barrier to my blogging regularly.&lt;/p&gt;

&lt;p&gt;In the move to BlogEngine.net I, unfortunately, lost all the comments for my
blog. This is partically unfortunate for &lt;a href=&quot;post/2006/09/19/Fatherly-Advice-To-New-Programmers.aspx&quot;&gt;Fatherly Advice To New Programmers&lt;/a&gt;,
my most popular and well received blog post.&lt;/p&gt;

&lt;p&gt;I shut down comments to my blog at the time I moved to BlogEngine.net and
&lt;a href=&quot;http://azure.microsoft.com/&quot;&gt;Azure&lt;/a&gt; because I spent most of my time deleting spam comments than actually
writing blog entires. I have re-enabled them with the move to Jekyll by using
&lt;a href=&quot;https://disqus.com/&quot;&gt;disqus&lt;/a&gt; as my comment server. Hopefully I will be able to manage my comments
better and get less comment spam.&lt;/p&gt;

</description>
                <link>http://removingalldoubt.com/blog/2015/01/24/moving-again</link>
                <guid>http://removingalldoubt.com/blog/2015/01/24/moving-again</guid>
                <pubDate>2015-01-24T00:00:00+00:00</pubDate>
        </item>

        <item>
                <title>New address</title>
                <description>&lt;div class=&quot;text&quot;&gt;&lt;p&gt;This blog&amp;nbsp;had a&amp;nbsp;good, long run on &lt;a href=&quot;http://www.pocketsilicon.com/&quot;&gt;Brian Pepin&lt;/a&gt;&amp;nbsp;servers he was gracious enough to host in is house. Brian&apos;s house, however,&amp;nbsp;is going through some renovations so he&amp;nbsp;decided to move his own web servers to Azure and shutdown his homegrown server farm. That means my free-loading days are over! I needed to do what I should have done a long time ago; move my blog to Azure.&lt;/p&gt;
&lt;p&gt;If you see this post that means my blog has successfully moved! Onward and upward!&lt;/p&gt;&lt;/div&gt;
</description>
                <link>http://removingalldoubt.com/blog/2013/02/07/New-address</link>
                <guid>http://removingalldoubt.com/blog/2013/02/07/New-address</guid>
                <pubDate>2013-02-07T08:20:00+00:00</pubDate>
        </item>

        <item>
                <title>Simulating INumeric with policies</title>
                <description>&lt;div class=&quot;text&quot;&gt;&lt;p&gt;After reading &lt;a href=&quot;http://blogs.msdn.com/lucabol&quot;&gt;Luca Bolognese&apos;s&lt;/a&gt;
blog post regarding using
&lt;a href=&quot;http://blogs.msdn.com/lucabol/archive/2009/02/05/simulating-inumeric-with-dynamic-in-c-4-0.aspx&quot;&gt;
C# 4.0&apos;s dynamic keyword to simulate INumeric&lt;/a&gt; I immediately thought that must be a way to express
this with generics instead. Generics, however, do not allow you to abstract over operators (which is what
is is needed to really do &lt;code&gt;INumeric&lt;/code&gt;) but you can simulate it using adapters as described in
&lt;a href=&quot;http://www.removingalldoubt.com/PermaLink.aspx/f712e487-a6f5-4a65-a718-5ce580f399a8&quot;&gt;
this post&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Say I want to write a general average routine that is generic over what type I am using to store the data.
That is, I want one routine that works equally for &lt;code&gt;&lt;strong&gt;double&lt;/strong&gt;&lt;/code&gt; as well as &lt;code&gt;&lt;strong&gt;decimal&lt;/strong&gt;&lt;/code&gt;.
What I want to be able to write is something like,&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; T Average(&lt;strong&gt;params&lt;/strong&gt; T[] values) {
    T total = 0;

    &lt;strong&gt;foreach&lt;/strong&gt; (&lt;strong&gt;var&lt;/strong&gt; v &lt;strong&gt;in&lt;/strong&gt; values)
        total = total + v;

    &lt;strong&gt;return&lt;/strong&gt; total / values.Length;
}&lt;/pre&gt;
&lt;p&gt;where the &lt;code&gt;T&lt;/code&gt; is any type that supports adding and dividing. I can&apos;t get this code to
compile exactly but we can get surprisingly close! My final version looks like,&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;strong&gt;class&lt;/strong&gt; Math&amp;lt;T, A&amp;gt; : PolicyUser&amp;lt;T, A&amp;gt; &lt;strong&gt;where&lt;/strong&gt; A : &lt;strong&gt;struct&lt;/strong&gt;, IOperatorPolicy&amp;lt;T&amp;gt; {
    &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; T Average(&lt;strong&gt;params&lt;/strong&gt; T[] values) {
        &lt;strong&gt;var&lt;/strong&gt; total = L(0);

        &lt;strong&gt;foreach&lt;/strong&gt; (&lt;strong&gt;var&lt;/strong&gt; v &lt;strong&gt;in&lt;/strong&gt; values)
            total = total + v;

        &lt;strong&gt;return&lt;/strong&gt; total / values.Length;
    }
}&lt;/pre&gt;
&lt;p&gt;which is much closer to the original than I thought originally could get!&lt;/p&gt;
&lt;p&gt;I started by cloning the operator policy and added a &lt;code&gt;FromInt()&lt;/code&gt; I will explain below,&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;strong&gt;interface&lt;/strong&gt; IOperatorPolicy&amp;lt;T&amp;gt; {
    T Add(T a, T b);
    T Subtract(T a, T b);
    T Multiply(T a, T b);
    T Divide(T a, T b);
    T FromInt(&lt;strong&gt;int&lt;/strong&gt; value);
}
&lt;/pre&gt;
&lt;p&gt;Then I cloned the &lt;code&gt;&lt;strong&gt;double&lt;/strong&gt;&lt;/code&gt; policy struct and then added a &lt;code&gt;&lt;strong&gt;decimal&lt;/strong&gt;&lt;/code&gt; mainly by cut/paste/search/replace&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;strong&gt;struct&lt;/strong&gt; DoubleOperatorPolicy : IOperatorPolicy&amp;lt;&lt;strong&gt;double&lt;/strong&gt;&amp;gt; {
    &lt;strong&gt;double&lt;/strong&gt; IOperatorPolicy&amp;lt;&lt;strong&gt;double&lt;/strong&gt;&amp;gt;.Add(&lt;strong&gt;double&lt;/strong&gt; a, &lt;strong&gt;double&lt;/strong&gt; b) { &lt;strong&gt;return&lt;/strong&gt; a + b; }
    &lt;strong&gt;double&lt;/strong&gt; IOperatorPolicy&amp;lt;&lt;strong&gt;double&lt;/strong&gt;&amp;gt;.Subtract(&lt;strong&gt;double&lt;/strong&gt; a, &lt;strong&gt;double&lt;/strong&gt; b) { &lt;strong&gt;return&lt;/strong&gt; a - b; }
    &lt;strong&gt;double&lt;/strong&gt; IOperatorPolicy&amp;lt;&lt;strong&gt;double&lt;/strong&gt;&amp;gt;.Multiply(&lt;strong&gt;double&lt;/strong&gt; a, &lt;strong&gt;double&lt;/strong&gt; b) { &lt;strong&gt;return&lt;/strong&gt; a * b; }
    &lt;strong&gt;double&lt;/strong&gt; IOperatorPolicy&amp;lt;&lt;strong&gt;double&lt;/strong&gt;&amp;gt;.Divide(&lt;strong&gt;double&lt;/strong&gt; a, &lt;strong&gt;double&lt;/strong&gt; b) { &lt;strong&gt;return&lt;/strong&gt; a / b; }
    &lt;strong&gt;double&lt;/strong&gt; IOperatorPolicy&amp;lt;&lt;strong&gt;double&lt;/strong&gt;&amp;gt;.FromInt(&lt;strong&gt;int&lt;/strong&gt; value) { &lt;strong&gt;return&lt;/strong&gt; value; }
}

&lt;strong&gt;struct&lt;/strong&gt; DecimalOperatorPolicy : IOperatorPolicy&amp;lt;&lt;strong&gt;decimal&lt;/strong&gt;&amp;gt; {
    &lt;strong&gt;decimal&lt;/strong&gt; IOperatorPolicy&amp;lt;&lt;strong&gt;decimal&lt;/strong&gt;&amp;gt;.Add(&lt;strong&gt;decimal&lt;/strong&gt; a, &lt;strong&gt;decimal&lt;/strong&gt; b) { &lt;strong&gt;return&lt;/strong&gt; a + b; }
    &lt;strong&gt;decimal&lt;/strong&gt; IOperatorPolicy&amp;lt;&lt;strong&gt;decimal&lt;/strong&gt;&amp;gt;.Subtract(&lt;strong&gt;decimal&lt;/strong&gt; a, &lt;strong&gt;decimal&lt;/strong&gt; b) { &lt;strong&gt;return&lt;/strong&gt; a - b; }
    &lt;strong&gt;decimal&lt;/strong&gt; IOperatorPolicy&amp;lt;&lt;strong&gt;decimal&lt;/strong&gt;&amp;gt;.Multiply(&lt;strong&gt;decimal&lt;/strong&gt; a, &lt;strong&gt;decimal&lt;/strong&gt; b) { &lt;strong&gt;return&lt;/strong&gt; a * b; }
    &lt;strong&gt;decimal&lt;/strong&gt; IOperatorPolicy&amp;lt;&lt;strong&gt;decimal&lt;/strong&gt;&amp;gt;.Divide(&lt;strong&gt;decimal&lt;/strong&gt; a, &lt;strong&gt;decimal&lt;/strong&gt; b) { &lt;strong&gt;return&lt;/strong&gt; a / b; }
    &lt;strong&gt;decimal&lt;/strong&gt; IOperatorPolicy&amp;lt;&lt;strong&gt;decimal&lt;/strong&gt;&amp;gt;.FromInt(int value) { &lt;strong&gt;return&lt;/strong&gt; value; }
}&lt;/pre&gt;
&lt;p&gt;The cleaver bit is to define a base type that contains a &lt;code&gt;struct&lt;/code&gt; that defines the operators
as overloads. The overloads use a policy &lt;code&gt;&lt;strong&gt;struct&lt;/strong&gt;&lt;/code&gt; to implement that overloaded operators. This &lt;code&gt;
&lt;strong&gt;struct&lt;/strong&gt;&lt;/code&gt;
also defines implicit conversion operators to hide some (but not all) of the messiness introduced by using this
wrapper type. The &lt;code&gt;PolicyUser&lt;/code&gt; class looks like,&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;strong&gt;class&lt;/strong&gt; PolicyUser&amp;lt;T, A&amp;gt; &lt;strong&gt;where&lt;/strong&gt; A: &lt;strong&gt;struct&lt;/strong&gt;, IOperatorPolicy&amp;lt;T&amp;gt; {
    &lt;strong&gt;static&lt;/strong&gt; A Policy = &lt;strong&gt;new&lt;/strong&gt; A();

    &lt;strong&gt;protected&lt;/strong&gt; &lt;strong&gt;struct&lt;/strong&gt; Value {
        &lt;strong&gt;public&lt;/strong&gt; T V;

        &lt;strong&gt;public&lt;/strong&gt; Value(T v) { V = v; }

        &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; Value &lt;strong&gt;operator&lt;/strong&gt; +(Value a, Value b) { &lt;strong&gt;return&lt;/strong&gt; &lt;strong&gt;new&lt;/strong&gt; Value(Policy.Add(a.V, b.V)); }
        &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; Value &lt;strong&gt;operator&lt;/strong&gt; -(Value a, Value b) { &lt;strong&gt;return&lt;/strong&gt; &lt;strong&gt;new&lt;/strong&gt; Value(Policy.Subtract(a.V, b.V)); }
        &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; Value &lt;strong&gt;operator&lt;/strong&gt; *(Value a, Value b) { &lt;strong&gt;return&lt;/strong&gt; &lt;strong&gt;new&lt;/strong&gt; Value(Policy.Multiply(a.V, b.V)); }
        &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; Value &lt;strong&gt;operator&lt;/strong&gt; /(Value a, Value b) { &lt;strong&gt;return&lt;/strong&gt; &lt;strong&gt;new&lt;/strong&gt; Value(Policy.Divide(a.V, b.V)); }
        &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;strong&gt;implicit&lt;/strong&gt; &lt;strong&gt;operator&lt;/strong&gt; Value(T v) { &lt;strong&gt;return&lt;/strong&gt; &lt;strong&gt;new&lt;/strong&gt; Value(v); }
        &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;strong&gt;implicit&lt;/strong&gt; &lt;strong&gt;operator&lt;/strong&gt; Value(&lt;strong&gt;int&lt;/strong&gt; v) { &lt;strong&gt;return&lt;/strong&gt; &lt;strong&gt;new&lt;/strong&gt; Value(Policy.FromInt(v)); }
        &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;strong&gt;implicit&lt;/strong&gt; &lt;strong&gt;operator&lt;/strong&gt; T(Value v) { &lt;strong&gt;return&lt;/strong&gt; v.V; }
    }

    &lt;strong&gt;protected&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; Value L(&lt;strong&gt;int&lt;/strong&gt; value) { &lt;strong&gt;return&lt;/strong&gt; &lt;strong&gt;new&lt;/strong&gt; Value(Policy.FromInt(value)); }
}&lt;/pre&gt;
&lt;p&gt;What the &lt;code&gt;Value&lt;/code&gt; struct does is allows you to use the type &lt;code&gt;Value&lt;/code&gt; in place of the type &lt;code&gt;T&lt;/code&gt; whenever you want to
use operators. When operators are used you only need to ensure one sub-expression is promoted to &lt;code&gt;Value&lt;/code&gt; and the implict operator will
take care of the rest.&lt;/p&gt;
&lt;p&gt;One remaining oddness is using literals. Implicit operators have there limits which makes using literal a bit strange. This was also odd in the
&lt;a href=&quot;http://www.removingalldoubt.com/PermaLink.aspx/f712e487-a6f5-4a65-a718-5ce580f399a8&quot;&gt;Policy&lt;/a&gt; post as well and I used the same trick to make it a little less cumbersome. I introduced
a static method &lt;code&gt;L()&lt;/code&gt; that takes an integer and converts it to &lt;code&gt;Value&lt;/code&gt; allowing you to use integer literals by calling &lt;code&gt;FromInt()&lt;/code&gt; I introduced earlier. You can
extend to this to allow other types by repeating the pattern for, say &lt;code&gt;&lt;strong&gt;double&lt;/strong&gt;&lt;/code&gt;, allowing you to use &lt;code&gt;&lt;strong&gt;double&lt;/strong&gt;&lt;/code&gt; literals as well. I didn&apos;t
because I didn&apos;t need it for my example (but probably will if I try to implement something more complicated.
&lt;/p&gt;
&lt;p&gt;To bind the actual data type to provide the data type and the policy. To make this simpler I created two create implementations of &lt;code&gt;Math&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;  &lt;strong&gt;class&lt;/strong&gt; DoubleMath : Math&amp;lt;&lt;strong&gt;double&lt;/strong&gt;, DoubleOperatorPolicy&amp;gt; { }
  &lt;strong&gt;class&lt;/strong&gt; DecimalMath : Math&amp;lt;&lt;strong&gt;decimal&lt;/strong&gt;, DecimalOperatorPolicy&amp;gt; { }
&lt;/pre&gt;
&lt;p&gt;Calling the average method looks like,&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;    &lt;strong&gt;var&lt;/strong&gt; resultDouble = DoubleMath.Average(1, 2, 3, 4, 5);
    &lt;strong&gt;var&lt;/strong&gt; resultDecimal = DecimalMath.Average(1, 2, 3, 4, 5);
&lt;/pre&gt;
&lt;p&gt;It should be straight forward to see how this could be adapted to a &lt;code&gt;Financial&lt;/code&gt; class, as Luca was trying to build, and how the routines could
be made independent of the data type used in the calculations.&lt;/p&gt;
&lt;p&gt;There you have it. Policies can be used to simulate &lt;code&gt;INumeric&lt;/code&gt; without having to resort to using &lt;code&gt;&lt;strong&gt;dynamic&lt;/strong&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Edit: Mar 9, 2009: Fixed HTML formatting error
&lt;/p&gt;&lt;/div&gt;
</description>
                <link>http://removingalldoubt.com/programming/2009/03/06/Simulating-INumeric-with-policies</link>
                <guid>http://removingalldoubt.com/programming/2009/03/06/Simulating-INumeric-with-policies</guid>
                <pubDate>2009-03-06T19:22:00+00:00</pubDate>
        </item>

        <item>
                <title>Zero the value is not</title>
                <description>&lt;div class=&quot;text&quot;&gt;&lt;p&gt;One of my pet-peeves is code written like this,&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;strong&gt;if&lt;/strong&gt; (0 != value) { ... }&lt;/pre&gt;

&lt;p&gt;This reads backwards to me. I hear echoes of
&lt;a href=&quot;http://en.wikipedia.org/wiki/Frank_Oz&quot;&gt;Frank Oz&lt;/a&gt; in my head, &quot;if zero
the value is not&quot;. As cute as this dialect is coming from a pointy-eared muppet,
it still takes a bit to translate back into the more common English phrasing,
&quot;if the value is not zero&quot;. It seems programmers who insist on writing in this style
have forgotten that their source should be readable as well as executable. Don&apos;t
force your readers to have to do grammar gymnastics as well as having to
decipher your algorithmic gynmastics.&lt;/p&gt;
&lt;p&gt;Once upon a time, this was considered good style in order to avoid accidentally
writing something like,&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;strong&gt;if&lt;/strong&gt; (value = 0) { ... }&lt;/pre&gt;

&lt;p&gt;In C#, this is illegal. In most (all?) modern C and C++ compilers, this will
generate a warning. (You do compile with warnings reported as errors right?)
There ceases to be a good reason to write test expressions backwards. Let&apos;s let
this style rest in peace.&lt;/p&gt;
&lt;p&gt;Even if you think the above form has some redeeming value, let&apos;s all agree
that any programmer that voluntarily writes something like,&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;strong&gt;if&lt;/strong&gt; (&lt;strong&gt;false&lt;/strong&gt; == value) { ... }&lt;/pre&gt;

&lt;p&gt;should have an electric shock sent directly through their keyboard.&lt;/p&gt;
&lt;/div&gt;
</description>
                <link>http://removingalldoubt.com/programming/2008/10/31/Zero-the-value-is-not</link>
                <guid>http://removingalldoubt.com/programming/2008/10/31/Zero-the-value-is-not</guid>
                <pubDate>2008-10-31T06:32:00+00:00</pubDate>
        </item>

        <item>
                <title>BeginInvoke's last parameter: What's in a name?</title>
                <description>&lt;p&gt;I have been playing around with asynchronous programming lately and it 
bothers me that the last parameter to the &lt;em&gt;begin invoke&lt;/em&gt; pattern doesn&apos;t
have a name that everyone agrees on. The &lt;em&gt;begin invoke&lt;/em&gt; pattern is the
asynchronous calling pattern were the &lt;code&gt;BeginXxxx()&lt;/code&gt;, such as &lt;code&gt;
Stream.BeginRead()&lt;/code&gt;, takes a set of parameters, a callback method, and
some, well, thing, at the end that is carried along with the asynchronous
operation that eventually finds it way into the &lt;code&gt;IAsyncResult&lt;/code&gt;. The
problem is what do we call that thing? In an informal survey of the methods in
the BCL that implement this pattern I have found a wide variation. Here is a
partial list in somewhat order of popularity,&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;object&lt;/li&gt;
	&lt;li&gt;stateObject&lt;/li&gt;
	&lt;li&gt;state&lt;/li&gt;
	&lt;li&gt;asyncState&lt;/li&gt;
	&lt;li&gt;extraData&lt;/li&gt;
	&lt;li&gt;data&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There seems to be little agreement about what to call it. We could pick the
most prevalent but the name &lt;code&gt;
object&lt;/code&gt; occurs the most often because every
delegate gets a &lt;code&gt;BeginInvoke()&lt;/code&gt; method created for it and in this
automatically generated code the parameter is called &lt;code&gt;object&lt;/code&gt;. We can&apos;t standardize on &lt;code&gt;object&lt;/code&gt; because it is a reserved word in several languages so either it would be impossible to
specify or awkward (i.e. &lt;code&gt;@object&lt;/code&gt; in C#). What I would like is a
name that we can all use and we can all agree on.&lt;/p&gt;
&lt;p&gt;To name the thing, we first must understand why it it is there at all. It
exists to hold some state on behalf of the caller. Having the state object makes
programming against the &lt;em&gt;begin invoke&lt;/em&gt; pattern easier in languages that
do not have anonymous methods that capture local variables. Consider the
following program (which intentionally ignores errors because it is only an
example),&lt;/p&gt;
&lt;pre&gt;&lt;b&gt;const&lt;/b&gt; &lt;b&gt;string&lt;/b&gt; uriFormat =
    &quot;http://messenger.services.live.com/users/{0}@apps.messenger.live.com/presence&quot;;
&lt;b&gt;const&lt;/b&gt; &lt;b&gt;string&lt;/b&gt; responsePattern =
    @&quot;&quot;&quot;statusText&quot;&quot;\s*:\s*&quot;&quot;(?&amp;lt;status&amp;gt;\w+).*&quot;&quot;displayName&quot;&quot;\s*:\s*&quot;&quot;(?&amp;lt;name&amp;gt;\w+)&quot;&quot;&quot;;

&lt;b&gt;static&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Main(&lt;b&gt;string&lt;/b&gt;[] args) {
    &lt;b&gt;var&lt;/b&gt; uri = &lt;b&gt;string&lt;/b&gt;.Format(uriFormat, args[0]);
    &lt;b&gt;var&lt;/b&gt; request = WebRequest.Create(uri);
    &lt;b&gt;var&lt;/b&gt; result = request.BeginGetResponse(PresenceCallback, request);

    // Other interesting work....

    result.AsyncWaitHandle.WaitOne();
}

&lt;b&gt;private&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; PresenceCallback(IAsyncResult result) {
    &lt;b&gt;var&lt;/b&gt; request = result.AsyncState &lt;b&gt;as&lt;/b&gt; WebRequest;
    &lt;b&gt;var&lt;/b&gt; response = request.EndGetResponse(result);
    &lt;b&gt;var&lt;/b&gt; s = &lt;b&gt;new&lt;/b&gt; StreamReader(response.GetResponseStream()).ReadToEnd();
    &lt;b&gt;var&lt;/b&gt; search = &lt;b&gt;new&lt;/b&gt; Regex(responsePattern);
    &lt;b&gt;var&lt;/b&gt; match = search.Match(s);
    &lt;b&gt;if&lt;/b&gt; (match.Success)
        Console.WriteLine(&quot;{0} is {1}&quot;,
           match.Groups[&quot;name&quot;].Captures[0].Value,
           match.Groups[&quot;status&quot;].Captures[0].Value);
    &lt;b&gt;else&lt;/b&gt;
        Console.WriteLine(&quot;Unexpected response&quot;);
}
&lt;/pre&gt;

&lt;p&gt;This will get the online status of a Windows Live Messanger account given the
live ID account number. For example, passing 1afa695addc07e5 as an argument to
the above will tell whether or not I am online. In this case I am using last
parameter of the &lt;code&gt;BeginGetResponse()&lt;/code&gt; method to pass the request
itself. This then is cast back to &lt;code&gt;WebRequest&lt;/code&gt; in the &lt;code&gt;
PresenceCallback()&lt;/code&gt; method so I can call &lt;code&gt;EndGetResponse()&lt;/code&gt; to
retrieve the actual response. As far at the &lt;code&gt;BeginGetResponse()&lt;/code&gt; call
is concerned, this value is opaque. It ignores it completely and just supplies
it blindly in the &lt;code&gt;IAsyncResult&lt;/code&gt;. It makes no assumptions about the
data at all, it is just something the caller just wants carried around. If I was using
anonymous delegates in C# this would look a lot better as,&lt;/p&gt;
&lt;pre&gt;&lt;b&gt;static&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Main(&lt;b&gt;string&lt;/b&gt;[] args) {
    &lt;b&gt;var&lt;/b&gt; uri = &lt;b&gt;string&lt;/b&gt;.Format(uriFormat, args[0]);
    &lt;b&gt;var&lt;/b&gt; request = WebRequest.Create(uri);
    request.BeginGetResponse(result =&amp;gt; {
        &lt;b&gt;var&lt;/b&gt; response = request.EndGetResponse(result);
        &lt;b&gt;var&lt;/b&gt; s = &lt;b&gt;new&lt;/b&gt; StreamReader(response.GetResponseStream()).ReadToEnd();
        &lt;b&gt;var&lt;/b&gt; search = &lt;b&gt;new&lt;/b&gt; Regex(responsePattern);
        &lt;b&gt;var&lt;/b&gt; match = search.Match(s);
        &lt;b&gt;if&lt;/b&gt; (match.Success)
            Console.WriteLine(&quot;{0} is {1}&quot;,
               match.Groups[&quot;name&quot;].Captures[0].Value,
               match.Groups[&quot;status&quot;].Captures[0].Value);
        &lt;b&gt;else&lt;/b&gt;
            Console.WriteLine(&quot;Unexpected response&quot;);
        }
    }, &lt;b&gt;null&lt;/b&gt;);

    // Other interesting work....

    result.AsyncWaitHandle.WaitOne();
}&lt;/pre&gt;

&lt;p&gt;Here the request local variable &lt;code&gt;request&lt;/code&gt; is captured
automatically by the C# compiler and placed into a compiler generated class as a
field. The compiler generated class also contains the code I supplied in the
lambda as an instance method. When I refer to response in the lambda the
reference is automatically translated into a field lookup in the generated
class. Since &lt;code&gt;request&lt;/code&gt; is already accessible in the lambda I don&apos;t
need the last parameter to carry anything useful so I pass &lt;code&gt;&lt;strong&gt;null&lt;/strong&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Anonymous methods makes using callbacks much easier but since not all
languages support anonymous methods or lambdas the BCL standardized on a method
pattern for &lt;em&gt;begin invoke&lt;/em&gt; that can easily be used by those languages. If the calling pattern did not have a caller state
object then the work performed automatically by the C# compiler would have to be
repeated manually by the programmer in these, then, second class languages. The
.NET team did not want such languages to be second class citizen (especially since neither C# nor VB.NET supported anonymous methods initially)
so they required the presence of the caller state object parameter.&lt;/p&gt;
&lt;p&gt;Now we know why it is there, what to do we call it? I like &lt;code&gt;state&lt;/code&gt;
because the parameter represents state the caller want&apos;s to preserve. I don&apos;t
like &lt;code&gt;object&lt;/code&gt; because it is a common reserved word. I don&apos;t like
&lt;code&gt;stateObject&lt;/code&gt; because a symbol&apos;s type should not be repeated in the
name, we already know it is an &lt;code&gt;&lt;b&gt;object&lt;/b&gt;&lt;/code&gt; by its type. &lt;code&gt;
asyncState&lt;/code&gt; is acceptable, especially since that is the name it is given
by &lt;code&gt;IAsyncResult&lt;/code&gt;, but it is a bit redundant, we already know, by
context, it is asynchronous state. Plus we should avoid abbreviation, like &quot;async&quot;,
in symbol names (though it is very common, and asynchronous is very very long,
so not that bad). &lt;code&gt;data&lt;/code&gt; seems fine to me, it is a synonym to state,
but it is overused. &lt;code&gt;extraData&lt;/code&gt; I cannot, for the life of me, figure
out. Extra for whom? Extra as opposed to what? Unfortunately, this is the name
given to the parameter by &lt;code&gt;IHttpAsyncHandler&lt;/code&gt; (see, I told you &quot;async&quot;
was common).&amp;nbsp; Its name tells me nothing about what I should do with it. It
is very unclear that this value should be mapped to &lt;code&gt;AsyncState&lt;/code&gt; in &lt;code&gt;IAsyncResult&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;I propose we call it &lt;code&gt;state&lt;/code&gt;, with an acceptable variation of
&lt;code&gt;asyncState&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Now, if changing a parameter name was not a breaking change...&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;Trivia: &lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;The above uses the &lt;a href=&quot;http://live.com&quot;&gt;Live Services&lt;/a&gt; that you
	can find more about &lt;a href=&quot;http://dev.live.com&quot;&gt;here&lt;/a&gt;.&lt;/li&gt;
	&lt;li&gt;The IM presence service returns JSON which I parse using a fancy looking
	&lt;code&gt;Regex&lt;/code&gt; instance. I recommend that production code use &lt;code&gt;
	DataContractJsonSerializer() &lt;/code&gt;instead.&lt;/li&gt;
&lt;/ul&gt;
</description>
                <link>http://removingalldoubt.com/programming/2008/06/16/BeginInvokes-last-parameter-Whats-in-a-name</link>
                <guid>http://removingalldoubt.com/programming/2008/06/16/BeginInvokes-last-parameter-Whats-in-a-name</guid>
                <pubDate>2008-06-16T13:21:00+00:00</pubDate>
        </item>

        <item>
                <title>If you are using a loop, you're doing it wrong</title>
                <description>&lt;div class=&quot;text&quot;&gt;&lt;p&gt;&quot;If you are using a loop, you&apos;re doing it wrong.&quot; &lt;/p&gt;
&lt;p&gt;That is the advice one of my college professors told us when he was teaching us
&lt;a href=&quot;http://en.wikipedia.org/wiki/APL_(programming_language)&quot;&gt;APL&lt;/a&gt;. APL
was designed to perform vector and matrix operations. Programming in APL is an
exercise in stringing operators together to perform strange and wonderful
things. Using a loop is just wrong and slows things down.&lt;/p&gt;
&lt;p&gt;It is similar with LINQ, if you are using a loop you are doing it wrong. I
find myself
doing a lot of prototyping lately and I am forcing myself to use LINQ; not
because I don&apos;t like it, far from it, I really like LINQ, but using loops is so
ingrained into my psyche that I have to stop myself and force myself to think in
LINQ. Every time I am tempted to write a loop that involves a collection or an
array I ask myself, could I use LINQ instead? Programmers with more of a
database background seem to take to LINQ like a duck to water. They think in
sets and vectors, I don&apos;t, but I am getting there.&lt;/p&gt;
&lt;p&gt;For example sometimes I find myself needing to return an &lt;code&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/code&gt; when
I have a &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt; but of a different &lt;code&gt;T&lt;/code&gt;. This often happens when I want to keep
internal implementation details private. My internal &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt; might have the
actual class but I need to return an enumerable for some interface. Before I
would simply write a loop using the C# 2.0&apos;s &lt;code&gt;&lt;strong&gt;yield&lt;/strong&gt;&lt;/code&gt; syntax,&lt;/p&gt;

  &lt;pre class=&quot;code&quot;&gt;List&amp;lt;Foo&amp;gt; foos = &lt;strong&gt;new&lt;/strong&gt; List&amp;lt;Foo&amp;gt;();

...

&lt;strong&gt;public&lt;/strong&gt; IEnumerable&amp;lt;IFoo&amp;gt; Foos {
    &lt;strong&gt;get&lt;/strong&gt; {
        &lt;strong&gt;foreach&lt;/strong&gt; (Foo f &lt;strong&gt;in&lt;/strong&gt; foos)
            &lt;strong&gt;yield return&lt;/strong&gt; f;
    }
}&lt;/pre&gt;

&lt;p&gt;This loop involves a collection, can I use LINQ? Sure! By using LINQ&apos;s &lt;code&gt;Cast&amp;lt;T&amp;gt;()&lt;/code&gt; method this can be replaced with,&lt;/p&gt;

  &lt;pre class=&quot;code&quot;&gt;&lt;strong&gt;public&lt;/strong&gt; IEnumerable&amp;lt;IFoo&amp;gt; Foos {
    &lt;strong&gt;get&lt;/strong&gt; { &lt;strong&gt;return&lt;/strong&gt; foos.Cast&amp;lt;IFoo&amp;gt;(); }
}&lt;/pre&gt;

&lt;p&gt;If you are trying to find if a list contains an object by some name, you
could write a loop like,&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;bool&lt;/strong&gt; Contains(&lt;strong&gt;string&lt;/strong&gt; value) {
    &lt;strong&gt;foreach&lt;/strong&gt; (Foo foo &lt;strong&gt;in&lt;/strong&gt; foos)
        &lt;strong&gt;if&lt;/strong&gt; (foo.Name == value)
            &lt;strong&gt;return&lt;/strong&gt; &lt;strong&gt;true&lt;/strong&gt;;
    &lt;strong&gt;return&lt;/strong&gt; &lt;strong&gt;false&lt;/strong&gt;;
}
&lt;/pre&gt;
&lt;p&gt;Using LINQ this might look like,&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;bool&lt;/strong&gt; Contains(&lt;strong&gt;string&lt;/strong&gt; value) {
    &lt;strong&gt;return&lt;/strong&gt; (&lt;strong&gt;from&lt;/strong&gt; foo &lt;strong&gt;in&lt;/strong&gt; foos &lt;strong&gt;where&lt;/strong&gt; foo.Name == value &lt;strong&gt;select&lt;/strong&gt; foo).Any();
}
&lt;/pre&gt;
&lt;p&gt;A nice thing about LINQ is you can perform complicated queries in pieces.
With deferred execution of enumerators, this is fairly efficient as well. This
is really helpful in the debugger. In one chunk of code I was writing I needed to
coalesce adjacent ranges that are marked with the same text value. Assume you
have a structure called Range that looks like,&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;strong&gt;struct&lt;/strong&gt; Range {
    &lt;strong&gt;public int&lt;/strong&gt; Start;
    &lt;strong&gt;public int&lt;/strong&gt; Length;
}&lt;/pre&gt;
&lt;p&gt;and another &lt;code&gt;&lt;strong&gt;struct&lt;/strong&gt;&lt;/code&gt; that labels the ranges with names,&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;strong&gt;struct&lt;/strong&gt; NamedRange {
    &lt;strong&gt;public string&lt;/strong&gt; Name;
    &lt;strong&gt;public&lt;/strong&gt; Range Range;
}
&lt;/pre&gt;
&lt;p&gt;Now lets have a routine that calculates the range information over some
stream,&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;strong&gt;public&lt;/strong&gt; IEnumerable&amp;lt;NamedRange&amp;gt; GetNamedRanges(Stream stream) {
    ...
}&lt;/pre&gt;
&lt;p&gt;Lets assume that name ranges are some things like &quot;whtespace&quot;, &quot;reserved&quot;,
&quot;identifier&quot;, &quot;number&quot; &quot;string&quot;, etc. as you might expect to receive from a
lexical scanner like found in this
&lt;a href=&quot;http://www.removingalldoubt.com/PermaLink.aspx/f615fab5-28e7-4560-8d9e-db90502a4c5e&quot;&gt;
post&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;What I want to do with these ranges is to convert the names into styles such
as you might find referencing a CSS style sheet. So, in effect, I am mapping
NameRange values to StyledRange values where StyleRange would look something
like,&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;strong&gt;struct&lt;/strong&gt; StyledRange {
    &lt;strong&gt;public&lt;/strong&gt; Style Style;
    &lt;strong&gt;public&lt;/strong&gt; Range Range;
}&lt;/pre&gt;
&lt;p&gt;Lets create a dictionary that maps range names to styles such as,&lt;/p&gt;
&lt;pre cla=&quot;code&quot;&gt;styleMap[&quot;number&quot;] = &lt;strong&gt;new&lt;/strong&gt; Style() { Name = &quot;green&quot; };
styleMap[&quot;reserved&quot;] = &lt;strong&gt;new&lt;/strong&gt; Style() { Name = &quot;bold&quot; };&lt;/pre&gt;
&lt;p&gt;I only wanted to highlight numbers and reserved words, for everything
else I will use the default style,&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;defaultStyle = &lt;strong&gt;new&lt;/strong&gt; Style { Name = &quot;normal&quot; };&lt;/pre&gt;
&lt;p&gt;We can translate our named ranges directly into styled ranges by using a
LINQ query expression such as,&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;strong&gt;var&lt;/strong&gt; ranges = GetNamedRanges(stream);
&lt;strong&gt;var&lt;/strong&gt; styledRanges = &lt;strong&gt;from&lt;/strong&gt; range &lt;strong&gt;in&lt;/strong&gt; ranges
                  &lt;strong&gt; select new&lt;/strong&gt; StyledRange() {
                       Style = styleMap.MapOrDefault(range.Name)
                       Range = range.Range
                   };
&lt;/pre&gt;
&lt;p&gt;where &lt;code&gt;MapOrDefault()&lt;/code&gt; is an extension method for &lt;code&gt;
IDictionary&amp;lt;TKey, TValue&amp;gt;&lt;/code&gt; that looks like,&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;strong&gt;public static&lt;/strong&gt; TValue MapOrDefault&amp;lt;TKey, TValue&amp;gt;(
      &lt;strong&gt;this&lt;/strong&gt; IDictionary&amp;lt;TKey, TValue&amp;gt; dictionary, TKey key) {
    TValue result;
    &lt;strong&gt;if&lt;/strong&gt; (dictionary.TryGetValue(key, &lt;strong&gt;out&lt;/strong&gt; result))
        &lt;strong&gt;return&lt;/strong&gt; result;
    &lt;strong&gt;return&lt;/strong&gt; &lt;strong&gt;default&lt;/strong&gt;(TValue);
}&lt;/pre&gt;
&lt;p&gt;which is patterned after the existing LINQ methods for &lt;code&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/code&gt;,
&lt;code&gt;FirstOrDefault()&lt;/code&gt; and &lt;code&gt;LastOrDefault()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Since many of the ranges that have different names will have the same style, it
would be nice to coalesce adjacent styles together so no two adjacent ranges
have the same style. In other words, we only want a new styled range when the
style changes. The above query expression just produces a one-to-one
mapping of named range to styled range. What we need is something that will
merge adjacent ranges. Do do this I will introduce another extension method,
&lt;code&gt;Reduce&amp;lt;T&amp;gt;()&lt;/code&gt; for &lt;code&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/code&gt;,&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;strong&gt;public static&lt;/strong&gt; IEnumerable&amp;lt;T&amp;gt; Reduce&amp;lt;T&amp;gt;(&lt;strong&gt;this&lt;/strong&gt; IEnumerable&amp;lt;T&amp;gt; e,
    Func&amp;lt;T, T, &lt;strong&gt;bool&lt;/strong&gt;&amp;gt; match,
    Func&amp;lt;T, T, T&amp;gt; reduce) {
    &lt;strong&gt;var&lt;/strong&gt; en = e.GetEnumerator();
    T last;
    &lt;strong&gt;if&lt;/strong&gt; (en.MoveNext()) {
        last = en.Current;
        &lt;strong&gt;while&lt;/strong&gt; (en.MoveNext()) {
            &lt;strong&gt;if&lt;/strong&gt; (!match(last, en.Current)) {
                &lt;strong&gt;yield return&lt;/strong&gt; last;
                last = en.Current;
            }
            &lt;strong&gt;else&lt;/strong&gt;
                last = reduce(last, en.Current);
        }
        &lt;strong&gt;yield return&lt;/strong&gt; last;
    }
}&lt;/pre&gt;
&lt;p&gt;What this method does is if two adjacent elements match (as defined by the &lt;code&gt;match&lt;/code&gt;
delegate returning &lt;code&gt;&lt;strong&gt;true&lt;/strong&gt;&lt;/code&gt;) they will be reduced into one element by calling the &lt;code&gt;reduce&lt;/code&gt;
delegate. For example, the &lt;code&gt;Sum&amp;lt;T&amp;gt;()&lt;/code&gt;standard extension method could be
implemented using &lt;code&gt;Reduce&amp;lt;T&amp;gt;()&lt;/code&gt; as,&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;strong&gt;var&lt;/strong&gt; sum = numbers.Reduce((a, b) =&amp;gt; true, (a, b) =&amp;gt; a + b).First();&lt;/pre&gt;
&lt;p&gt;Now that we have &lt;code&gt;Reduce&amp;lt;T&amp;gt;()&lt;/code&gt;, lets reduce the list of styled
ranges to coalesce the adjacent ranges with the same style. This can be done by,&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;styledRanges = styledRanges.Reduce(
    (r1, r2) =&amp;gt; r1.Style == r2.Style,
    (r1, r2) =&amp;gt; &lt;strong&gt;new&lt;/strong&gt; StyledRange() {
        Style = r1.Style,
        Range = MergeRanges(r1.Range, r2.Range)});&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;MergeRanges()&lt;/code&gt; referenced above, is,&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;Range MergeRanges(Range r1, Range r2) {
    &lt;strong&gt;return new&lt;/strong&gt; Range() { Start = r1.Start, Length = r2.Start - r1.Start + r2.Length };
}&lt;/pre&gt;
&lt;p&gt;In my example, this took 18 ranges for a typical line of C# source down to 7
ranges. Pretty good. But I noticed that some of those ranges were styling
whitespace as &quot;normal&quot;. This seems like a waste; why switch back from
green to black text for writing whitespace? Why not combine those with
adjacent ranges instead? A simple approach to this is to add the following code
prior to mapping the styles,&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;ranges = ranges.Reduce(
    (r1, r2) =&amp;gt; r1.Name == &quot;whitespace&quot; || r2.Name == &quot;whitespace&quot;,
    (r1, r2) =&amp;gt; &lt;strong&gt;new&lt;/strong&gt; NamedRange() {
        Name = r1.Name == &quot;whitespace&quot; ? r2.Name : r1.Name,
        Range = MergeRanges(r1.Range, r2.Range)
    });&lt;/pre&gt;
&lt;p&gt;This says to merge whitespace ranges with adjacent non-whitespace ranges.
This reduces the range count from 7 to 4. Not bad from the original 18 and that
was just for one line of source. This savings adds up quickly over an entire file.&lt;/p&gt;
&lt;p&gt;The complete example, made as a function, looks like,&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;IEnumerable&amp;lt;StyledRange&amp;gt; StyleRanges(IEnumerable&amp;lt;NamedRange&amp;gt; ranges) {

    &lt;em&gt;// Merge whitespace ranges with adjacent non-whitespace ranges&lt;/em&gt;
    ranges = ranges.Reduce(
        (r1, r2) =&amp;gt; r1.Name == &quot;whitespace&quot; || r2.Name == &quot;whitespace&quot;,
        (r1, r2) =&amp;gt; new NamedRange() {
            Name = r1.Name == &quot;whitespace&quot; ? r2.Name : r1.Name,
            Range = MergeRanges(r1.Range, r2.Range)});

    &lt;em&gt;// Map named ranges to styles.&lt;/em&gt;
    &lt;strong&gt;var&lt;/strong&gt; styledRanges = &lt;strong&gt;from&lt;/strong&gt; range &lt;strong&gt;in&lt;/strong&gt; ranges
                       &lt;strong&gt;select new&lt;/strong&gt; StyledRange() {
                           Style = styleMap.MapOrDefault(range.Name)
                           Range = range.Range };

    &lt;em&gt;// Merge adjacent ranges with the same style.&lt;/em&gt;
    styledRanges = styledRanges.Reduce(
        (r1, r2) =&amp;gt; r1.Style == r2.Style,
        (r1, r2) =&amp;gt; &lt;strong&gt;new&lt;/strong&gt; StyledRange() {
            Style = r1.Style,
            Range = MergeRanges(r1.Range, r2.Range)});

    &lt;strong&gt;return&lt;/strong&gt; styledRanges;
}&lt;/pre&gt;

&lt;p&gt;There are a few nice things about this function. First, it builds up an &lt;code&gt;
IEnumerable&amp;lt;T&amp;gt;&lt;/code&gt; but this enumerable doesn&apos;t execute until one of its
enumerators is enumerated. This is due to the deferred execution of enumerators.
Second, even though deferred execution makes single step debugging challenging,
you can get a picture of what the function will do by using &lt;code&gt;ToArray()&lt;/code&gt;
in the watch windows on the intermediate results. This allows you to inspect the
intermediate result to see if the mapping or reducing is what you expected.
Third, this routine has no loops. It operates on each enumerable as a set. Now
some of you will rightly say there is a loop buried in the &lt;code&gt;Reduce&amp;lt;T&amp;gt;()&lt;/code&gt;
method. True; &lt;code&gt;Reduce&amp;lt;T&amp;gt;()&lt;/code&gt; is just like many other LINQ extension
methods, they contain loops implied by the returned enumerator. But LINQ allows me to write code that communicates
what I am trying to do without it being obscured by the details of how it is done.
I think of LINQ not so much as a set of features in C# but a way of programming.
A way of programming that, if you are using loops, you are doing it wrong.&lt;/p&gt;
&lt;/div&gt;
</description>
                <link>http://removingalldoubt.com/programming/2008/03/29/If-you-are-using-a-loop-youre-doing-it-wrong</link>
                <guid>http://removingalldoubt.com/programming/2008/03/29/If-you-are-using-a-loop-youre-doing-it-wrong</guid>
                <pubDate>2008-03-29T16:55:00+00:00</pubDate>
        </item>

        <item>
                <title>Scanners (not the movie)</title>
                <description>&lt;div class=&quot;text&quot;&gt;&lt;p&gt;In my previous post I introduced a program I wrote to that uses the method
described in
&lt;a href=&quot;http://www.amazon.com/First-Order-Logic-Raymond-M-Smullyan/dp/0486683702/ref=sr_11_1/102-8843584-4849732?ie=UTF8&amp;amp;qid=1192783059&amp;amp;sr=11-1&quot;&gt;First-Order Logic&lt;/a&gt; by
&lt;a href=&quot;http://en.wikipedia.org/wiki/Raymond_Smullyan&quot;&gt;Raymond M. Smullyan&lt;/a&gt;
to prove an arbitrary propositional logic expression is a tautology. In this
program I use some techniques that I want to describe. The first is the scanner.
When writing scanners I have always hand written the scanner. There are two
primary reasons for this, first I have never found scanners that difficult to
write and, second, they are very time critical, often taking more than 20% of
the overall parsing time, and auto-generated scanners are hard to optimize.
Tableaux uses a hand-written scanner. The code for the scanner is in Scanner.cs
in the zip file found
&lt;a href=&quot;files/Tableaux.zip&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;A scanner is a routine that takes text input and classifies it into a stream
of tokens. Scanners typically give one token at a time. The scanner is the lowest level of the parsing process. Parsers
are built on top of scanners and use the scanner results to determine the
meaning of the source using some kind of grammar. The distinction between the
parser and the scanner is somewhat arbitrary. You could just consider each
character of the input as a token and build the parser up from there. Doing this
is typically slower than a traditional scanner, however, so most parsers use a
scanner. The line between the scanner and the parser is usually drawn at the
lexical elements such as identifiers, reserved words, numeric and string
constants, comments, etc. More formally, a lexical element is a string of
characters that can be matched and classified by a regular expression. Scanner
generators, such as &lt;a href=&quot;http://en.wikipedia.org/wiki/Lex_programming_tool&quot;&gt;
Lex&lt;/a&gt;, use regular expressions to specify the scanner.&lt;/p&gt;
&lt;p&gt;The trick to a fast scanner is to reduce the per-character cost of the
scanner. One way to do this is to make sure you are executing the fewest number of instructions
per-character-scanned as possible. A few of tricks I use to reduce the
per-character cost include,&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;Use the switch statement&lt;ul&gt;
		&lt;li&gt;That included switch or case statement typically heavily optimize
		the result, often generating jump tables.&lt;/li&gt;
	&lt;/ul&gt;
	&lt;/li&gt;
	&lt;li&gt;Pull fields into local variables and only write them at the before
	returning.&lt;ul&gt;
		&lt;li&gt;Local variables can be mapped to registers during code generation.
		Fields never will be. Stores to global memory (i.e. a field of a object
		allocated from the heap) are very hard for code generators to optimize.
		Local variable are much easier.&lt;/li&gt;
	&lt;/ul&gt;
	&lt;/li&gt;
	&lt;li&gt;Make checking for boundaries (end of file or end of buffer) appear as a
	character.&lt;ul&gt;
		&lt;li&gt;This merges the end-of-file check with character classification.&lt;/li&gt;
		&lt;li&gt;In this case I use the character value 0 to indicate end of file. If
		I was willing to use unsafe code I would have taken advantage of the
		fact that the CLR ensures a 0 value at the end of every string.&lt;/li&gt;
	&lt;/ul&gt;
	&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;One thing that looks slow that isn&apos;t is the GetChar() routine. Normally you
would see that and think I am paying for a CALL/RET pair but his gets inlined in
retail. I am paying for the end of buffer check every character but, as noted
above, this can be removed if I wanted to use unsafe code, definitely overkill
for a small program like tableaux.&lt;/p&gt;
&lt;p&gt;I went a bit overboard, intentionally, with identifier. It handles C# style
identifiers, handling Unicode letter and number classifications. To avoid
calling the Unicode classification routines for every character I hard-coded the
ASCII part and only call the Unicode classification routines when I encounter a
non-ASCII potential identifier character. It is not that the classification
routines are slow, it is that every cycle counts in the scanner and avoiding the
CALL/RET and character table look is significant for file that contain mostly
ASCII characters. Note also that I hard-code some non-identifier characters in
the identifier internal loop. This avoids calling the Unicode classification
routines for those character, which are the most common characters you might
find after an identifier, which avoids calling the Unicode classification routines
at all for most input.&lt;/p&gt;
&lt;/div&gt;
</description>
                <link>http://removingalldoubt.com/programming/2007/12/30/Scanners-not-the-movie</link>
                <guid>http://removingalldoubt.com/programming/2007/12/30/Scanners-not-the-movie</guid>
                <pubDate>2007-12-30T17:01:00+00:00</pubDate>
        </item>

        <item>
                <title>To b | !b, that is the question</title>
                <description>&lt;div class=&quot;text&quot;&gt;&lt;p&gt;When I was reading
&lt;a href=&quot;http://www.amazon.com/First-Order-Logic-Raymond-M-Smullyan/dp/0486683702/ref=sr_11_1/102-8843584-4849732?ie=UTF8&amp;amp;qid=1192783059&amp;amp;sr=11-1&quot;&gt;First-Order Logic&lt;/a&gt; by
&lt;a href=&quot;http://en.wikipedia.org/wiki/Raymond_Smullyan&quot;&gt;Raymond M. Smullyan&lt;/a&gt; I
was fascinated by
the second chapter in which he describes a method for proving a propositional
logic expression is a tautology. This method is called the Tableau Method. The Tableau Method is a
rather straight forward way to prove a statement is always going to be true by
demonstrating that asserting it is false will result in a contradiction. The
propositional logic expression language Dr. Smullyan uses is rather simple. I
present a modified version of it here. My version
contains a not operator (!), a conjunction operator (&amp;amp;), a disjunction operator
(|) and an implication operator (→).&lt;sup&gt;&lt;a name=&quot;note1_1ref&quot; href=&quot;#note1_1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; For example,&lt;/p&gt;
&lt;p style=&quot;margin-left: 40px;&quot;&gt;p &amp;amp; q&lt;/p&gt;
&lt;p&gt;is true when both &lt;em&gt;p&lt;/em&gt; and &lt;em&gt;q&lt;/em&gt; are true; &lt;/p&gt;
&lt;p style=&quot;margin-left: 40px&quot;&gt;p | q&lt;/p&gt;
&lt;p&gt;is true when either &lt;em&gt;p&lt;/em&gt; is true or &lt;em&gt;q&lt;/em&gt; is true;&lt;/p&gt;
&lt;p style=&quot;margin-left: 40px&quot;&gt;p &lt;span class=&quot;style1&quot;&gt;→ q&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;is true when&lt;em&gt; p &lt;/em&gt;is false or &lt;em&gt;p&lt;/em&gt; is true and &lt;em&gt;q&lt;/em&gt; is also
true; and finally,&lt;/p&gt;
&lt;p style=&quot;margin-left: 40px&quot;&gt;!p&lt;/p&gt;
&lt;p&gt;is true only when &lt;em&gt;p&lt;/em&gt; is false. This should be very familiar to any
programmer. A propositional logic expression is a tautology if it will always be
true regardless of the value of its propositional variables. An example of a
simple tautology is,&lt;/p&gt;
&lt;p style=&quot;margin-left: 40px&quot;&gt;p | !p&lt;/p&gt;
&lt;p&gt;which will be true if &lt;em&gt;p&lt;/em&gt; is true or false. This statement is always
true. This can be proven by simply demonstrating that asserting it is false
results in a contradiction. For example, we assert a statement is false by
prefixing it with a capital F or true with a capital T. For the above expression
this would look like,&lt;/p&gt;
&lt;p style=&quot;margin-left: 40px&quot;&gt;F p | !p&lt;/p&gt;
&lt;p&gt;For this to be false both of the sub-expressions would also have to be
false. This would then look like&lt;/p&gt;
&lt;p style=&quot;margin-left: 40px&quot;&gt;F p&lt;br /&gt;F !p&lt;/p&gt;
&lt;p&gt;For &lt;em&gt;!p&lt;/em&gt; to be false &lt;em&gt;p&lt;/em&gt; would have to be true, which looks
like,&lt;/p&gt;
&lt;p style=&quot;margin-left: 40px&quot;&gt;T p&lt;/p&gt;
&lt;p&gt;Now we have a contradiction, according to propositional logic, &lt;em&gt;p&lt;/em&gt; cannot be both true and false so &lt;em&gt;
p | !p&lt;/em&gt; must always be true. Using the Tableau Method this would look like,&lt;/p&gt;
&lt;p style=&quot;margin-left: 40px&quot;&gt;(1) F p | !p&lt;br /&gt;(2) F p (1)&lt;br /&gt;(3) F !p (1)&lt;br /&gt;(4) T p (3)&lt;br /&gt;X&lt;/p&gt;
&lt;p&gt;where the X indicates that the tableau ends in a contradiction (i.e. is
closed) and the
numbers to the left label the expressions and the numbers to the right indicate
which expression the assertion is derived from. To produce a tableau you first
assert the expression is false then use the following table to add assertions,&lt;/p&gt;
&lt;table style=&quot;width: 100%&quot;&gt;
	&lt;tbody&gt;&lt;tr&gt;
		&lt;td&gt;&lt;strong&gt;Expression of the form&lt;/strong&gt;&lt;/td&gt;
		&lt;td&gt;&lt;strong&gt;Asserts&lt;/strong&gt;&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td style=&quot;width: 273px&quot;&gt;T !p&lt;/td&gt;
		&lt;td&gt;F p&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td style=&quot;width: 273px&quot;&gt;F !p&lt;/td&gt;
		&lt;td&gt;T p&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td style=&quot;width: 273px&quot;&gt;T p &amp;amp; q&lt;/td&gt;
		&lt;td&gt;T p and T q&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td style=&quot;width: 273px&quot;&gt;F p &amp;amp; q&lt;/td&gt;
		&lt;td&gt;F p or F q&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td style=&quot;width: 273px&quot;&gt;T p | q&lt;/td&gt;
		&lt;td&gt;T p or T q&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td style=&quot;width: 273px&quot;&gt;F p | q&lt;/td&gt;
		&lt;td&gt;F p and F q&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td style=&quot;width: 273px&quot;&gt;T p → q&lt;/td&gt;
		&lt;td&gt;F p or T q&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td style=&quot;width: 273px&quot;&gt;F p → q&lt;/td&gt;
		&lt;td&gt;T p and F q&lt;/td&gt;
	&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;When two expressions are asserted the table might branch. If the above table
says &quot;and&quot; then both statement are asserted directly, but if it contains an &quot;or&quot;
then both assertions must lead to a contradiction independently. For example,
consider wanting to prove the transitive property of the implication operator,
that is &lt;em&gt;(p→q &amp;amp; q→r) → (p→r)&lt;/em&gt;, this would start off with adding asserts
until we are required to branch,&lt;/p&gt;
&lt;p style=&quot;margin-left: 40px&quot;&gt;
  (1) F (p→q &amp;amp; q→r) → (p→r)&lt;br /&gt;
  (2) T p→q &amp;amp; q→r (1)&lt;br /&gt;
  (3) F p→r (1)&lt;br /&gt;
  (4) T p→q (2)&lt;br /&gt;
  (5) T q→r (2)&lt;br /&gt;
  (6) T p (3)&lt;br /&gt;
  (7) F r (3)&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;We now have to handle (4) and (5) with require us to branch the table. The first branch is
the &lt;em&gt;F p&lt;/em&gt; branch,&lt;/p&gt;
&lt;p style=&quot;margin-left: 40px&quot;&gt;
  (8) F p (4)&lt;br /&gt;
  X
&lt;/p&gt;
&lt;p&gt;This lead immediately to a contradiction because of (6) so this branch of the tableau is closed. We now need
handle the &lt;em&gt;T q&lt;/em&gt; branch,&lt;/p&gt;
&lt;p style=&quot;margin-left: 40px&quot;&gt;
  (9) T q (4)
&lt;/p&gt;
&lt;p&gt;This doesn&apos;t lead to an immediate contradiction so we need to branch again for (5). The first
branch for (5) looks like&lt;/p&gt;
&lt;p style=&quot;margin-left: 40px&quot;&gt;
  (10) F q (5)&lt;br /&gt;
  X
&lt;/p&gt;
&lt;p&gt;This again leads to an immediate contradiction, we already asserted
that &lt;em&gt;q&lt;/em&gt; must be true in (9). The second branch for (5) is the &lt;em&gt;T r&lt;/em&gt; branch.&lt;/p&gt;
&lt;p style=&quot;margin-left: 40px&quot;&gt;
  (11) T r (5)&lt;br /&gt;
  X
&lt;/p&gt;
&lt;p&gt;This also lead to an immediate contradiction because of (7). Since all
branches lead to a contradiction the original expression must always be true.&lt;/p&gt;
&lt;p&gt;Putting this altogether we get,&lt;/p&gt;
&lt;table&gt;
&lt;tbody&gt;&lt;tr&gt;
&lt;td&gt;
&lt;p&gt;
  (1) F (p→q &amp;amp; q→r) → (p→r)&lt;br /&gt;
  (2) T p→q &amp;amp; q→r (1)&lt;br /&gt;
  (3) F p→r (1)&lt;br /&gt;
  (4) T p→q (2)&lt;br /&gt;
  (5) T q→r (2)&lt;br /&gt;
  (6) T p (3)&lt;br /&gt;
  (7) F r (3)&lt;br /&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;table style=&quot;text-align: center;&quot;&gt;
&lt;tbody&gt;&lt;tr&gt;
&lt;td valign=&quot;top&quot; style=&quot;border-right-color: black;
	border-right-style:solid;
	border-right-width:thin;
	padding-right: 10px&quot;&gt;
&lt;p&gt;
  (8) F p (4)&lt;br /&gt;
  X
&lt;/p&gt;
&lt;/td&gt;
&lt;td style=&quot;padding-right:10px;&quot;&gt;
&lt;p&gt;
  (9) T q (4)
&lt;/p&gt;
&lt;table&gt;
&lt;tbody&gt;&lt;tr&gt;
&lt;td style=&quot;border-right-color: black;
	border-right-style:solid;
	border-right-width:thin;
	padding-right: 10px&quot;&gt;
&lt;p&gt;
  (10) F q (5)&lt;br /&gt;
  X
&lt;/p&gt;
&lt;/td&gt;
&lt;td style=&quot;padding-right:10px;&quot;&gt;
&lt;p&gt;
  (11) T r (5)&lt;br /&gt;
  X
&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;This is a simple and elegant way to demonstrate proof by contradiction. Of
course I just had to write a program to tell me whether an
arbitrary propositional logic expression is a tautology and automatically
generate the tableau.&lt;/p&gt;
&lt;p&gt;You can find the source project that is works with the just released Visual
Studio 2008 &lt;a href=&quot;/files/Tableaux.zip&quot;&gt;here&lt;/a&gt;. It requires VS 2008 because I am addicted to some of the new
C# features, especially the &lt;b&gt;&lt;code&gt;var&lt;/code&gt;&lt;/b&gt; keyword.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;&lt;a name=&quot;note1_1&quot;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; Dr. Sumullyan uses different characters for his operators
but his characters are not as commonly included in browser fonts and are not as easy to
type on a standard keyboard (using &lt;span class=&quot;style3&quot;&gt;-&amp;gt;&lt;/span&gt; as a synonym
of →) as the ones I use. &lt;a href=&quot;#note1_1ref&quot;&gt;&amp;lt;&amp;lt; back&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;/files/Tableaux.zip&quot;&gt;Tableaux.zip (9.88 kb)&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;
</description>
                <link>http://removingalldoubt.com/programming/2007/12/19/To-b-or-not-b-this-is-the-question</link>
                <guid>http://removingalldoubt.com/programming/2007/12/19/To-b-or-not-b-this-is-the-question</guid>
                <pubDate>2007-12-19T17:10:00+00:00</pubDate>
        </item>

        <item>
                <title>Operators, Generics and Policies II: Adapter Policy</title>
                <description>&lt;div class=&quot;text&quot;&gt;&lt;p&gt;The mixin technique in my
&lt;a href=&quot;post/2007/07/25/Operators-Generics-and-Policies.asp&quot;&gt;last post&lt;/a&gt; was a bit awkward to use
with generic
methods. It is much less awkward to use for classes, however. For example, if
you want an expression evaluator much like I posted
&lt;a href=&quot;post/2005/11/12/The-Expression-Problem-Part-I-Procedural.aspx&quot;&gt;
here&lt;/a&gt;, but you want to leave the result type open, you can use this technique
to do it.&lt;/p&gt;
&lt;p&gt;First, I declared a policy interface for the operators I wanted to abstract
over,&lt;/p&gt;
&lt;pre&gt;&lt;b&gt;partial&lt;/b&gt; &lt;b&gt;interface&lt;/b&gt; IOperatorPolicy&amp;lt;T&amp;gt; {
    T Add(T a, T b);
    T Subtract(T a, T b);
    T Multiply(T a, T b);
    T Divide(T a, T b);
}&lt;/pre&gt;

&lt;p&gt;Second, I created the operator policies for &lt;code&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/code&gt;
and &lt;code&gt;&lt;strong&gt;double&lt;/strong&gt;&lt;/code&gt;. I could have created more, &lt;code&gt;
&lt;strong&gt;decimal&lt;/strong&gt;&lt;/code&gt;, &lt;code&gt;&lt;strong&gt;float&lt;/strong&gt;&lt;/code&gt;, etc. but
two is sufficient for demonstration.&lt;/p&gt;
&lt;pre&gt;&lt;strong&gt;partial&lt;/strong&gt; &lt;b&gt;struct&lt;/b&gt; IntOperatorPolicy : IOperatorPolicy&amp;lt;&lt;b&gt;int&lt;/b&gt;&amp;gt; {
    &lt;b&gt;int&lt;/b&gt; IOperatorPolicy&amp;lt;&lt;b&gt;int&lt;/b&gt;&amp;gt;.Add(&lt;b&gt;int&lt;/b&gt; a, &lt;b&gt;int&lt;/b&gt; b) { &lt;b&gt;return&lt;/b&gt; a + b; }
    &lt;b&gt;int&lt;/b&gt; IOperatorPolicy&amp;lt;&lt;b&gt;int&lt;/b&gt;&amp;gt;.Subtract(&lt;b&gt;int&lt;/b&gt; a, &lt;b&gt;int&lt;/b&gt; b) { &lt;b&gt;return&lt;/b&gt; a - b; }
    &lt;b&gt;int&lt;/b&gt; IOperatorPolicy&amp;lt;&lt;b&gt;int&lt;/b&gt;&amp;gt;.Multiply(&lt;b&gt;int&lt;/b&gt; a, &lt;b&gt;int&lt;/b&gt; b) { &lt;b&gt;return&lt;/b&gt; a * b; }
    &lt;b&gt;int&lt;/b&gt; IOperatorPolicy&amp;lt;&lt;b&gt;int&lt;/b&gt;&amp;gt;.Divide(&lt;b&gt;int&lt;/b&gt; a, &lt;b&gt;int&lt;/b&gt; b) { &lt;b&gt;return&lt;/b&gt; a / b; }
}

&lt;b&gt;partial struct&lt;/b&gt; DoubleOperatorPolicy : IOperatorPolicy&amp;lt;&lt;b&gt;double&lt;/b&gt;&amp;gt; {
    &lt;b&gt;double&lt;/b&gt; IOperatorPolicy&amp;lt;&lt;b&gt;double&lt;/b&gt;&amp;gt;.Add(&lt;b&gt;double&lt;/b&gt; a, &lt;b&gt;double&lt;/b&gt; b) { &lt;b&gt;return&lt;/b&gt; a + b;
    &lt;b&gt;double&lt;/b&gt; IOperatorPolicy&amp;lt;&lt;b&gt;double&lt;/b&gt;&amp;gt;.Subtract(&lt;b&gt;double&lt;/b&gt; a, &lt;b&gt;double&lt;/b&gt; b) { &lt;b&gt;return&lt;/b&gt; a - b; }
    &lt;b&gt;double&lt;/b&gt; IOperatorPolicy&amp;lt;&lt;b&gt;double&lt;/b&gt;&amp;gt;.Multiply(&lt;b&gt;double&lt;/b&gt; a, &lt;b&gt;double&lt;/b&gt; b) { &lt;b&gt;return&lt;/b&gt; a * b; }
    &lt;b&gt;double&lt;/b&gt; IOperatorPolicy&amp;lt;&lt;b&gt;double&lt;/b&gt;&amp;gt;.Divide(&lt;b&gt;double&lt;/b&gt; a, &lt;b&gt;double&lt;/b&gt; b) { &lt;b&gt;return&lt;/b&gt; a / b; }
}&lt;/pre&gt;

&lt;p&gt;Next are the expression nodes themselves. I wrap them in a generic class
that takes the parameters. This mitigates some of the clutter caused by the
policy.&lt;/p&gt;
&lt;pre&gt;&lt;b&gt;partial&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; Expression&amp;lt;T, OperatorPolicy&amp;gt;
    &lt;strong&gt;where&lt;/strong&gt; OperatorPolicy : &lt;b&gt;struct&lt;/b&gt;, IOperatorPolicy&amp;lt;T&amp;gt; {

    &lt;b&gt;static&lt;/b&gt; OperatorPolicy _operatorPolicy = &lt;b&gt;new&lt;/b&gt; OperatorPolicy();

    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;abstract&lt;/b&gt; &lt;b&gt;partial&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; Expr {
        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;abstract&lt;/b&gt; T Evaluate();
    }

    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;partial&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; Literal : Expr {
        &lt;b&gt;private&lt;/b&gt; T _literal;

        &lt;b&gt;public&lt;/b&gt; Literal(T literal) { _literal = literal; }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;override&lt;/b&gt; T Evaluate() {
            &lt;b&gt;return&lt;/b&gt; _literal;
        }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;override&lt;/b&gt; &lt;b&gt;string&lt;/b&gt; ToString() {
            &lt;b&gt;return&lt;/b&gt; _literal.ToString();
        }
    }

    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;abstract&lt;/b&gt; &lt;b&gt;partial&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; BinaryOp : Expr {
        &lt;b&gt;private&lt;/b&gt; Expr _left;
        &lt;b&gt;private&lt;/b&gt; Expr _right;

        &lt;b&gt;protected&lt;/b&gt; BinaryOp(Expr left, Expr right) { _left = left; _right = right; }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;override&lt;/b&gt; T Evaluate() {
            &lt;b&gt;return&lt;/b&gt; EvaluateOp(_left.Evaluate(), _right.Evaluate());
        }

        &lt;b&gt;protected&lt;/b&gt; &lt;b&gt;abstract&lt;/b&gt; T EvaluateOp(T left, T right);
    }

    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;partial&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; Add : BinaryOp {
        &lt;b&gt;public&lt;/b&gt; Add(Expr left, Expr right) : &lt;b&gt;base&lt;/b&gt;(left, right) { }
        &lt;b&gt;protected&lt;/b&gt; &lt;b&gt;override&lt;/b&gt; T EvaluateOp(T left, T right) {
            &lt;b&gt;return&lt;/b&gt; _operatorPolicy.Add(left, right);
        }
    }

    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;partial&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; Add : BinaryOp {
        &lt;b&gt;public&lt;/b&gt; Add(Expr left, Expr right) : &lt;b&gt;base&lt;/b&gt;(left, right) { }
        &lt;b&gt;protected&lt;/b&gt; &lt;b&gt;override&lt;/b&gt; T EvaluateOp(T left, T right) {
            &lt;b&gt;return&lt;/b&gt; _operatorPolicy.Add(left, right);
        }
    }

    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;partial&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; Subtract : BinaryOp {
        &lt;b&gt;public&lt;/b&gt; Subtract(Expr left, Expr right) : &lt;b&gt;base&lt;/b&gt;(left, right) { }
        &lt;b&gt;protected&lt;/b&gt; &lt;b&gt;override&lt;/b&gt; T EvaluateOp(T left, T right) {
            &lt;b&gt;return&lt;/b&gt; _operatorPolicy.Subtract(left, right);
        }
    }

    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;partial&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; Multiply : BinaryOp {
        &lt;b&gt;public&lt;/b&gt; Multiply(Expr left, Expr right) : &lt;b&gt;base&lt;/b&gt;(left, right) { }
        &lt;b&gt;protected&lt;/b&gt; &lt;b&gt;override&lt;/b&gt; T EvaluateOp(T left, T right) {
            &lt;b&gt;return&lt;/b&gt; _operatorPolicy.Multiply(left, right);
        }
    }

    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;partial&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; Divide : BinaryOp {
        &lt;b&gt;public&lt;/b&gt; Divide(Expr left, Expr right) : &lt;b&gt;base&lt;/b&gt;(left, right) { }
        &lt;b&gt;protected&lt;/b&gt; &lt;b&gt;override&lt;/b&gt; T EvaluateOp(T left, T right) {
            &lt;b&gt;return&lt;/b&gt; _operatorPolicy.Divide(left, right);
        }
    }
}&lt;/pre&gt;

&lt;p&gt;Here the &lt;code&gt;Expression&lt;/code&gt; class acts as a container of a family of
classes. Once you have an instance of one of the &lt;code&gt;Expr&lt;/code&gt; derived classes, calling
&lt;code&gt;Evaluate()&lt;/code&gt; is straight-forward. Unfortunately, constructing one is a bit of a
challenge. To get a literal of the &lt;code&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/code&gt; expression you would need to do,&lt;/p&gt;
&lt;pre&gt;&lt;strong&gt;var&lt;/strong&gt; i1 = &lt;strong&gt;new&lt;/strong&gt; Expression&amp;lt;&lt;strong&gt;int&lt;/strong&gt;, IntOperatorPolicy&amp;gt;.Literal(1);&lt;/pre&gt;

&lt;p&gt;You can make this a little better by introducing a &lt;code&gt;&lt;strong&gt;static&lt;/strong&gt;&lt;/code&gt;
method into &lt;code&gt;Expression&lt;/code&gt; like,&lt;/p&gt;
&lt;pre&gt;&lt;b&gt;public&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; Expr L(T literal) {
    &lt;b&gt;return&lt;/b&gt; &lt;b&gt;new&lt;/b&gt; Literal(literal);
}&lt;/pre&gt;

&lt;p&gt;then the above becomes,&lt;/p&gt;
&lt;pre&gt;&lt;strong&gt;var&lt;/strong&gt; i1 = Expression&amp;lt;&lt;strong&gt;int&lt;/strong&gt;, IntOperatorPolicy&amp;gt;.L(1);&lt;/pre&gt;
&lt;p&gt;This still a bit awkward especially when you want to construct a more
complicated expression,&lt;/p&gt;
&lt;pre&gt;&lt;strong&gt;var&lt;/strong&gt; e = &lt;strong&gt;new&lt;/strong&gt; Expression&amp;lt;&lt;strong&gt;int&lt;/strong&gt;, IntOperatorPolicy&amp;gt;.Add(i1, i1);&lt;/pre&gt;
&lt;p&gt;This can be made significantly better by adding operator overloads to &lt;code&gt;Expr&lt;/code&gt; as
in,&lt;/p&gt;
&lt;pre&gt;&lt;b&gt;public&lt;/b&gt; &lt;b&gt;abstract&lt;/b&gt; &lt;strong&gt;partial&lt;/strong&gt; &lt;b&gt;class&lt;/b&gt; Expr {
    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;abstract&lt;/b&gt; T Evaluate();
    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; Expr &lt;b&gt;operator&lt;/b&gt; +(Expr a, Expr b) {
        &lt;b&gt;return&lt;/b&gt; &lt;b&gt;new&lt;/b&gt; Add(a, b);
    }
    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; Expr &lt;b&gt;operator&lt;/b&gt; -(Expr a, Expr b) {
        &lt;b&gt;return&lt;/b&gt; &lt;b&gt;new&lt;/b&gt; Subtract(a, b);
    }
    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; Expr &lt;b&gt;operator&lt;/b&gt; *(Expr a, Expr b) {
        &lt;b&gt;return&lt;/b&gt; &lt;b&gt;new&lt;/b&gt; Multiply(a, b);
    }
    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; Expr &lt;b&gt;operator&lt;/b&gt; /(Expr a, Expr b) {
        &lt;b&gt;return&lt;/b&gt; &lt;b&gt;new&lt;/b&gt; Divide(a, b);
    }
}&lt;/pre&gt;
&lt;p&gt;now the &lt;code&gt;e&lt;/code&gt; assignment can look like,&lt;/p&gt;
&lt;pre&gt;&lt;strong&gt;var&lt;/strong&gt; e = i1 + i1;&lt;/pre&gt;
&lt;p&gt;and finally, to make it look even better you can put the code to create the
expression in a static method of a class that derives from &lt;code&gt;Expression&lt;/code&gt; and closes the
type parameters. Here are examples of one for &lt;code&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/code&gt; and
a similar one for &lt;code&gt;&lt;strong&gt;double&lt;/strong&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;b&gt;class&lt;/b&gt; TestInt : Expression&amp;lt;&lt;b&gt;int&lt;/b&gt;, IntOperatorPolicy&amp;gt; {
    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Run() {
        &lt;strong&gt;var&lt;/strong&gt; iv = L(1) + L(2) * L(3);
        Console.WriteLine(iv.Evaluate());
    }
}

&lt;b&gt;class&lt;/b&gt; TestDouble : Expression&amp;lt;&lt;b&gt;double&lt;/b&gt;, DoubleOperatorPolicy&amp;gt; {
    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Run() {
        &lt;strong&gt;var&lt;/strong&gt; dv = L(1.2) + L(3.4) * L(5.6);
        Console.WriteLine(dv.Evaluate());
    }
}&lt;/pre&gt;
&lt;p&gt;This shows taking advantage of the C# compiler&apos;s operator overloading to
produce an expression tree and the use of the &lt;code&gt;L()&lt;/code&gt; method as a sort
of cast to change a literal of &lt;code&gt;T&lt;/code&gt; into a &lt;code&gt;Literal&amp;lt;T&amp;gt;&lt;/code&gt;.
The call to &lt;code&gt;Evaluate()&lt;/code&gt; calculates the result of the expression as
an &lt;code&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/code&gt; or &lt;code&gt;&lt;strong&gt;double&lt;/strong&gt;&lt;/code&gt;.
The code generated, as discussed
&lt;a href=&quot;post/2007/07/25/Operators-Generics-and-Policies.aspx&quot;&gt;
last time&lt;/a&gt;, is quite good when JIT&apos;ing outside the debugger and using
optimized retail bits.&lt;/p&gt;
&lt;p&gt;This use of a policy &lt;code&gt;&lt;strong&gt;struct&lt;/strong&gt;&lt;/code&gt; is what I refer to
as an &lt;em&gt;adapter policy&lt;/em&gt;. The &lt;code&gt;Expression&lt;/code&gt; class has an
abstraction it supports, represented by &lt;code&gt;IOperatorPolicy&amp;lt;T&amp;gt;&lt;/code&gt;, but
this abstraction is not supported by any of the interesting types you would want
to pass as &lt;code&gt;Expression&lt;/code&gt;&apos;s &lt;code&gt;T&lt;/code&gt;. This is solved by creating
an &lt;em&gt;adapter policy&lt;/em&gt; and passing that along with &lt;code&gt;T&lt;/code&gt;. This
allows a generic type to introduce an abstraction that is unknown to the types
the generic wants to range over, allow us, as above, to pass &lt;code&gt;int&lt;/code&gt;
for &lt;code&gt;Expression&lt;/code&gt;&apos;s &lt;code&gt;T&lt;/code&gt; even though &lt;code&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/code&gt;
doesn&apos;t implement &lt;code&gt;IOperatorPolicy&amp;lt;T&amp;gt;&lt;/code&gt; itself.&lt;/p&gt;
&lt;p&gt;Next time I will discuss another application of an &lt;em&gt;adapter policy&lt;/em&gt;.&lt;/p&gt;
&lt;/div&gt;
</description>
                <link>http://removingalldoubt.com/programming/2007/07/31/Operators-Generics-and-Policies-II-Adapter-Policy</link>
                <guid>http://removingalldoubt.com/programming/2007/07/31/Operators-Generics-and-Policies-II-Adapter-Policy</guid>
                <pubDate>2007-07-31T22:27:00+00:00</pubDate>
        </item>

        <item>
                <title>Operators, Generics and Policies</title>
                <description>&lt;div class=&quot;text&quot;&gt;&lt;p&gt;One of the limitations of C# generics is you cannot abstract over operators.
That is not completely true, you can if the base class used in &lt;code&gt;&lt;strong&gt;where&lt;/strong&gt;&lt;/code&gt; clause has
operators; but, since operators are more useful, and more common, on value types,
that is only marginally helpful. Also, this abstraction is provided by the base
class not by using generics. What I would like to do is declare that my type&amp;nbsp;
parameter requires the type to implement a particular operator. For example, to create a generic &lt;code&gt;Add()&lt;/code&gt;
method I would like to specify that the type parameter must implement the &lt;code&gt;
+&lt;/code&gt; operator and then be able to use the &lt;code&gt;+&lt;/code&gt; operator in my code
such as,&lt;/p&gt;
&lt;pre&gt;&lt;span style=&quot;color: blue&quot;&gt;&lt;em&gt;// Invalid C#&lt;/em&gt;&lt;/span&gt;
&lt;strong&gt;static&lt;/strong&gt; T Add&amp;lt;T&amp;gt;(T a, T b) &lt;strong&gt;where&lt;/strong&gt; T: T &lt;strong&gt;operator&lt;/strong&gt; +(T, T) {
    &lt;strong&gt;return&lt;/strong&gt; a + b;
}&lt;/pre&gt;

&lt;p&gt;C# doesn&apos;t support this but you can get somewhat close by supplying a policy
&lt;code&gt;&lt;strong&gt;struct&lt;/strong&gt;&lt;/code&gt; like I described in
&lt;a href=&quot;post/2007/07/02/C-Mixins-Sort-of.aspx&quot;&gt;
this post&lt;/a&gt;. The policy &lt;code&gt;&lt;strong&gt;interface&lt;/strong&gt;&lt;/code&gt; and the &lt;code&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/code&gt; policy &lt;code&gt;&lt;strong&gt;struct&lt;/strong&gt;&lt;/code&gt; would
look like,&lt;/p&gt;
&lt;pre&gt;&lt;b&gt;interface&lt;/b&gt; IAddPolicy&amp;lt;T&amp;gt; {
    T Add(T a, T b);
}

&lt;b&gt;struct&lt;/b&gt; IntAddPolicy : IAddPolicy&amp;lt;&lt;b&gt;int&lt;/b&gt;&amp;gt; {
    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;int&lt;/b&gt; Add(&lt;b&gt;int&lt;/b&gt; a, &lt;b&gt;int&lt;/b&gt; b) { &lt;b&gt;return&lt;/b&gt; a + b; }
}&lt;/pre&gt;
&lt;p&gt;This allows you to write the following,&lt;/p&gt;
&lt;pre&gt;&lt;b&gt;static&lt;/b&gt; T Add&amp;lt;T, P&amp;gt;(T a, T b) &lt;strong&gt;where&lt;/strong&gt; P: &lt;b&gt;struct&lt;/b&gt;, IAddPolicy&amp;lt;T&amp;gt; {
    &lt;b&gt;return&lt;/b&gt; &lt;b&gt;new&lt;/b&gt; P().Add(a, b);
}&lt;/pre&gt;
&lt;p&gt;Unfortunately the policy &lt;code&gt;&lt;strong&gt;struct&lt;/strong&gt;&lt;/code&gt; cannot be
inferred from the parameters so you have to supply the type parameter
explicitly,&lt;/p&gt;
&lt;pre&gt;&lt;strong&gt;var&lt;/strong&gt; i = Add&amp;lt;&lt;b&gt;int&lt;/b&gt;, IntAddPolicy&amp;gt;(3, 4);&lt;/pre&gt;
&lt;p&gt;which doesn&apos;t look pretty and generates less than efficient code for the &lt;code&gt;Add()&lt;/code&gt;
method on an i386 architecture,&lt;/p&gt;
&lt;pre&gt;00000000  sub         esp,8
00000003  xor         eax,eax
00000005  mov         dword ptr [esp],eax
00000008  mov         dword ptr [esp+4],eax
0000000c  mov         byte ptr [esp],0
00000010  movsx       eax,byte ptr [esp]
00000014  mov         byte ptr [esp+4],al
00000018  add         ecx,edx
0000001a  mov         eax,ecx
0000001c  add         esp,8
0000001f  ret              &lt;/pre&gt;
&lt;p&gt;This generates code to initialize the policy
structure we don&apos;t actually use. We can get rid of that by recasting this a bit to save the dummy &lt;code&gt;&lt;strong&gt;struct&lt;/strong&gt;&lt;/code&gt; allocation by putting the
&lt;code&gt;Add()&lt;/code&gt; method in a wrapping class that takes the type parameters.
The type would allocate the &lt;code&gt;&lt;strong&gt;struct&lt;/strong&gt;&lt;/code&gt; instead of the
method. This looks
like,&lt;/p&gt;
&lt;pre&gt;&lt;b&gt;class&lt;/b&gt; Adder&amp;lt;T, P&amp;gt;
    &lt;strong&gt;where&lt;/strong&gt; P: &lt;b&gt;struct&lt;/b&gt;, IAddPolicy&amp;lt;T&amp;gt;
{
    &lt;b&gt;static&lt;/b&gt; P AddPolicy = &lt;b&gt;new&lt;/b&gt; P();
    &lt;b&gt;static&lt;/b&gt; &lt;b&gt;public&lt;/b&gt; T Add(T a, T b) { &lt;b&gt;return&lt;/b&gt; AddPolicy.Add(a, b); }
}&lt;/pre&gt;
&lt;p&gt;which generates the following code for &lt;code&gt;Add()&lt;/code&gt;,&lt;/p&gt;
&lt;pre&gt;00000000  add         ecx,edx
00000002  mov         eax,ecx
00000004  ret              &lt;/pre&gt;
&lt;p&gt;This can be called like,&lt;/p&gt;
&lt;pre&gt;&lt;strong&gt;var&lt;/strong&gt; i2 = Adder&amp;lt;&lt;b&gt;int&lt;/b&gt;, IntAddPolicy&amp;gt;.Add(3, 4);&lt;/pre&gt;
&lt;p&gt;Note that the code in &lt;code&gt;IntAddPolicy&lt;/code&gt; gets inlined into the &lt;code&gt;
Add()&lt;/code&gt; method. Even though this doesn&apos;t get inlined into the caller of
&lt;code&gt;Add(),&lt;/code&gt; it will still be pretty quick with the branch prediction and
register aliasing of modern processors. This means that you are not paying much
(and in many cases nothing) in code quality for using an &lt;code&gt;AddPolicy&lt;/code&gt; over writing out the &lt;code&gt;
Add()&lt;/code&gt; method explicitly.&lt;/p&gt;
&lt;p&gt;To use the &lt;code&gt;Add()&lt;/code&gt; method above with another type, such as &lt;code&gt;
&lt;strong&gt;double&lt;/strong&gt;&lt;/code&gt;, you need to create another
add policy &lt;code&gt;&lt;strong&gt;struct&lt;/strong&gt;&lt;/code&gt;. This would look like,&lt;/p&gt;
&lt;pre&gt;&lt;b&gt;struct&lt;/b&gt; DoubleAddPolicy : IAddPolicy&amp;lt;&lt;b&gt;double&lt;/b&gt;&amp;gt; {
    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;double&lt;/b&gt; Add(&lt;b&gt;double&lt;/b&gt; a, &lt;b&gt;double&lt;/b&gt; b) { &lt;b&gt;return&lt;/b&gt; a + b; }
}&lt;/pre&gt;
&lt;p&gt;Using this &lt;code&gt;&lt;strong&gt;struct&lt;/strong&gt;&lt;/code&gt; to call &lt;code&gt;Add()&lt;/code&gt;
will produce the following code for the &lt;code&gt;Add()&lt;/code&gt; method,&lt;/p&gt;
&lt;pre&gt;00000000  fld         qword ptr [esp+0Ch]
00000004  fadd        qword ptr [esp+4]
00000008  ret         10h  &lt;/pre&gt;
&lt;p&gt;This is not quite as good as the code generated for &lt;code&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/code&gt;
because it uses the stack to pass the parameters but it is still pretty fast.&lt;/p&gt;
&lt;p&gt;We now have a method that can add two values of any type &lt;code&gt;T&lt;/code&gt; for
which an &lt;code&gt;AddPolicy&lt;/code&gt; can be created. This is pretty close to, and
more general than, what I originally wanted. Unfortunately, even though the code
generated is good, calling this code is very awkward; we have to call a static
method of a parameterized class and the policy &lt;code&gt;&lt;strong&gt;struct&lt;/strong&gt;&lt;/code&gt;
prevents the compiler from being able to use type inferencing to supply the
parameters for us. Next time I will describe a more practical application of
this technique where the awkwardness is not as noticiable.&lt;/p&gt;
&lt;/div&gt;
</description>
                <link>http://removingalldoubt.com/programming/2007/07/25/Operators-Generics-and-Policies</link>
                <guid>http://removingalldoubt.com/programming/2007/07/25/Operators-Generics-and-Policies</guid>
                <pubDate>2007-07-25T22:46:00+00:00</pubDate>
        </item>

        <item>
                <title>C# Mixins - Sort of</title>
                <description>&lt;div class=&quot;text&quot;&gt;&lt;p&gt;I recently needed to create a set of classes that was the combinatorial
expansion of several implementation flavors of two independent sets of methods. Why I needed
combinatorial expansion is not that interesting but the solution is. What I came
up with is a way of supporting a form of
&lt;a href=&quot;http://en.wikipedia.org/wiki/Mixin&quot;&gt;mixin&lt;/a&gt;&apos;s in C#.&lt;/p&gt;
&lt;p&gt;Lets take the methods,&lt;/p&gt;
&lt;pre&gt;&lt;strong&gt;void&lt;/strong&gt; Lock();
&lt;strong&gt;bool&lt;/strong&gt; Unlock();
&lt;strong&gt;bool&lt;/strong&gt; Locked { &lt;strong&gt;get&lt;/strong&gt;; }&lt;/pre&gt;

&lt;p&gt;If you call &lt;code&gt;Lock()&lt;/code&gt;, &lt;code&gt;Locked&lt;/code&gt;
returns &lt;code&gt;&lt;strong&gt;true&lt;/strong&gt;&lt;/code&gt;, until you call &lt;code&gt;Unlock()&lt;/code&gt;. Also &lt;code&gt;Unlock()&lt;/code&gt;
returns &lt;code&gt;&lt;strong&gt;true&lt;/strong&gt;&lt;/code&gt; if the object is now unlocked. There are several
implementations I can think of for these methods, one uses a Boolean value and
doesn&apos;t support nested calls to &lt;code&gt;Lock()&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;strong&gt;void&lt;/strong&gt; Lock() {
  _locked = &lt;strong&gt;true&lt;/strong&gt;;
}

&lt;strong&gt;bool&lt;/strong&gt; Unlock() {
  _locked = &lt;strong&gt;false&lt;/strong&gt;;
  &lt;strong&gt;return&lt;/strong&gt; &lt;strong&gt;true&lt;/strong&gt;;
}

&lt;strong&gt;bool&lt;/strong&gt; Locked {
  &lt;strong&gt;get&lt;/strong&gt; { &lt;strong&gt;return&lt;/strong&gt; _locked; }
}&lt;/pre&gt;

&lt;p&gt;Another uses an integer value to count the number of nested calls to &lt;code&gt;
Lock()&lt;/code&gt; and it remains locked until a matching number of &lt;code&gt;Unlock()&lt;/code&gt;
calls are made.&lt;/p&gt;
&lt;pre&gt;&lt;strong&gt;void&lt;/strong&gt; Lock() {
  _count++;
}

&lt;strong&gt;void&lt;/strong&gt; Unlock() {
  _count--;
  &lt;strong&gt;return&lt;/strong&gt; _count == 0;
}

&lt;strong&gt;void&lt;/strong&gt; Locked() {
  &lt;strong&gt;return&lt;/strong&gt; _count &amp;gt; 0;
}&lt;/pre&gt;
&lt;p&gt;This isn&apos;t thread safe so we could provide a relatively thread-safe version
in,&lt;/p&gt;
&lt;pre&gt;&lt;b&gt;void&lt;/b&gt; Lock() {
    System.Threading.Interlocked.Increment(&lt;b&gt;ref&lt;/b&gt; _count);
}

&lt;b&gt;bool&lt;/b&gt; Unlock() {
    &lt;b&gt;return&lt;/b&gt; System.Threading.Interlocked.Decrement(&lt;b&gt;ref&lt;/b&gt; _count) == 0;
}

&lt;b&gt;bool&lt;/b&gt; Locked {
    get { &lt;b&gt;return&lt;/b&gt; _count &amp;gt; 0; }
}&lt;/pre&gt;

&lt;p&gt;I am sure you have already thought of at least one more. Now I have four
possible implementations (including yours), which is the correct one? Unfortunately, that depends
on how the locking is going to be used. If I know I will never need nested
locks, only taking space for a Boolean makes sense. If I know I will need to
support nested locks then the &lt;code&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/code&gt; seems like a
good choice. If it is going to be used in a multi-threaded application I should
probably use the thread-safe version. I now have two choices, pick one
implementation or pass it the implementation as a parameter. Picking one
implementation is easy, just copy of of the above implementation into the class; but how do you efficiently pass an implementation as a
parameter? &lt;/p&gt;
&lt;p&gt;From now on I will refer to each implementation as a locking policy. What I
want is an efficient way to pass the locking policy to a set of classes. Most obvious way is to
write a class for each policy and pass the policy
as a parameter to each instance. This might look something like,&lt;/p&gt;
&lt;pre&gt;&lt;strong&gt;abstract&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; LockingPolicy {
  &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;abstract&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; Lock();
  &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;abstract&lt;/strong&gt; &lt;strong&gt;bool&lt;/strong&gt; Unlock();
  &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;abstract&lt;/strong&gt; &lt;strong&gt;bool&lt;/strong&gt; Locked { &lt;strong&gt;get&lt;/strong&gt;; }
}

&lt;strong&gt;class&lt;/strong&gt; LockableObject {
    &lt;strong&gt;private&lt;/strong&gt; LockingPolicy _lockingPolicy;

    &lt;strong&gt;public&lt;/strong&gt; LockableObject(LockingPolicy lockingPolicy) {
        _lockingPolicy = lockingPolicy;
    }

    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Lock() {
        _lockingPolicy.Lock();
    }

    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;bool&lt;/b&gt; Unlock() {
        &lt;b&gt;return&lt;/b&gt; _lockingPolicy.Unlock();
    }

    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;bool&lt;/b&gt; Locked {
        get { &lt;b&gt;return&lt;/b&gt; _lockingPolicy.Locked; }
    }
}&lt;/pre&gt;

&lt;p&gt;Unfortunately, this has, on average, 36 bytes of overhead per instance on
a 32 bit machine (more on 64).&amp;nbsp; This seems a bit much for such a policy and picking just one
implementation, even one too general for the average application, starts
looking like a better alternative. We must be able to do better than this.&lt;/p&gt;
&lt;p&gt;Since passing the implementation as a parameter to the instances is
expensive, why not try to pass the implementation to the class instead? In order
to do that we need use a type parameter. What we want is something like,&lt;/p&gt;
&lt;pre&gt;&lt;b&gt;class&lt;/b&gt; LockableObject&amp;lt;LockingPolicy&amp;gt; {
    &lt;b&gt;private&lt;/b&gt; LockingPolicy _lockingPolicy = &lt;b&gt;new&lt;/b&gt; LockingPolicy();

    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Lock() {
        _lockingPolicy.Lock();
    }

    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;bool&lt;/b&gt; Unlock() {
        &lt;b&gt;return&lt;/b&gt; _lockingPolicy.Unlock();
    }

    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;bool&lt;/b&gt; Locked {
        get { &lt;b&gt;return&lt;/b&gt; _lockingPolicy.Locked; }
    }
}&lt;/pre&gt;
&lt;p&gt;In other words, we need to passing a &lt;code&gt;&lt;strong&gt;class&lt;/strong&gt;&lt;/code&gt; and delegate the implementation to
that &lt;code&gt;&lt;strong&gt;class&lt;/strong&gt;&lt;/code&gt;. This does not look significantly better than the previous
implementation, however, because we are new&apos;ing up an instance and assigning it
to an instance variable which, as before, consumes a minimum of 36 bytes per
instance. But if &lt;code&gt;LockingPolicy&lt;/code&gt; is a &lt;code&gt;&lt;strong&gt;struct&lt;/strong&gt;&lt;/code&gt; instead
of a &lt;code&gt;&lt;strong&gt;class&lt;/strong&gt;&lt;/code&gt; then it will only add the size of the
&lt;code&gt;&lt;strong&gt;struct&lt;/strong&gt;&lt;/code&gt; to the &lt;code&gt;LockableObject&lt;/code&gt; instance
size. In other words, we would only be taking space for either a &lt;code&gt;&lt;strong&gt;bool&lt;/strong&gt;&lt;/code&gt;
or an &lt;code&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/code&gt;. The size overhead looks good, now how
about the delegation? In order to call methods on &lt;code&gt;LockingPolicy&lt;/code&gt; the
&lt;code&gt;LockingPolicy&lt;/code&gt; &lt;code&gt;&lt;strong&gt;struct&lt;/strong&gt;&lt;/code&gt; must implement
an interface. It cannot be an &lt;code&gt;&lt;strong&gt;abstract&lt;/strong&gt;&lt;/code&gt;
&lt;code&gt;&lt;strong&gt;class&lt;/strong&gt;&lt;/code&gt; because all &lt;code&gt;&lt;strong&gt;struct&lt;/strong&gt;&lt;/code&gt;s
are sealed and cannot be inherited from. The interface for the locking policy is fairly straight
forward and looks like,&lt;/p&gt;
&lt;pre&gt;&lt;b&gt;interface&lt;/b&gt; ILockingPolicy {
    &lt;b&gt;void&lt;/b&gt; Lock();
    &lt;b&gt;bool&lt;/b&gt; Unlock();
    &lt;b&gt;bool&lt;/b&gt; Locked { get; }
}&lt;/pre&gt;
&lt;p&gt;An implementation of the Boolean locking policy looks like,&lt;/p&gt;
&lt;pre&gt;&lt;b&gt;struct&lt;/b&gt; SimpleLockPolicy : ILockingPolicy {
    &lt;b&gt;bool&lt;/b&gt; _locked;

    &lt;b&gt;void&lt;/b&gt; ILockingPolicy.Lock() {
        _locked = &lt;b&gt;true&lt;/b&gt;;
    }
    &lt;b&gt;bool&lt;/b&gt; ILockingPolicy.Unlock() {
        _locked = &lt;b&gt;false&lt;/b&gt;;
        &lt;b&gt;return&lt;/b&gt; &lt;b&gt;false&lt;/b&gt;;
    }
    &lt;b&gt;bool&lt;/b&gt; ILockingPolicy.Locked {
        get { &lt;b&gt;return&lt;/b&gt; _locked; }
    }
}&lt;/pre&gt;
&lt;p&gt;The counted locking policy looks like,&lt;/p&gt;
&lt;pre&gt;&lt;b&gt;struct&lt;/b&gt; CountedLockPolicy : ILockingPolicy {
    &lt;b&gt;private&lt;/b&gt; &lt;b&gt;int&lt;/b&gt; _count;
    &lt;b&gt;void&lt;/b&gt; ILockingPolicy.Lock() {
        _count++;
    }
    &lt;b&gt;bool&lt;/b&gt; ILockingPolicy.Unlock() {
        _count--;
        &lt;b&gt;return&lt;/b&gt; _count == 0;
    }
    &lt;b&gt;bool&lt;/b&gt; ILockingPolicy.Locked {
        get { &lt;b&gt;return&lt;/b&gt; _count &amp;gt; 0; }
    }
}&lt;/pre&gt;
&lt;p&gt;We now need to add a &lt;code&gt;&lt;strong&gt;where&lt;/strong&gt;&lt;/code&gt; clause to the &lt;code&gt;LockableObject&lt;/code&gt;
class so we can call the methods provided by the &lt;code&gt;&lt;strong&gt;struct&lt;/strong&gt;&lt;/code&gt;&apos;s methods. The modified &lt;code&gt;LockableObject&lt;/code&gt;
class looks like,&lt;/p&gt;
&lt;pre&gt;&lt;b&gt;class&lt;/b&gt; LockableObject&amp;lt;LockingPolicy&amp;gt;
    &lt;strong&gt;where&lt;/strong&gt; LockingPolicy : &lt;b&gt;struct&lt;/b&gt;, ILockingPolicy
{
    &lt;b&gt;private&lt;/b&gt; LockingPolicy _lockingPolicy = &lt;b&gt;new&lt;/b&gt; LockingPolicy();

    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Lock() {
        _lockingPolicy.Lock();
    }

    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;bool&lt;/b&gt; Unlock() {
        &lt;b&gt;return&lt;/b&gt; _lockingPolicy.Unlock();
    }

    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;bool&lt;/b&gt; Locked {
        get { &lt;b&gt;return&lt;/b&gt; _lockingPolicy.Locked; }
    }
}&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;LockableObject&lt;/code&gt; can now be instantiated like,&lt;/p&gt;
&lt;pre&gt;&lt;strong&gt;var&lt;/strong&gt; lockableObject = &lt;b&gt;new&lt;/b&gt; LockableObject&amp;lt;SimpleLockPolicy&amp;gt;();&lt;/pre&gt;
&lt;p&gt;for a lockable object that uses the Boolean implementation. To use the
counted locking policy you would construct the object like,&lt;/p&gt;
&lt;pre&gt;&lt;strong&gt;var&lt;/strong&gt; lockableObject = &lt;b&gt;new&lt;/b&gt; LockableObject&amp;lt;CountedLockPolicy&amp;gt;();&lt;/pre&gt;

&lt;p&gt;The space consumption looks good but the delegation looks expensive it call
through an interface which is a virtual. But since we are using &lt;code&gt;&lt;strong&gt;
struct&lt;/strong&gt;&lt;/code&gt;s, which are &lt;code&gt;&lt;strong&gt;sealed&lt;/strong&gt;&lt;/code&gt;, the
call through the &lt;code&gt;&lt;strong&gt;interface&lt;/strong&gt;&lt;/code&gt; can be made static
and then be a candidate for inlining. In fact,
this is what happens in retail builds when not debugging. This is not entirely
free however since there the runtime doesn&apos;t inline twice. Implementing &lt;code&gt;
Lock()&lt;/code&gt; directly instead of through delegation means the &lt;code&gt;Lock()&lt;/code&gt;
method can be inlined, removing the call entirely, the delegated version inlines
the delegation but it still generates a call.&lt;/p&gt;
&lt;p&gt;This mechanism for C# mixins is not as convenient as languages that support
multiple inheritance, because we have to manually delegate to the policy, but it
is just as efficient as we will see demonstrated more clearly in some of the
next entries.&lt;/p&gt;
&lt;/div&gt;
</description>
                <link>http://removingalldoubt.com/programming/2007/07/02/C-Mixins-Sort-of</link>
                <guid>http://removingalldoubt.com/programming/2007/07/02/C-Mixins-Sort-of</guid>
                <pubDate>2007-07-02T23:42:00+00:00</pubDate>
        </item>

        <item>
                <title>Keyed Binary Search</title>
                <description>&lt;div class=&quot;text&quot;&gt;&lt;p&gt;One thing that has always frustrated me about typical binary search routines
found in libraries is they always assume you can easily produce a copy of the
item you are searching for. What I mean is they typically follow this pattern,&lt;/p&gt;
&lt;pre&gt;&lt;strong&gt;int&lt;/strong&gt; BinarySearch&amp;lt;T&amp;gt;(IList&amp;lt;T&amp;gt; list, T item);&lt;/pre&gt;

&lt;p&gt;which takes a list of some type, left open here, and searches for it in the
list. This is great if the very next thing you do next is insert &lt;code&gt;item&lt;/code&gt; into
the list. You already have &lt;code&gt;item&lt;/code&gt; in hand so this signature is
perfect. But if you
don&apos;t have a ready &lt;code&gt;item&lt;/code&gt;, and &lt;code&gt;item&lt;/code&gt; is difficult to
produce, you are stuck. &lt;/p&gt;
&lt;p&gt;This happened to me just recently in my code. I had a
sorted list of a data structure representing a span of source, similar to a &lt;code&gt;TextPointer&lt;/code&gt; from WPF, called a
&lt;code&gt;TextRange&lt;/code&gt;. My version can quickly return the
offset of the beginning of the span but, since &lt;code&gt;TextRange&lt;/code&gt; tracks source changes,
creating a new &lt;code&gt;TextRange&lt;/code&gt; is expensive. When text in the editor is
change, I receive the location of
the change in an event. How do I figure out which one of my &lt;code&gt;TextRange&lt;/code&gt;s was modified? Since
the list is sorted, the obvious choice is to use a binary search which yields O(Log N)
performance. Since my &lt;code&gt;TextRange&lt;/code&gt;s are store in a &lt;code&gt;List&amp;lt;TextRange&amp;gt;&lt;/code&gt;,
obviously I should
call &lt;code&gt;List&amp;lt;T&amp;gt;.BinarySearch()&lt;/code&gt;. Unfortunately, &lt;code&gt;TextRange&lt;/code&gt;s are expensive to produce;
too expensive for a per-keystroke operation (I am glossing over some details for
the sake of brevity; you need to trust me this is true). I have one of two choices, write my
own &lt;code&gt;BinarySearch()&lt;/code&gt; or create a fake &lt;code&gt;TextRange&lt;/code&gt; that is a special
&lt;code&gt;TextRange&lt;/code&gt; that
is less expensive to create and only used for searching. Neither choice is
pleasant. Hand-rolled binary searches are not hard to right but, for some
strange reason, I always get them wrong the first time.&lt;/p&gt;
&lt;p&gt;What I needed is prebuilt and tested version of &lt;code&gt;BinarySearch()&lt;/code&gt; that looks more like,&lt;/p&gt;
&lt;pre&gt;&lt;strong&gt;int&lt;/strong&gt; BinarySearch&amp;lt;T, K&amp;gt;(IList&amp;lt;T&amp;gt; list, K key, Converter&amp;lt;T, K&amp;gt; converter);&lt;/pre&gt;

&lt;p&gt;What this binary search routine does is find some key value in the list
given a function that can convert any list item into the key. In my example, I
would call it like,&lt;/p&gt;
&lt;pre&gt;&lt;strong&gt;int&lt;/strong&gt; location = BinarySearch(textRangeList, location, TextRangeToLocation);&lt;/pre&gt;

&lt;p&gt;where &lt;code&gt;TextRangeToLocation&lt;/code&gt; looks something like,&lt;/p&gt;
&lt;pre&gt;&lt;strong&gt;int&lt;/strong&gt; TextRangeToLocation(TextRange range) { &lt;strong&gt;return&lt;/strong&gt; range.Location; }&lt;/pre&gt;

&lt;p&gt;I don&apos;t need to produce a &lt;code&gt;TextRange&lt;/code&gt;. I have the location I am looking for
handy, I just need to know what, if any, &lt;code&gt;TextRange&lt;/code&gt; contains the
location. This method
would do just that. You can even use a lambda function in C# 3.0 to get this
all on one line,&lt;/p&gt;
&lt;pre&gt;&lt;strong&gt;int&lt;/strong&gt; location = BinarySearch(textRangeList, location, (n) =&amp;gt; n.Location);&lt;/pre&gt;
&lt;p&gt;This is starting to look like a good idea. Let&apos;s code it up. Here is the
method I ended up with,&lt;/p&gt;
&lt;pre&gt;&lt;b&gt;public&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; &lt;b&gt;int&lt;/b&gt; BinarySearch&amp;lt;T, K&amp;gt;(IList&amp;lt;T&amp;gt; list, K value,
    Converter&amp;lt;T, K&amp;gt; convert, Comparison&amp;lt;K&amp;gt; compare) {
    &lt;b&gt;int&lt;/b&gt; i = 0;
    &lt;b&gt;int&lt;/b&gt; j = list.Count - 1;
    &lt;b&gt;while&lt;/b&gt; (i &amp;lt;= j) {
        &lt;b&gt;int&lt;/b&gt; m = i + (j - i) / 2;
        &lt;b&gt;int&lt;/b&gt; c = compare(convert(list[m]), value);
        &lt;b&gt;if&lt;/b&gt; (c == 0)
            &lt;b&gt;return&lt;/b&gt; m;
        &lt;b&gt;if&lt;/b&gt; (c &amp;lt; 0)
            i = m + 1;
        &lt;b&gt;else&lt;/b&gt;
            j = m - 1;
    }
    &lt;b&gt;return&lt;/b&gt; ~i;
}&lt;/pre&gt;


&lt;p&gt;I added an extra parameter, &lt;code&gt;compare&lt;/code&gt;. This is required because I
didn&apos;t put any limits on what &lt;code&gt;K&lt;/code&gt; can be and I need a way to compare
one &lt;code&gt;K&lt;/code&gt; with another. Many types, such as &lt;code&gt;int&lt;/code&gt;, already know how to
compare values of their own type. Typically types that are comparable, such as
&lt;code&gt;int&lt;/code&gt;,&lt;code&gt; string&lt;/code&gt;, etc., implement
&lt;code&gt;IComparable&amp;lt;T&amp;gt;&lt;/code&gt;. We can accommodate such types by creating an
overloaded version of &lt;code&gt;BinarySearch()&lt;/code&gt; that looks like, &lt;/p&gt;
&lt;pre&gt;&lt;b&gt;public&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; &lt;b&gt;int&lt;/b&gt; BinarySearch&amp;lt;T, K&amp;gt;(IList&amp;lt;T&amp;gt; list, K value,
    Converter&amp;lt;T, K&amp;gt; convert) &lt;strong&gt;where&lt;/strong&gt; K : IComparable&amp;lt;K&amp;gt; {
    &lt;b&gt;return&lt;/b&gt; list.BinarySearch(value, convert, (n, m) =&amp;gt; n.CompareTo(m));
}&lt;/pre&gt;


&lt;p&gt;which uses a lambda function to create a &lt;code&gt;Comparison&amp;lt;T&amp;gt;&lt;/code&gt;  for any
type that supports &lt;code&gt;IComparable&amp;lt;T&amp;gt;&lt;/code&gt;. Beware that this doesn&apos;t
work well with reference types because it doesn&apos;t check for &lt;strong&gt;&lt;code&gt;null&lt;/code&gt;&lt;/strong&gt;.
You can make it safer by putting &lt;strong&gt;&lt;code&gt;struct&lt;/code&gt;&lt;/strong&gt; in the
&lt;strong&gt;&lt;code&gt;where&lt;/code&gt;&lt;/strong&gt; clause but that seemed overly restrictive to
me.&lt;/p&gt;
&lt;p&gt;Now that I have a &lt;code&gt;BinarySearch()&lt;/code&gt; that meets my needs better than the built-in
&lt;code&gt;BinarySearch()&lt;/code&gt;, my next thought was I should evangelize the benefits my version
of &lt;code&gt;BinarySearch()&lt;/code&gt; to the team responsible for maintaining the CLR
collections and, maybe, someday, I will have it as
a native part of the CLR; maybe, someday. Then I remember extensions methods. In
C# 3.0, with a slight modification to the above methods, they can appear as if
they were already part of the CLR.&lt;/p&gt;
&lt;pre&gt;&lt;b&gt;public&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; ListExtensionMethods {
    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; &lt;b&gt;int&lt;/b&gt; BinarySearch&amp;lt;T, K&amp;gt;(&lt;b&gt;this&lt;/b&gt; IList&amp;lt;T&amp;gt; list, K value,
        Converter&amp;lt;T, K&amp;gt; convert, Comparison&amp;lt;K&amp;gt; compare) {
        &lt;b&gt;int&lt;/b&gt; i = 0;
        &lt;b&gt;int&lt;/b&gt; j = list.Count - 1;
        &lt;b&gt;while&lt;/b&gt; (i &amp;lt;= j) {
            &lt;b&gt;int&lt;/b&gt; m = i + (j - i) / 2;
            &lt;b&gt;int&lt;/b&gt; c = compare(convert(list[m]), value);
            &lt;b&gt;if&lt;/b&gt; (c == 0)
                &lt;b&gt;return&lt;/b&gt; m;
            &lt;b&gt;if&lt;/b&gt; (c &amp;lt; 0)
                i = m + 1;
            &lt;b&gt;else&lt;/b&gt;
                j = m - 1;
        }
        &lt;b&gt;return&lt;/b&gt; ~i;
    }

    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; &lt;b&gt;int&lt;/b&gt; BinarySearch&amp;lt;T, K&amp;gt;(&lt;b&gt;this&lt;/b&gt; IList&amp;lt;T&amp;gt; list, K value,
        Converter&amp;lt;T, K&amp;gt; convert) where K : IComparable&amp;lt;K&amp;gt; {
        &lt;b&gt;return&lt;/b&gt; list.BinarySearch(value, convert, (n, m) =&amp;gt; n.CompareTo(m));
    }
}&lt;/pre&gt;
&lt;p&gt;My version of &lt;code&gt;BinarySearch()&lt;/code&gt; now
appears as part of the methods of any type that implements &lt;code&gt;IList&amp;lt;T&amp;gt;&lt;/code&gt;.
This allows us to write,&lt;/p&gt;
&lt;pre&gt;&lt;strong&gt;int&lt;/strong&gt; location = textRangeList.BinarySearch(location, (n) =&amp;gt; n.Location);&lt;/pre&gt;
&lt;p&gt;just like it was a native part of the CLR. No more hand-rolled &lt;code&gt;
BinarySearch()&lt;/code&gt;s!&lt;/p&gt;
&lt;p&gt;Now that we have a &lt;code&gt;ListExtensionMethods &lt;/code&gt;class, what other missing methods
could we add? Here are a few that I thought would be useful,&lt;/p&gt;
&lt;pre&gt;&lt;b&gt;public&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Sort&amp;lt;T, K&amp;gt;(&lt;b&gt;this&lt;/b&gt; List&amp;lt;T&amp;gt; list,
    Converter&amp;lt;T, K&amp;gt; coverter, Comparison&amp;lt;K&amp;gt; compare) {
    list.Sort((n, m) =&amp;gt; compare(convert(n), convert(m)));
}

&lt;b&gt;public&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Sort&amp;lt;T, K&amp;gt;(&lt;b&gt;this&lt;/b&gt; List&amp;lt;T&amp;gt; list,
    Converter&amp;lt;T, K&amp;gt;  converter) &lt;b&gt;where&lt;/b&gt; K : IComparable&amp;lt;K&amp;gt; {
    list.Sort((n, m) =&amp;gt; convert(n).CompareTo(convert(m)));
}

&lt;b&gt;public&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Swap&amp;lt;T&amp;gt;(&lt;b&gt;this&lt;/b&gt; IList&amp;lt;T&amp;gt; list, &lt;b&gt;int&lt;/b&gt; index1,
    &lt;b&gt;int&lt;/b&gt; index2) {
    var value = list[index1];
    list[index1] = list[index2];
    list[index2] = value;
}

&lt;b&gt;public&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Shuffle&amp;lt;T&amp;gt;(&lt;b&gt;this&lt;/b&gt; IList&amp;lt;T&amp;gt; list) {
    &lt;b&gt;int&lt;/b&gt; count = list.Count;
    Random r = &lt;b&gt;new&lt;/b&gt; Random();
    &lt;b&gt;for&lt;/b&gt;(&lt;b&gt;int&lt;/b&gt; i = count - 1; i &amp;gt; 1; i--)
        list.Swap(i, r.Next(i - 1));
}&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;Sort()&lt;/code&gt; methods above are allow you to extract a key for
sorting using the same methods as above. These are not strictly necessary since
constructing the lambda function I use is not that complicated but I believe they
make the code more readable if you are consistent in how you extract keys from a
list, so these are convenient wrappers that allow you to use the same methods to
sort and search. &lt;code&gt;Swap()&lt;/code&gt; and
&lt;code&gt;Shuffle()&lt;/code&gt; are throw-ins. They are really only useful for demo code
or, in my case, test code. I used them to test the &lt;code&gt;Sort()&lt;/code&gt; method
which I used to test the &lt;code&gt;BinarySearch()&lt;/code&gt; method.&lt;/p&gt;
&lt;/div&gt;
</description>
                <link>http://removingalldoubt.com/programming/2007/05/24/Keyed-Binary-Search</link>
                <guid>http://removingalldoubt.com/programming/2007/05/24/Keyed-Binary-Search</guid>
                <pubDate>2007-05-24T16:44:00+00:00</pubDate>
        </item>

        <item>
                <title>Pick a License</title>
                <description>&lt;p&gt;Reading &lt;a href=&quot;http://www.codinghorror.com/blog/archives/000833.html&quot;&gt;Pick
a License, Any License&lt;/a&gt; by Jeff Atwood inspired me to pick one for the code I
post on this blog. I picked
&lt;a href=&quot;http://opensource.org/licenses/MS-PL&quot;&gt;
MS-PL&lt;/a&gt;. I certainly intended people to use the code I post, this make that
intent more clear. I have modified the my copyright footer to reflect this
selection.&lt;/p&gt;

&lt;p class=&quot;edit&quot;&gt;[EDIT 01/21/2015: This has changed to be the Apache license or
the Ms-PL. MS-PL url was also updated.] &lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/2007/04/04/Pick-a-License</link>
                <guid>http://removingalldoubt.com/programming/2007/04/04/Pick-a-License</guid>
                <pubDate>2007-04-04T10:11:00+00:00</pubDate>
        </item>

        <item>
                <title>PolyDictionary IV: By extension</title>
                <description>&lt;p&gt;The technique we used to build &lt;code&gt;PolyDictionary&lt;/code&gt; can be used in other 
places. For example, you can adapt a non-type-checked store to a
type-checked store by supplying your own &lt;code&gt;Get() &lt;/code&gt;and &lt;code&gt;Set()&lt;/code&gt;
methods. One place this might be useful is when you encounter something like
CodeDom&apos;s &lt;code&gt;UserData&lt;/code&gt;. It is intended to allow users or producers
of a CodeDom to annotate a
CodeDom with their own information. Because it uses &lt;code&gt;IDictionary,&lt;/code&gt;
using &lt;code&gt;UserData&lt;/code&gt; is not type checked at compile
time. You can introduce type checking by using your own methods to access the
dictionary like,&lt;/p&gt;
&lt;pre&gt;  &lt;b&gt;public&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; T Get&amp;lt;T&amp;gt;(IDictionary dictionary, Key&amp;lt;T&amp;gt; key) {
      &lt;b&gt;return&lt;/b&gt; (T)dictionary[key];
  }

  &lt;b&gt;public&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Set&amp;lt;T&amp;gt;(IDictionary dictionary, Key&amp;lt;T&amp;gt; key, T value) {
      dictionary[key] = value;
  }&lt;/pre&gt;
&lt;p&gt;which uses the &lt;code&gt;Key&amp;lt;T&amp;gt;&lt;/code&gt; class from last time. This allows
the compiler to ensure that the type of the value you get out is the same as you
put in and vise versa.&lt;/p&gt;
&lt;p&gt;With C# 3.0, however, you can get even more creative. You can create extension
methods for &lt;code&gt;IDictionary&lt;/code&gt; that makes it appear that all &lt;code&gt;IDictionary&lt;/code&gt;s have a type
checked &lt;code&gt;Get()&lt;/code&gt; and &lt;code&gt;Set()&lt;/code&gt; like,&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;static&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; IDictionaryExtensions {
        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; T Get&amp;lt;T&amp;gt;(&lt;b&gt;this&lt;/b&gt; IDictionary dictionary, Key&amp;lt;T&amp;gt; key) {
            &lt;b&gt;return&lt;/b&gt; (T)dictionary[key];
        }
        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Set&amp;lt;T&amp;gt;(&lt;b&gt;this&lt;/b&gt; IDictionary dictionary, Key&amp;lt;T&amp;gt; key, T value) {
            dictionary[key] = value;
        }
    }&lt;/pre&gt;
&lt;p&gt;This allows you to write code like,&lt;/p&gt;
&lt;pre&gt;    Key&amp;lt;&lt;b&gt;string&lt;/b&gt;&amp;gt; k1 = &lt;b&gt;new&lt;/b&gt; Key&amp;lt;&lt;b&gt;string&lt;/b&gt;&amp;gt;();
    Key&amp;lt;&lt;b&gt;int&lt;/b&gt;&amp;gt; k2 = &lt;b&gt;new&lt;/b&gt; Key&amp;lt;&lt;b&gt;int&lt;/b&gt;&amp;gt;();
    Key&amp;lt;&lt;b&gt;double&lt;/b&gt;&amp;gt; k3 = &lt;b&gt;new&lt;/b&gt; Key&amp;lt;&lt;b&gt;double&lt;/b&gt;&amp;gt;();

    IDictionary dictionary = &lt;b&gt;new&lt;/b&gt; Hashtable();

    dictionary.Set(k1, &quot;one&quot;);
    dictionary.Set(k2, 2);
    dictionary.Set(k3, 3.33);

    &lt;b&gt;string&lt;/b&gt; v1 = dictionary.Get(k1);
    &lt;b&gt;int&lt;/b&gt; v2 = dictionary.Get(k2);
    &lt;b&gt;double&lt;/b&gt; v3 = dictionary.Get(k3);

    Console.WriteLine(&quot;v1 = {0}&quot;, v1);
    Console.WriteLine(&quot;v2 = {0}&quot;, v2);
    Console.WriteLine(&quot;v3 = {0}&quot;, v3);&lt;/pre&gt;
&lt;p&gt;which allows us to use an &lt;code&gt;IDictionary&lt;/code&gt; almost identically to a
&lt;code&gt;PolyDictionary&lt;/code&gt;.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This works great for &lt;code&gt;Get()&lt;/code&gt; and &lt;code&gt;Set()&lt;/code&gt; but this
doesn&apos;t work for &lt;code&gt;Add()&lt;/code&gt;, unfortunately.
The compiler accepts it but it doesn&apos;t do what we want. If we use a value
of an incorrect type for a particular key we want the compiler to generate and
error. In other words, if we change the &lt;code&gt;Set()&lt;/code&gt; call for &lt;code&gt;k1&lt;/code&gt; above to,&lt;/p&gt;
&lt;pre&gt;  dictionary.&lt;span style=&quot;background-color:#FFFF00&quot;&gt;Add&lt;/span&gt;(k1, &lt;span style=&quot;background-color:#FFFF00&quot;&gt;1&lt;/span&gt;);&lt;/pre&gt;
&lt;p&gt;we want the compiler to generate an error. This doesn&apos;t happen because of method overloading. The object version of
Add,
&lt;code&gt;Add(&lt;strong&gt;object&lt;/strong&gt; value)&lt;/code&gt; is called if the type is incorrect; this is not what we want.
We could add an &lt;code&gt;AddSafe()&lt;/code&gt; method to the extension class but then we
would have to remember to call it instead of &lt;code&gt;Add()&lt;/code&gt;, which defeats
the purpose in my opinion.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/2007/03/31/PolyDictionary-IV-By-extension</link>
                <guid>http://removingalldoubt.com/programming/2007/03/31/PolyDictionary-IV-By-extension</guid>
                <pubDate>2007-03-31T23:06:00+00:00</pubDate>
        </item>

        <item>
                <title>PolyDictionary III: Cast away</title>
                <description>&lt;p&gt;The astute among you realized right away that &lt;code&gt;PolyDictionary&lt;/code&gt; 
does not really avoid a cast, it just hides it. The &lt;code&gt;TryGetValue()&lt;/code&gt;
has a cast in it, highlighted below,&lt;/p&gt;
&lt;pre&gt;        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;bool&lt;/b&gt; TryGetValue&amp;lt;K, V&amp;gt;(Key&amp;lt;K, V&amp;gt; key, &lt;b&gt;out&lt;/b&gt; V value) {
            &lt;b&gt;object&lt;/b&gt; objValue;
            &lt;b&gt;if&lt;/b&gt; (_table.TryGetValue(key, &lt;b&gt;out&lt;/b&gt; objValue)) {
                value = &lt;span style=&quot;background-color:#FFFF00&quot;&gt;(V)objValue&lt;/span&gt;;
                &lt;b&gt;return&lt;/b&gt; &lt;b&gt;true&lt;/b&gt;;
            }
            value = &lt;b&gt;default&lt;/b&gt;(V);
            &lt;b&gt;return&lt;/b&gt; &lt;b&gt;false&lt;/b&gt;;
        }&lt;/pre&gt;
&lt;p&gt;So, can we get rid of the this cast? Yes, but the results are not very
pleasant. One approach is to create one dictionary for each unique value type
and then find that dictionary by that type and index into it using the key,
instead of storing all the values in the same dictionary. This sounds similar to the
original problem! All we need is a &lt;code&gt;PolyDictionary&lt;/code&gt; that maps &lt;code&gt;
T&lt;/code&gt; to &lt;code&gt;Dictionary&amp;lt;Key&amp;lt;T&amp;gt;, T&amp;gt;&lt;/code&gt;. We could code this but we
couldn&apos;t run it because we would be defining &lt;code&gt;PolyDictionary&lt;/code&gt; in
terms of itself, consuming all of memory in the process. We need something like &lt;code&gt;
PolyDictionary&lt;/code&gt; but isn&apos;t a &lt;code&gt;PolyDictionary&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;OK, things will now get ugly. Have your barf bags on the stand-by! And, for
the love of God, don&apos;t ever put this in a production application! &lt;/p&gt;
&lt;p&gt;Now that is off my chest, here is one technique to avoid the cast. You can
create a nested class &lt;code&gt;SubDictionary&lt;/code&gt; that has a static instance
variable which maps &lt;code&gt;PolyDictionaries&lt;/code&gt; to &lt;code&gt;Dictionary&amp;lt;Key&amp;lt;T&amp;gt;, T&amp;gt;&lt;/code&gt;.
We can then parameterize &lt;code&gt;SubDictionary&lt;/code&gt; by &lt;code&gt;T&lt;/code&gt; which will
create a unique dictionary for each &lt;code&gt;T&lt;/code&gt; (I warned you it was ugly).
This is because the CLR treats every instantiation of &lt;code&gt;SubDictionary&amp;lt;T&amp;gt;&lt;/code&gt; as a
unique type with unique static variables. Essentially we are tricking the CLR
into producing a dictionary of &lt;code&gt;T&lt;/code&gt; to &lt;code&gt;Dictionary&amp;lt;PolyDictionary,Dictionary&amp;lt;Key&amp;lt;T&amp;gt;,T&amp;gt;&amp;gt;&lt;/code&gt;.
Since the variable is static we need to find the version that is for the
instance we are using, hence the key of &lt;code&gt;PolyDictionary&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Here is the example,&lt;/p&gt;
&lt;pre&gt;    /* Never, Never use this code! */
    &lt;b&gt;class&lt;/b&gt; PolyDictionary {

        &lt;b&gt;class&lt;/b&gt; SubDictionaries&amp;lt;T&amp;gt; {
            &lt;b&gt;internal&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; Dictionary&amp;lt;PolyDictionary, Dictionary&amp;lt;Key&amp;lt;T&amp;gt;, T&amp;gt;&amp;gt; Dictionaries =
                &lt;b&gt;new&lt;/b&gt; Dictionary&amp;lt;PolyDictionary,Dictionary&amp;lt;Key&amp;lt;T&amp;gt;,T&amp;gt;&amp;gt;();
        }

        &lt;b&gt;public&lt;/b&gt; PolyDictionary() { }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Add&amp;lt;T&amp;gt;(Key&amp;lt;T&amp;gt; key, T value) {
            Dictionary&amp;lt;Key&amp;lt;T&amp;gt;, T&amp;gt; subDictionary = GetDictionary(key);
            subDictionary.Add(key, value);
        }

        &lt;b&gt;public&lt;/b&gt; T Get&amp;lt;T&amp;gt;(Key&amp;lt;T&amp;gt; key) {
            Dictionary&amp;lt;Key&amp;lt;T&amp;gt;, T&amp;gt; subDictionary = FindDictionary(key);
            &lt;b&gt;if&lt;/b&gt; (subDictionary != &lt;b&gt;null&lt;/b&gt;)
                &lt;b&gt;return&lt;/b&gt; subDictionary[key];
            &lt;b&gt;throw&lt;/b&gt; &lt;b&gt;new&lt;/b&gt; KeyNotFoundException();
        }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;bool&lt;/b&gt; TryGetValue&amp;lt;T&amp;gt;(Key&amp;lt;T&amp;gt; key, &lt;b&gt;out&lt;/b&gt; T value) {
            Dictionary&amp;lt;Key&amp;lt;T&amp;gt;, T&amp;gt; subDictionary = FindDictionary(key);
            &lt;b&gt;if&lt;/b&gt; (subDictionary != &lt;b&gt;null&lt;/b&gt;)
                &lt;b&gt;return&lt;/b&gt; subDictionary.TryGetValue(key, &lt;b&gt;out&lt;/b&gt; value);
            &lt;b&gt;else&lt;/b&gt; {
                value = &lt;b&gt;default&lt;/b&gt;(T);
                &lt;b&gt;return&lt;/b&gt; &lt;b&gt;false&lt;/b&gt;;
            }
        }

        Dictionary&amp;lt;Key&amp;lt;T&amp;gt;, T&amp;gt; FindDictionary&amp;lt;T&amp;gt;(Key&amp;lt;T&amp;gt; key) {
            Dictionary&amp;lt;Key&amp;lt;T&amp;gt;, T&amp;gt; subDictionary;
            &lt;b&gt;if&lt;/b&gt; (!SubDictionaries&amp;lt;T&amp;gt;.Dictionaries.TryGetValue(&lt;b&gt;this&lt;/b&gt;, &lt;b&gt;out&lt;/b&gt; subDictionary))
                &lt;b&gt;return&lt;/b&gt; &lt;b&gt;null&lt;/b&gt;;
            &lt;b&gt;return&lt;/b&gt; subDictionary;
        }

        Dictionary&amp;lt;Key&amp;lt;T&amp;gt;, T&amp;gt; GetDictionary&amp;lt;T&amp;gt;(Key&amp;lt;T&amp;gt; key) {
            Dictionary&amp;lt;Key&amp;lt;T&amp;gt;, T&amp;gt; subDictionary;
            &lt;b&gt;if&lt;/b&gt; (!SubDictionaries&amp;lt;T&amp;gt;.Dictionaries.TryGetValue(&lt;b&gt;this&lt;/b&gt;, &lt;b&gt;out&lt;/b&gt; subDictionary)) {
                subDictionary = &lt;b&gt;new&lt;/b&gt; Dictionary&amp;lt;Key&amp;lt;T&amp;gt;, T&amp;gt;();
                SubDictionaries&amp;lt;T&amp;gt;.Dictionaries.Add(&lt;b&gt;this&lt;/b&gt;, subDictionary);
            }
            &lt;b&gt;return&lt;/b&gt; subDictionary;
        }
    }&lt;/pre&gt;

&lt;p&gt;This code has so many problems I don&apos;t know where to start. For one, it
never frees memory, the dictionaries are in memory and will never be
collected. I only include it to show that it can be done. As you can see,
sometimes it is better to have a cast. There might be better techniques that
eliminate some of the problems but my feeling is, why bother. The cast is not that
bad.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/2007/03/26/PolyDictionary-III-Cast-away</link>
                <guid>http://removingalldoubt.com/programming/2007/03/26/PolyDictionary-III-Cast-away</guid>
                <pubDate>2007-03-26T19:02:00+00:00</pubDate>
        </item>

        <item>
                <title>PolyDictionary II: Constructed Keys</title>
                <description>&lt;p&gt;In the last entry we defined a dictionary that can contain a value of any 
type and extra that value out of the dictionary without our code using a cast.
The dictionary itself used a cast (which I show a &quot;way&quot; to remove it next time) but our code
didn&apos;t. One problem it had, however, is it used declared keys instead of
constructed keys. A declared key is a key can only be referenced by knowing the
variable it is assigned to; a unique object. A constructed key, on the other
hand, uses object equality instead of object identity for the key. String keys
are constructed keys; you can read them from a file, calculate them or use a
literal value. The dictionary will eventually compare the strings
for equality. In other words, constructed keys can be constructed from data
where declared keys cannot.&lt;/p&gt;
&lt;p&gt;Can we modify &lt;code&gt;PolyDictionary&lt;/code&gt; to use constructed keys? One way to
do it is to create a new &lt;code&gt;Key&lt;/code&gt; type that takes the constructed key
type as a
parameter. One such type is, &lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;struct&lt;/b&gt; Key&amp;lt;K, V&amp;gt; {
        &lt;b&gt;public&lt;/b&gt; K Value;
        &lt;b&gt;public&lt;/b&gt; Key(K value) {
            &lt;b&gt;this&lt;/b&gt;.Value = value;
        }
        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; &lt;b&gt;implicit&lt;/b&gt; &lt;b&gt;operator&lt;/b&gt; Key&amp;lt;K, V&amp;gt;(K value) {
            &lt;b&gt;return&lt;/b&gt; &lt;b&gt;new&lt;/b&gt; Key&amp;lt;K, V&amp;gt;(value);
        }
    }&lt;/pre&gt;
&lt;p&gt;This &lt;code&gt;&lt;strong&gt;struct&lt;/strong&gt;&lt;/code&gt; just wraps the key value. I use a
&lt;code&gt;&lt;strong&gt;struct&lt;/strong&gt;&lt;/code&gt; instead of a &lt;code&gt;&lt;strong&gt;class&lt;/strong&gt;&lt;/code&gt;
because structs will do a value comparison for equality by default, classes use
reference equality.&lt;/p&gt;
&lt;p&gt;The existing methods of &lt;code&gt;PolyDictionary&lt;/code&gt;
do not take this kind of key; but, we can add methods to &lt;code&gt;PolyDictionary&lt;/code&gt; that do. For
example, we can create an &lt;code&gt;Add()&lt;/code&gt; method that looks like,&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Add&amp;lt;K, V&amp;gt;(Key&amp;lt;K, V&amp;gt; key, V value);&lt;/pre&gt;
&lt;p&gt;We can then create a &lt;code&gt;Get()&lt;/code&gt; method that looks like,&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;public&lt;/b&gt; V Get&amp;lt;K, V&amp;gt;(Key&amp;lt;K, V&amp;gt; key);&lt;/pre&gt;
&lt;p&gt;All these methods can be added directly to &lt;code&gt;PolyDictionary&lt;/code&gt;
because, through method overloading, they don&apos;t interfere with the original,
declared key versions.&lt;/p&gt;
&lt;p&gt;Using this dictionary looks like,&lt;/p&gt;
&lt;pre&gt;    Key&amp;lt;&lt;b&gt;string&lt;/b&gt;, &lt;b&gt;string&lt;/b&gt;&amp;gt; k1 = &quot;k1&quot;;
    Key&amp;lt;&lt;b&gt;string&lt;/b&gt;, &lt;b&gt;int&lt;/b&gt;&amp;gt; k2 = &quot;k2&quot;;
    Key&amp;lt;&lt;b&gt;int&lt;/b&gt;, &lt;b&gt;double&lt;/b&gt;&amp;gt; k3 = 3;

    PolyDictionary dictionary = &lt;b&gt;new&lt;/b&gt; PolyDictionary();

    dictionary.Add(k1, &quot;one&quot;);
    dictionary.Add(k2, 2);
    dictionary.Add(k3, 3.33);

    &lt;b&gt;string&lt;/b&gt; v1 = dictionary.Get(k1);
    &lt;b&gt;int&lt;/b&gt; v2 = dictionary.Get(k2);
    &lt;b&gt;double&lt;/b&gt; v3 = dictionary.Get(k3);

    Console.WriteLine(&quot;v1 = {0}&quot;, v1);
    Console.WriteLine(&quot;v2 = {0}&quot;, v2);
    Console.WriteLine(&quot;v3 = {0}&quot;, v3);
&lt;/pre&gt;
&lt;p&gt;The complete example of &lt;code&gt;PolyDictionary&lt;/code&gt; follows,&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;class&lt;/b&gt; PolyDictionary {
        &lt;b&gt;private&lt;/b&gt; Dictionary&amp;lt;&lt;b&gt;object&lt;/b&gt;, &lt;b&gt;object&lt;/b&gt;&amp;gt; _table;

        &lt;b&gt;public&lt;/b&gt; PolyDictionary() {
            _table = &lt;b&gt;new&lt;/b&gt; Dictionary&amp;lt;&lt;b&gt;object&lt;/b&gt;, &lt;b&gt;object&lt;/b&gt;&amp;gt;();
        }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Add&amp;lt;K, V&amp;gt;(Key&amp;lt;K, V&amp;gt; key, V value) {
            _table.Add(key, value);
        }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Add&amp;lt;T&amp;gt;(Key&amp;lt;T&amp;gt; key, T value) {
            _table.Add(key, value);
        }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;bool&lt;/b&gt; Contains&amp;lt;K, V&amp;gt;(Key&amp;lt;K, V&amp;gt; key) {
            &lt;b&gt;return&lt;/b&gt; _table.ContainsKey(key);
        }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;bool&lt;/b&gt; Contains&amp;lt;T&amp;gt;(Key&amp;lt;T&amp;gt; key) {
            &lt;b&gt;return&lt;/b&gt; _table.ContainsKey(key);
        }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Remove&amp;lt;K, V&amp;gt;(Key&amp;lt;K, V&amp;gt; key) {
            _table.Remove(key);
        }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Remove&amp;lt;T&amp;gt;(Key&amp;lt;T&amp;gt; key) {
            _table.Remove(key);
        }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;bool&lt;/b&gt; TryGetValue&amp;lt;K, V&amp;gt;(Key&amp;lt;K, V&amp;gt; key, &lt;b&gt;out&lt;/b&gt; V value) {
            &lt;b&gt;object&lt;/b&gt; objValue;
            &lt;b&gt;if&lt;/b&gt; (_table.TryGetValue(key, &lt;b&gt;out&lt;/b&gt; objValue)) {
                value = (V)objValue;
                &lt;b&gt;return&lt;/b&gt; &lt;b&gt;true&lt;/b&gt;;
            }
            value = &lt;b&gt;default&lt;/b&gt;(V);
            &lt;b&gt;return&lt;/b&gt; &lt;b&gt;false&lt;/b&gt;;
        }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;bool&lt;/b&gt; TryGetValue&amp;lt;T&amp;gt;(Key&amp;lt;T&amp;gt; key, &lt;b&gt;out&lt;/b&gt; T value) {
            &lt;b&gt;object&lt;/b&gt; objValue;
            &lt;b&gt;if&lt;/b&gt; (_table.TryGetValue(key, &lt;b&gt;out&lt;/b&gt; objValue)) {
                value = (T)objValue;
                &lt;b&gt;return&lt;/b&gt; &lt;b&gt;true&lt;/b&gt;;
            }
            value = &lt;b&gt;default&lt;/b&gt;(T);
            &lt;b&gt;return&lt;/b&gt; &lt;b&gt;false&lt;/b&gt;;
        }

        &lt;b&gt;public&lt;/b&gt; V Get&amp;lt;K, V&amp;gt;(Key&amp;lt;K, V&amp;gt; key) {
            &lt;b&gt;return&lt;/b&gt; (V)_table[key];
        }

        &lt;b&gt;public&lt;/b&gt; T Get&amp;lt;T&amp;gt;(Key&amp;lt;T&amp;gt; key) {
            &lt;b&gt;return&lt;/b&gt; (T)_table[key];
        }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Set&amp;lt;K, V&amp;gt;(Key&amp;lt;K, V&amp;gt; key, V value) {
            _table[key] = value;
        }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Set&amp;lt;T&amp;gt;(Key&amp;lt;T&amp;gt; key, T value) {
            _table[key] = value;
        }
    }&lt;/pre&gt;
&lt;p&gt;Note: the implementation changed a bit from the previous example. The &lt;code&gt;
Get()&lt;/code&gt; methods now use the &lt;code&gt;&lt;strong&gt;this&lt;/strong&gt;&lt;/code&gt; property
instead of calling &lt;code&gt;TryGetValue()&lt;/code&gt;. This allows the &lt;code&gt;Get()
&lt;/code&gt;methods to be inlined at the cost of an additional cast or two in the
code.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/2007/03/21/PolyDictionary-II-Constructed-Keys</link>
                <guid>http://removingalldoubt.com/programming/2007/03/21/PolyDictionary-II-Constructed-Keys</guid>
                <pubDate>2007-03-21T15:47:00+00:00</pubDate>
        </item>

        <item>
                <title>PolyDictionary</title>
                <description>&lt;p&gt;CLR&apos;s standard &lt;code&gt;Dictionary&amp;lt;K,T&amp;gt;&lt;/code&gt; allows you to express the 
type of the key and the type value so you don&apos;t have to cast on the way in
and out of a
dictionary. For example you can cache a bunch of strings based on some integer
key. I use this in the following program that takes an integer number and writes
out the English equivalent.&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;class&lt;/b&gt; Program {
        &lt;b&gt;static&lt;/b&gt; Dictionary&amp;lt;&lt;b&gt;int&lt;/b&gt;, &lt;b&gt;string&lt;/b&gt;&amp;gt; names = &lt;b&gt;new&lt;/b&gt; Dictionary&amp;lt;&lt;b&gt;int&lt;/b&gt;, &lt;b&gt;string&lt;/b&gt;&amp;gt;();

        &lt;b&gt;static&lt;/b&gt; Program() {
            names[0] = &quot;zero&quot;;
            names[1] = &quot;one&quot;;
            names[2] = &quot;two&quot;;
            names[3] = &quot;three&quot;;
            names[4] = &quot;four&quot;;
            names[5] = &quot;five&quot;;
            names[6] = &quot;six&quot;;
            names[7] = &quot;seven&quot;;
            names[8] = &quot;eight&quot;;
            names[9] = &quot;nine&quot;;
            names[10] = &quot;ten&quot;;
            names[11] = &quot;eleven&quot;;
            names[12] = &quot;twelve&quot;;
            names[13] = &quot;thirteen&quot;;
            names[14] = &quot;fourteen&quot;;
            names[15] = &quot;fifteen&quot;;
            names[16] = &quot;sixteen&quot;;
            names[17] = &quot;seventeen&quot;;
            names[18] = &quot;eighteen&quot;;
            names[19] = &quot;nineteen&quot;;
            names[20] = &quot;twenty&quot;;
            names[30] = &quot;thirty&quot;;
            names[40] = &quot;forty&quot;;
            names[50] = &quot;fifty&quot;;
            names[60] = &quot;sixty&quot;;
            names[70] = &quot;seventy&quot;;
            names[80] = &quot;eighty&quot;;
            names[90] = &quot;ninety&quot;;
            names[100] = &quot;hundred&quot;;
            names[1000] = &quot;thousand&quot;;
            names[1000000] = &quot;million&quot;;
            names[1000000000] = &quot;billion&quot;;
        }

        &lt;b&gt;static&lt;/b&gt; &lt;b&gt;int&lt;/b&gt; WriteValue(&lt;b&gt;int&lt;/b&gt; value, &lt;b&gt;int&lt;/b&gt; place) {
            &lt;b&gt;if&lt;/b&gt; (value &amp;gt;= place) {
                &lt;b&gt;int&lt;/b&gt; subValue = value / place;
                WriteValue(subValue);
                Console.Write(&quot; {0} &quot;, names[place]);
                value = value % place;
            }
            &lt;b&gt;return&lt;/b&gt; value;
        }

        &lt;b&gt;static&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; WriteValue(&lt;b&gt;int&lt;/b&gt; value) {
            &lt;b&gt;int&lt;/b&gt; originalValue = value;
            &lt;b&gt;if&lt;/b&gt; (value &amp;lt; 0) {
                value = -value;
                Console.WriteLine(&quot;negative &quot;);
            }
            value = WriteValue(value, 1000000000);
            value = WriteValue(value, 1000000);
            value = WriteValue(value, 1000);
            value = WriteValue(value, 100);
            &lt;b&gt;if&lt;/b&gt; (value &amp;gt; 20) {
                &lt;b&gt;int&lt;/b&gt; subValue = (value / 10) * 10;
                Console.Write(names[subValue]);
                value = value % 10;
                &lt;b&gt;if&lt;/b&gt; (value == 0)
                    &lt;b&gt;return&lt;/b&gt;;
                Console.Write(&quot;-&quot;);
            }
            &lt;b&gt;if&lt;/b&gt; (value != 0 || originalValue == 0)
                Console.Write(names[value]);
        }

        &lt;b&gt;static&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Main(&lt;b&gt;string&lt;/b&gt;[] args) {
            &lt;b&gt;while&lt;/b&gt; (&lt;b&gt;true&lt;/b&gt;) {
                Console.Write(&quot;Enter number: &quot;);
                &lt;b&gt;string&lt;/b&gt; line = Console.ReadLine();
                &lt;b&gt;if&lt;/b&gt; (line == &quot;exit&quot; || &lt;b&gt;string&lt;/b&gt;.IsNullOrEmpty(line))
                    &lt;b&gt;break&lt;/b&gt;;
                &lt;b&gt;int&lt;/b&gt; value;
                &lt;b&gt;if&lt;/b&gt; (&lt;b&gt;int&lt;/b&gt;.TryParse(line, &lt;b&gt;out&lt;/b&gt; value)) {
                    WriteValue(value);
                    Console.WriteLine();
                }
                &lt;b&gt;else&lt;/b&gt;
                    Console.WriteLine(&quot;Error: expected number&quot;);
            }
        }
    }&lt;/pre&gt;

&lt;p&gt;This example doesn&apos;t use any casts. The expression &lt;code&gt;names[10]&lt;/code&gt; is of type
&lt;code&gt;&lt;strong&gt;string&lt;/strong&gt;&lt;/code&gt; and the index is of type &lt;code&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/code&gt;.
This is great when the values of the dictionary are all homogeneous as &lt;code&gt;name&lt;/code&gt; is
above. But what if I want to store values of varying types but still not use a
cast. In other words, I want a heterogeneous, type-checked dictionary. If I
don&apos;t mind casts, I can just use a &lt;code&gt;Hashtable&lt;/code&gt; as in,&lt;/p&gt;
&lt;pre&gt;    Hashtable table = &lt;b&gt;new&lt;/b&gt; Hashtable();

    table.Add(&quot;k1&quot;, &quot;one&quot;);
    table.Add(&quot;k2&quot;, 2);
    table.Add(3, 3.33);

    &lt;b&gt;string&lt;/b&gt; v1 = (&lt;b&gt;string&lt;/b&gt;)table[&quot;k1&quot;];
    &lt;b&gt;int&lt;/b&gt; v2 = (&lt;b&gt;int&lt;/b&gt;)table[&quot;k2&quot;];
    &lt;b&gt;double&lt;/b&gt; v3 = (&lt;b&gt;double&lt;/b&gt;)table[3];

    Console.WriteLine(&quot;v1 = {0}&quot;, v1);
    Console.WriteLine(&quot;v2 = {0}&quot;, v2);
    Console.WriteLine(&quot;v3 = {0}&quot;, v3);&lt;/pre&gt;
&lt;p&gt;Unfortunately, if I later change the code to,&lt;/p&gt;
&lt;pre&gt;    table.Add(&quot;k1&quot;, &lt;span style=&quot;background-color:yellow&quot;&gt;1&lt;/span&gt;);
    table.Add(&quot;k2&quot;, 2);
    table.Add(3, 3.33);

    &lt;b&gt;string&lt;/b&gt; v1 = (&lt;b&gt;string&lt;/b&gt;)table[&quot;k1&quot;];
    &lt;b&gt;int&lt;/b&gt; v2 = (&lt;b&gt;int&lt;/b&gt;)table[&quot;k2&quot;];
    &lt;b&gt;double&lt;/b&gt; v3 = (&lt;b&gt;double&lt;/b&gt;)table[3];&lt;/pre&gt;
&lt;p&gt;The compiler will dutifully compile the code and I will get a runtime error
at the first use of the dictionary when it tries to cast an integer to a string.
I would like the compiler to catch this type of error instead of having to wait
until my user does. &lt;/p&gt;
&lt;p&gt;I have keys of varying type and values of varying type. How can I get
this and type verification as well? In other words, what I want is something
like,&lt;/p&gt;
&lt;pre&gt;    dictionary.Add(k1, &quot;one&quot;);
    dictionary.Add(k2, 2);
    dictionary.Add(k3, 3.33);

    &lt;b&gt;string&lt;/b&gt; v1 = dictionary[k1];
    &lt;b&gt;int&lt;/b&gt; v2 = dictionary[k2];
    &lt;b&gt;double&lt;/b&gt; v3 = dictionary[k3];&lt;/pre&gt;
&lt;p&gt;What I want to do is be able to infer the returns result of the expression
&lt;code&gt;dictionary[k1]&lt;/code&gt; from the key. In the above example, there should be
something about the &lt;code&gt;k1&lt;/code&gt; key that tells the compiler that the result will be a
&lt;code&gt;string&lt;/code&gt; and when I change it later to an &lt;code&gt;int&lt;/code&gt;, I want the
compiler to catch the assignment to &lt;code&gt;string&lt;/code&gt; as a type error.&lt;/p&gt;
&lt;p&gt;To do this I need a method that can infer the type of the return result from
a parameter. A simple example of such a method is,&lt;/p&gt;
&lt;pre&gt;    T NoOp(T value) { &lt;strong&gt;return&lt;/strong&gt; value; }&lt;/pre&gt;
&lt;p&gt;which returns the same value of the same type as it retrieves for any value
of any type. The return type is inferred from the parameter &lt;code&gt;value&lt;/code&gt;&apos;s type.&lt;/p&gt;
&lt;p&gt;This is not good enough yet. We don&apos;t want the key type to be the same as the value type;
we want the value type to be arbitrary. To do this we need to
encode the value&apos;s type in the key. One way is to have the
key&apos;s type include the value&apos;s type as part of the key&apos;s type definition. This can be done
through supplying a dummy type parameter, a type parameter that is not needed in the definition of
the type but the dummy type parameter is carried around by the type and can be
used by type inferencing. To do this we need
a method something like,&lt;/p&gt;
&lt;pre&gt;    T Get&amp;lt;T&amp;gt;(Key&amp;lt;T&amp;gt; key) { ... }&lt;/pre&gt;
&lt;p&gt;which takes a key and extracts the return type from the keys definition. This
requires a &lt;code&gt;Key&lt;/code&gt; class definition that has one type parameter which is simply,&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;class&lt;/b&gt; Key&amp;lt;T&amp;gt; { &lt;b&gt;public&lt;/b&gt; Key() { } }
&lt;/pre&gt;
&lt;p&gt;Note we don&apos;t use &lt;code&gt;T&lt;/code&gt; in &lt;code&gt;Key&lt;/code&gt;, it is just there so type inferencing will find it. We can now
define some keys,&lt;/p&gt;
&lt;pre&gt;    Key&amp;lt;&lt;b&gt;string&lt;/b&gt;&amp;gt; k1 = &lt;b&gt;new&lt;/b&gt; Key&amp;lt;&lt;b&gt;string&lt;/b&gt;&amp;gt;();
    Key&amp;lt;&lt;b&gt;int&lt;/b&gt;&amp;gt; k2 = &lt;b&gt;new&lt;/b&gt; Key&amp;lt;&lt;b&gt;int&lt;/b&gt;&amp;gt;();
    Key&amp;lt;&lt;b&gt;double&lt;/b&gt;&amp;gt; k3 = &lt;b&gt;new&lt;/b&gt; Key&amp;lt;&lt;b&gt;double&lt;/b&gt;&amp;gt;();
&lt;/pre&gt;
&lt;p&gt;The keys are just arbitrary instances. Type
inferencing will now be able to extract &lt;code&gt;T&lt;/code&gt; from the key&apos;s type and use it as the
return result! This means that &lt;code&gt;Get(k1)&lt;/code&gt; will be&amp;nbsp; of type &lt;code&gt;string&lt;/code&gt; and
&lt;code&gt;Get(k2)&lt;/code&gt; will be of type
&lt;code&gt;int&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Type inferencing also works the same way for other parameters of the method.
For the &lt;code&gt;Add()&lt;/code&gt; method we can define it as,&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Add&amp;lt;T&amp;gt;(Key&amp;lt;T&amp;gt; key, T value);&lt;/pre&gt;
&lt;p&gt;With this&amp;nbsp; &lt;code&gt;Add()&lt;/code&gt; the code above will compile because the type of &lt;code&gt;value&lt;/code&gt; is also
inferred from the key!&lt;/p&gt;
&lt;p&gt;Unfortunately, the &lt;code&gt;this&lt;/code&gt; property does not accept type parameters
so we cannot get the code, &lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;string&lt;/b&gt; v1 = dictionary[k1];
    &lt;b&gt;int&lt;/b&gt; v2 = dictionary[k2];
    &lt;b&gt;double&lt;/b&gt; v3 = dictionary[k3];&lt;/pre&gt;

&lt;p&gt;to compile; but if we replace the array indexers with a &lt;code&gt;Get()&lt;/code&gt;
method we can get close, as in,&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;string&lt;/b&gt; v1 = dictionary.Get(k1);
    &lt;b&gt;int&lt;/b&gt; v2 = dictionary.Get(k2);
    &lt;b&gt;double&lt;/b&gt; v3 = dictionary.Get(k3);&lt;/pre&gt;

&lt;p&gt;This works if dictionary has a &lt;code&gt;Get()&lt;/code&gt; method like we
described above,&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;public&lt;/b&gt; T Get&amp;lt;T&amp;gt;(Key&amp;lt;T&amp;gt; key);&lt;/pre&gt;
&lt;p&gt;There we have it! With the &lt;code&gt;Add()&lt;/code&gt; and &lt;code&gt;Get()&lt;/code&gt; methods we have the beginnings of
a dictionary that can hold any value of any type and retrieve the value back
without requiring our use of the dictionary to use a cast. A complete example is given below,&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;class&lt;/b&gt; Key&amp;lt;T&amp;gt; { &lt;b&gt;public&lt;/b&gt; Key() { } }

    &lt;b&gt;class&lt;/b&gt; PolyDictionary {
        &lt;b&gt;private&lt;/b&gt; Dictionary&amp;lt;&lt;b&gt;object&lt;/b&gt;, &lt;b&gt;object&lt;/b&gt;&amp;gt; _table;

        &lt;b&gt;public&lt;/b&gt; PolyDictionary() {
            _table = &lt;b&gt;new&lt;/b&gt; Dictionary&amp;lt;&lt;b&gt;object&lt;/b&gt;, &lt;b&gt;object&lt;/b&gt;&amp;gt;();
        }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Add&amp;lt;T&amp;gt;(Key&amp;lt;T&amp;gt; key, T value) {
            _table.Add(key, value);
        }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;bool&lt;/b&gt; Contains&amp;lt;T&amp;gt;(Key&amp;lt;T&amp;gt; key) {
            &lt;b&gt;return&lt;/b&gt; _table.ContainsKey(key);
        }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Remove&amp;lt;T&amp;gt;(Key&amp;lt;T&amp;gt; key) {
            _table.Remove(key);
        }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;bool&lt;/b&gt; TryGetValue&amp;lt;T&amp;gt;(Key&amp;lt;T&amp;gt; key, &lt;b&gt;out&lt;/b&gt; T value) {
            &lt;b&gt;object&lt;/b&gt; objValue;
            &lt;b&gt;if&lt;/b&gt; (_table.TryGetValue(key, &lt;b&gt;out&lt;/b&gt; objValue)) {
                value = (T)objValue;
                &lt;b&gt;return&lt;/b&gt; &lt;b&gt;true&lt;/b&gt;;
            }
            value = &lt;b&gt;default&lt;/b&gt;(T);
            &lt;b&gt;return&lt;/b&gt; &lt;b&gt;false&lt;/b&gt;;
        }

        &lt;b&gt;public&lt;/b&gt; T Get&amp;lt;T&amp;gt;(Key&amp;lt;T&amp;gt; key) {
            T value;
            &lt;b&gt;if&lt;/b&gt; (!TryGetValue(key, &lt;b&gt;out&lt;/b&gt; value))
                &lt;b&gt;throw&lt;/b&gt; &lt;b&gt;new&lt;/b&gt; KeyNotFoundException();
            &lt;b&gt;return&lt;/b&gt; value;
        }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Set&amp;lt;T&amp;gt;(Key&amp;lt;T&amp;gt; key, T value) {
            _table[key] = value;
        }
    }&lt;/pre&gt;
</description>
                <link>http://removingalldoubt.com/programming/2007/03/17/PolyDictionary</link>
                <guid>http://removingalldoubt.com/programming/2007/03/17/PolyDictionary</guid>
                <pubDate>2007-03-17T13:30:00+00:00</pubDate>
        </item>

        <item>
                <title>WPF Forum Best Practices</title>
                <description>&lt;p&gt;Rob&apos;s post
&lt;a href=&quot;http://rrelyea.spaces.live.com/Blog/cns!167AD7A5AB58D5FE!1676.entry&quot;&gt;Best Forum Practices&lt;/a&gt;
recommends some guidelines for the WPF forum. My favorite is,&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;u&gt;Recommended Steps for Answering [Questions]:&lt;/u&gt;&lt;/strong&gt; &lt;/p&gt;
&lt;p&gt;1) Answer the question&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;These are good guidelines for any question/answer style forum, not just
WPF&apos;s.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/2007/02/27/WPF-Forum-Best-Practices</link>
                <guid>http://removingalldoubt.com/programming/2007/02/27/WPF-Forum-Best-Practices</guid>
                <pubDate>2007-02-27T09:45:00+00:00</pubDate>
        </item>

        <item>
                <title>Follow-up to Advice</title>
                <description>&lt;p&gt;I have to be honest; I didn’t expect the response I
received from my
&lt;a href=&quot;http://www.removingalldoubt.com/commentview.aspx/a32977e2-cb7d-42ea-9d25-5e539423affd&quot;&gt;
Fatherly advice to new programmers&lt;/a&gt; article. There have been some very nice
things written about it, not only in the comments but in other blogs. I don’t
consider myself a very good writer and having my writing style praised was
highly unexpected. I thank everyone for
their kind words. Also, if you haven’t read the comments posted to the article
yet, I highly recommend it. They offer interesting insights from a variety of
perspectives.&lt;/p&gt;
&lt;p class=&quot;edit&quot;&gt;[EDIT 01/21/2015: Unfortunately, all the comments were lost
in the blogging software shuffle ]&lt;/p&gt;
&lt;p&gt;I also recommend reading some of the blogs entries that
have been written in response to it. I would like to point out one in particular
from Jeff Atwood:
&lt;a href=&quot;http://www.codinghorror.com/blog/archives/000773.html&quot;&gt;Shipping Isn&apos;t
Enough&lt;/a&gt;. I agree with him. You not only need to focus on shipping but make
sure what you are working on is worth shipping. You need to focus on shipping
programs people use and enjoy using.&lt;/p&gt;
&lt;p&gt;Other bloggers have pointed out that you often don’t that
much control over what you are working on, such as
&lt;a href=&quot;http://mikeomatic.net/&quot;&gt;Mike-O-Matic&lt;/a&gt;’s
&lt;a href=&quot;http://mikeomatic.net/?p=137&quot;&gt;Shipping Is Enough, Sometimes&lt;/a&gt;. And
other point out that sometimes you don’t know if what you are working on will be
used or not, as in Avonelle’s &lt;a href=&quot;http://www.coolbits.nu/1130.aspx&quot;&gt;If a
program falls in a forest...&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;These entries remind me of one more bit of advice: you are
not your users. Just because you like something some particular way doesn’t mean
that your users will, nor just because you would never use something doesn’t mean
nobody needs it. You are not your users; you should get to know them.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/2007/02/19/Follow-up-to-Advice</link>
                <guid>http://removingalldoubt.com/programming/2007/02/19/Follow-up-to-Advice</guid>
                <pubDate>2007-02-19T15:24:00+00:00</pubDate>
        </item>

        <item>
                <title>The Expression Problem: The JRuby Interpreter</title>
                <description>&lt;p&gt;I previously discussed the expression problem could be solved with various patterns 
including the
&lt;a href=&quot;http://www.removingalldoubt.com/PermaLink.aspx/861feae8-2404-4044-b661-aa13d432b08d&quot;&gt;
visitor pattern&lt;/a&gt; and using
&lt;a href=&quot;http://www.removingalldoubt.com/PermaLink.aspx/e6fb6ded-a66f-4707-97e7-0edaa04b30d9&quot;&gt;
a procedural approach&lt;/a&gt;. It is interesting to note that JRuby used to use a
visitor pattern but switched to a procedural approach to improve performance.
You can read about it
&lt;a href=&quot;http://www.artima.com/forums/flat.jsp?forum=281&amp;amp;thread=180325&quot;&gt;here&lt;/a&gt;.
This is a side note in the article since what they want to do is convert Ruby to
run as byte code similar to how
&lt;a href=&quot;http://www.python.org/workshops/1997-10/proceedings/hugunin.html&quot;&gt;
JPython&lt;/a&gt; works.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/2006/10/26/The-Expression-Problem-The-JRuby-Interpreter</link>
                <guid>http://removingalldoubt.com/programming/2006/10/26/The-Expression-Problem-The-JRuby-Interpreter</guid>
                <pubDate>2006-10-26T14:23:00+00:00</pubDate>
        </item>

        <item>
                <title>Linked List VI: Recursion - see recursion</title>
                <description>&lt;p&gt;I finally have a definitive answer to the question of why &lt;span class=&quot;code&quot;&gt;MyNode&lt;/span&gt; is needed.
Consider the following, simplified, version of the &lt;span class=&quot;code&quot;&gt;LinkedList&lt;/span&gt; classes I posed,&lt;/p&gt;
&lt;pre&gt;  &lt;b&gt;class&lt;/b&gt; C&amp;lt;X&amp;gt; { &lt;b&gt;class&lt;/b&gt; N { } }
  &lt;b&gt;class&lt;/b&gt; D: C&amp;lt;D.N&amp;gt; { }&lt;/pre&gt;
&lt;p&gt;This is really an obfuscated form of&lt;/p&gt;
&lt;pre&gt;  &lt;b&gt;class&lt;/b&gt; N&amp;lt;X&amp;gt; { }
  &lt;b&gt;class&lt;/b&gt; C&amp;lt;X&amp;gt; { }
  &lt;b&gt;class&lt;/b&gt; D: C&amp;lt;N&amp;lt;?&amp;gt;&amp;gt; { }&lt;/pre&gt;
&lt;p&gt;where ? needs to be replaced by an infinite list if &lt;span class=&quot;code&quot;&gt;N&amp;lt;N&amp;lt;...&amp;gt;&amp;gt;&lt;/span&gt;. In other
words, I am trying to define a type directly in terms of itself, a
&lt;span class=&quot;code&quot;&gt;T&lt;/span&gt; where &lt;span class=&quot;code&quot;&gt;T=N&amp;lt;T&amp;gt;&lt;/span&gt;. This is not
allowed. An additional type is required to break the cycle, such as
&lt;span class=&quot;code&quot;&gt;M&lt;/span&gt; in,&lt;/p&gt;

&lt;pre&gt;  &lt;b&gt;class&lt;/b&gt; M: N&amp;lt;M&amp;gt; { }&lt;/pre&gt;

&lt;p&gt;Then I can write,&lt;/p&gt;

&lt;pre&gt;  &lt;b&gt;class&lt;/b&gt; D: C&amp;lt;M&amp;gt; { }&lt;/pre&gt;

&lt;p&gt;So, there we have it, &lt;span class=&quot;code&quot;&gt;MyNode&lt;/span&gt; is necessary to avoid defining
a type directly in terms
of itself. The unobfuscated version makes this much more clear.
&lt;span class=&quot;code&quot;&gt;MyNode&lt;/span&gt; serves the same role as &lt;span class=&quot;code&quot;&gt;M&lt;/span&gt;
does above.&lt;/p&gt;
&lt;p&gt;Thanks again to &lt;a href=&quot;http://research.microsoft.com/~akenn/&quot;&gt;Andrew&lt;/a&gt;
for taking the time to beat this into my thick skull.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/2006/09/25/Linked-List-VI-Recursion-see-recursion</link>
                <guid>http://removingalldoubt.com/programming/2006/09/25/Linked-List-VI-Recursion-see-recursion</guid>
                <pubDate>2006-09-25T10:05:00+00:00</pubDate>
        </item>

        <item>
                <title>Fatherly Advice To New Programmers</title>
                <description>&lt;p&gt;It looks like none of my children will become programmers. Instead of letting
my fatherly advice to my new programmer son or daughter go to waste, I am going
to inflict it on you. If you are newly embarking on the journey that is becoming
a programmer, here is advice your father would tell you if he were a programmer.
These are things I had to learn the hard way. &lt;/p&gt;
&lt;p&gt;&lt;b&gt;Keep Learning: &lt;/b&gt;Read. Go to conferences. Subscribe to journals. Take
classes. Whatever it takes for you to keep learning, make it a priority. Learn
about every language you can find. Take time to learn about any new frameworks,
algorithms, techniques, models, paradigms, you can. Each gives you one more
tool in your tool chest. Each will help you more easily tackle your next
programming problem. Find a mentor, someone much better than you, and learn all
they can teach you. Never stop learning.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Learn To Communicate&lt;/b&gt;: I often joke that the most important skill you
can learn as a programmer is how to draw a rectangle on a white-board.
Communication is critical to the job of a programmer. Communicating with
customers, clients, users, co-workers, bosses, vice presidents, CEO&apos;s,
board-members, VC capitalists, all will become important at some point in your
career. Learn how to speak in public. Learn how to write in English. Learn to
effectively communicate in person. Learn how to persuade without shouting,
getting angry, or getting flustered. Learn how to speak without jargon. Help
people understand what you are doing. Learn to break things into simple,
understandable pieces. Learn to communicate by analogy and symbolism. Learn to
communicate.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Be Predictable:&lt;/b&gt; Learn how fast you can comfortably program. Wait to
predict how long it will take you to complete a task until you understand it.
Allow for the unexpected. Plan for vacations and time-off. Live with your
predictions. I don&apos;t believe I know a problem well enough to predict how long it
will take to complete until I can break that task down into sub-tasks that each
take no longer than 3 days (often less than one day). Live by this rule,
under-promise, over-deliver. It is better to deliver in 10 days what you
promised in 15 than to deliver in 10 days what you promised in 5. People depend,
schedule, and plan around your predictions. Make them the best you can and make
sure you can comfortably do them or you will be asked to live up to your
uncomfortable predictions. You will not be good at it at first; to compensate,
verify your predictions with someone more experienced. Learn to get better. Be
predictable; other depend on you.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Own Up To Your Mistakes:&lt;/b&gt; You will make mistakes. How you handle your
mistakes is how you will be judged. Learn how to say &quot;I was wrong.&quot; If you
underestimated how long it will take you to do something, tell people as soon as
it is clear to you. If you broke the build, fix it. If you created a bug, fix
it. Don&apos;t deny the mistake, don&apos;t make excuses for the mistake, don&apos;t figure out
how to hide the mistake, don&apos;t blame others for the mistake, do something about
it. Take ownership of your mistake or you &lt;i&gt;will&lt;/i&gt; repeat it.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Never Let Bad Code Off Your Desk:&lt;/b&gt; Your job as a programmer is to write
code that works, never let code off your desk you are not sure meets that
criteria. Not only does it reflect badly on you, it is much more expensive, and
much harder, to find a problem once it leaves your desk than before. Learn to
love unit tests. Learn to love code coverage. Learn to test your code better
than people who are paid to test it. Be embarrassed about bugs that are found
after you have checked-in. Be especially embarrassed when a customer finds the
bug. Don&apos;t rely on others to find your bugs for you, find them and fix them
yourself. Don&apos;t hope it will work. Test it. Don&apos;t assume it will work. Test it.
Don&apos;t whatever. Just test it. If you haven&apos;t tested it, it doesn&apos;t work; of this
you &lt;i&gt;can&lt;/i&gt; be sure. But, even if you are diligent with testing, bugs will
get by you. You will make mistakes but try your best not to.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Programming is Fun But Shipping is Your Job:&lt;/b&gt; Programming is fun. It is
the joy of discovery. It is the joy of creation. It is the joy of accomplishment. It
is the joy of learning. It is fun to see your handiwork displaying on the
screen. It is fun to have your co-workers marvel at your code. It is fun to have
people use your work. It is fun have your product lauded in public, used by
neighbors, and discussed in the press. Programming should be fun and if it
isn&apos;t, figure out what is making it not fun and fix it. However, shipping isn&apos;t
fun. I often have said that shipping a product feels good, like when someone
stops hitting you. Your job is completing the product, fixing the bugs, and
shipping. If bugs need fixing, fix them. If documentation needs writing, write
it. If code needs testing, test it. All of this is part of shipping. You don&apos;t
get paid to program, you get paid to ship. Be good at your job.&lt;/p&gt;
&lt;p&gt;Remember these simple statements,&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Never stop learning.&lt;/li&gt;
	&lt;li&gt;Communication is critical.&lt;/li&gt;
	&lt;li&gt;Under promise, over deliver.&lt;/li&gt;
	&lt;li&gt;&quot;I was wrong.&quot;&lt;/li&gt;
	&lt;li&gt;If it is not tested it doesn&apos;t work.&lt;/li&gt;
	&lt;li&gt;Programming isn&apos;t your job, shipping is.&lt;/li&gt;
&lt;/ul&gt;
</description>
                <link>http://removingalldoubt.com/programming/2006/09/19/Fatherly-Advice-To-New-Programmers</link>
                <guid>http://removingalldoubt.com/programming/2006/09/19/Fatherly-Advice-To-New-Programmers</guid>
                <pubDate>2006-09-19T05:21:00+00:00</pubDate>
        </item>

        <item>
                <title>Linked List V: Revenge of the Type Parameter</title>
                <description>&lt;p&gt;I honestly thought I was done with this topic but as it turns out I missed 
something. I stated firmly that &quot;currently, in C#, we are stuck with the cast&quot;
but I was wrong. One of the great things about working at Microsoft is the people you can interact with. In
back burner researching a definitive answer to Hallvard&apos;s
question to my post of why linked list &lt;span class=&quot;code&quot;&gt;LinkedList&amp;lt;T&amp;gt;&lt;/span&gt; cannot be written,&lt;/p&gt;
&lt;pre&gt;  &lt;b&gt;class&lt;/b&gt; LinkedList&amp;lt;T&amp;gt;: LinkedList&amp;lt;T, LinkedList&amp;lt;T&amp;gt;.Node&amp;gt; { }&lt;/pre&gt;
&lt;p&gt;I asked &lt;a href=&quot;http://research.microsoft.com/~akenn/&quot;&gt;Andrew Kennedy&lt;/a&gt; to look at my page.
I like talking to Andrew; I learn at least one new thing every time we talk.
This was certainly no exception. He quickly pointed out something
that was staring me squarely in the face but I missed entirely, folding my implementation of
&lt;span class=&quot;code&quot;&gt;DoubleLinkedList&amp;lt;T&amp;gt;&lt;/span&gt; with &lt;span class=&quot;code&quot;&gt;DoubleLinkedList&amp;lt;T, N&amp;gt;&lt;/span&gt;
and closing the node type directly
will remove the cast. In other words, &lt;span class=&quot;code&quot;&gt;DoubleLinkedList&amp;lt;T&amp;gt;&lt;/span&gt; can be written without
casts as,&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;class&lt;/b&gt; DoubleLinkedList&amp;lt;T&amp;gt;: LinkedList&amp;lt;T, DoubleLinkedList&amp;lt;T&amp;gt;.DoubleNode&amp;gt;,
        IDoubleLinkedList&amp;lt;T&amp;gt; {

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; DoubleNode: Node {
            DoubleNode _previous;

            &lt;b&gt;public&lt;/b&gt; &lt;b&gt;override&lt;/b&gt; DoubleNode Next {
                &lt;b&gt;get&lt;/b&gt; {
                    &lt;b&gt;return&lt;/b&gt; &lt;b&gt;base&lt;/b&gt;.Next;
                }
                &lt;b&gt;set&lt;/b&gt; {
                    &lt;b&gt;base&lt;/b&gt;.Next = value;
                    value._previous = &lt;b&gt;this&lt;/b&gt;;
                }
            }

            &lt;b&gt;public&lt;/b&gt; DoubleNode Previous { &lt;b&gt;get&lt;/b&gt; { &lt;b&gt;return&lt;/b&gt; _previous; } }
        }

        &lt;b&gt;public&lt;/b&gt; IEnumerable&amp;lt;T&amp;gt; Backwards {
            get {
                &lt;b&gt;if&lt;/b&gt; (Last != &lt;b&gt;null&lt;/b&gt;) {
                    DoubleNode current = Last;
                    &lt;b&gt;do&lt;/b&gt; {
                        &lt;b&gt;yield&lt;/b&gt; &lt;b&gt;return&lt;/b&gt; current.Data;
                        current = current.Previous;
                    } &lt;b&gt;while&lt;/b&gt; (current != Last);
                }
            }
        }
    }&lt;/pre&gt;

&lt;p&gt;which closes the node parameter instead of leaving it open. This is one of
those head-slapping, &quot;how stupid of me&quot; moments like when someone points out
that &quot;you know, if you would have pushed the other pawn instead, you would have
had checkmate in three moves.&quot; You can see that this is a straight forward
application of the requires clause version (declaring it to be
&lt;span class=&quot;code&quot;&gt;DoubleNode&lt;/span&gt; instead of requiring it). In my zest to introduce the concepts of the &quot;this type&quot; from LOOM and the
requires clause from Scala, I missed what should have been obvious.
Silly me. &lt;/p&gt;
&lt;p&gt;This solution has the limitation that you cannot derive something like
&lt;span class=&quot;code&quot;&gt;TripleLinkedList&amp;lt;T&amp;gt;&lt;/span&gt; from &lt;span class=&quot;code&quot;&gt;
DoubleLinkedList&amp;lt;T&amp;gt;&lt;/span&gt; so my example still holds some water (&lt;span class=&quot;code&quot;&gt;TripleLinkedList&amp;lt;T,
N&amp;gt;&lt;/span&gt; can be derived from &lt;span class=&quot;code&quot;&gt;DoubleLinkedList&amp;lt;T, N&amp;gt;&lt;/span&gt;)
but it is not nearly so compelling. I still think that one of the two features
would be very useful but I need a much better motivating example.&lt;/p&gt;

&lt;p&gt;While I still don&apos;t have a definitive answer to Hallvard&apos;s question, I picked
up this gem along the way. Not bad so far.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/2006/09/08/Linked-List-V-Revenge-of-the-Type-Parameter</link>
                <guid>http://removingalldoubt.com/programming/2006/09/08/Linked-List-V-Revenge-of-the-Type-Parameter</guid>
                <pubDate>2006-09-08T12:37:00+00:00</pubDate>
        </item>

        <item>
                <title>Linked List IV: Removing casts</title>
                <description>&lt;p&gt;In the last entry we successfully derived a double-linked list implementation 
from a single-linked list implementation, but we had to use casts to get it to
compile. What we want to do now is see if we can remove the casts.&lt;/p&gt;
&lt;p&gt;We will first remove the cast in the &lt;span class=&quot;code&quot;&gt;Backwards&lt;/span&gt;
method. &lt;/p&gt;
&lt;pre&gt;        DoubleNode current = (DoubleNode)Last;&lt;/pre&gt;
&lt;p&gt;The reason we need the cast is because the base class defines
&lt;span class=&quot;code&quot;&gt;Last&lt;/span&gt; to be&lt;/p&gt;
&lt;pre&gt;        &lt;b&gt;protected&lt;/b&gt; Node Last { get { &lt;b&gt;return&lt;/b&gt; _last; } }&lt;/pre&gt;

&lt;p&gt;What we need is the type to be &lt;span class=&quot;code&quot;&gt;DoubleNode&lt;/span&gt;, but this
property is defined in a descendent class. To allow us to change the type we
need to leave the type open, or, in other words,&amp;nbsp; add the node type as a
type parameter. To accomplish this, I replaced all references
to the node type with a type parameter. The new &lt;span class=&quot;code&quot;&gt;LinkedList&lt;/span&gt;
class looks
like,&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;class&lt;/b&gt; LinkedList&amp;lt;T, NodeType&amp;gt;: IEnumerable&amp;lt;T&amp;gt;, ILinkedList&amp;lt;T&amp;gt;
        &lt;b&gt;where&lt;/b&gt; NodeType: LinkedList&amp;lt;T, NodeType&amp;gt;.Node, &lt;b&gt;new&lt;/b&gt;() {

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; Node {
            &lt;b&gt;private&lt;/b&gt; T _data;
            &lt;b&gt;private&lt;/b&gt; NodeType _next;

            &lt;b&gt;public&lt;/b&gt; T Data {
                &lt;b&gt;get&lt;/b&gt; { &lt;b&gt;return&lt;/b&gt; _data; }
                &lt;b&gt;set&lt;/b&gt; { _data = value; }
            }
            &lt;b&gt;public&lt;/b&gt; &lt;b&gt;virtual&lt;/b&gt; NodeType Next {
                &lt;b&gt;get&lt;/b&gt; { &lt;b&gt;return&lt;/b&gt; _next; }
                &lt;b&gt;set&lt;/b&gt; { _next = value; }
            }
        }

        NodeType _last;

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Add(T value) {
            NodeType newNode = &lt;b&gt;new&lt;/b&gt; NodeType();
            newNode.Data = value;
            &lt;b&gt;if&lt;/b&gt; (_last == &lt;b&gt;null&lt;/b&gt;) {
                _last = newNode;
                newNode.Next = newNode;
            }
            &lt;b&gt;else&lt;/b&gt; {
                newNode.Next = _last.Next;
                _last.Next = newNode;
                _last = newNode;
            }
        }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Clear() {
            _last = &lt;b&gt;null&lt;/b&gt;;
        }

        &lt;b&gt;public&lt;/b&gt; IEnumerator&amp;lt;T&amp;gt; GetEnumerator() {
            &lt;b&gt;if&lt;/b&gt; (_last != &lt;b&gt;null&lt;/b&gt;) {
                NodeType current = _last;
                &lt;b&gt;do&lt;/b&gt; {
                    current = current.Next;
                    &lt;b&gt;yield&lt;/b&gt; &lt;b&gt;return&lt;/b&gt; current.Data;
                } &lt;b&gt;while&lt;/b&gt; (current != _last);
            }
        }

        IEnumerator IEnumerable.GetEnumerator() {
            &lt;b&gt;return&lt;/b&gt; GetEnumerator();
        }

        &lt;b&gt;protected&lt;/b&gt; NodeType Last {
            &lt;b&gt;get&lt;/b&gt; { &lt;b&gt;return&lt;/b&gt; _last; }
        }
    }&lt;/pre&gt;
&lt;p&gt;The &lt;b&gt;&lt;span class=&quot;code&quot;&gt;where&lt;/span&gt;&lt;/b&gt; clause looks complicated but what
it says is that &lt;span class=&quot;code&quot;&gt;NodeType&lt;/span&gt;&apos;s actual parameter must
derive from the &lt;span class=&quot;code&quot;&gt;Node&lt;/span&gt; type defined in the class. This
allows me to use the properties defined for the node class on variables of type
&lt;span class=&quot;code&quot;&gt;NodeType&lt;/span&gt;. The &lt;span class=&quot;code&quot;&gt;&lt;b&gt;new&lt;/b&gt;()&lt;/span&gt; part says that the
actual parameter must also
have a default constructor. This allows us to remove the &lt;span class=&quot;code&quot;&gt;
CreateNode()&lt;/span&gt; method.&lt;/p&gt;
&lt;p&gt;Unfortunately, this definition is a bit difficult to use
because you need to define a node class to pass as a parameter. To make this
easier, I defined a single parameter version,&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;class&lt;/b&gt; LinkedList&amp;lt;T&amp;gt;: LinkedList&amp;lt;T, LinkedList&amp;lt;T&amp;gt;.MyNode&amp;gt; {
        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; MyNode: Node { }
    }&lt;/pre&gt;
&lt;p&gt;I implemented the double-linked list as,&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;class&lt;/b&gt; DoubleLinkedList&amp;lt;T, NodeType&amp;gt;: LinkedList&amp;lt;T, NodeType&amp;gt;, IDoubleLinkedList&amp;lt;T&amp;gt;
        where NodeType: DoubleLinkedList&amp;lt;T, NodeType&amp;gt;.DoubleNode, &lt;b&gt;new&lt;/b&gt;() {

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; DoubleNode: Node {
            NodeType _previous;

            &lt;b&gt;public&lt;/b&gt; &lt;b&gt;override&lt;/b&gt; NodeType Next {
                &lt;b&gt;get&lt;/b&gt; {
                    &lt;b&gt;return&lt;/b&gt; &lt;b&gt;base&lt;/b&gt;.Next;
                }
                &lt;b&gt;set&lt;/b&gt; {
                    &lt;b&gt;base&lt;/b&gt;.Next = value;
                    value._previous = (NodeType)&lt;b&gt;this&lt;/b&gt;;
                }
            }

            &lt;b&gt;public&lt;/b&gt; NodeType Previous { &lt;b&gt;get&lt;/b&gt; { &lt;b&gt;return&lt;/b&gt; _previous; } }
        }

        &lt;b&gt;public&lt;/b&gt; IEnumerable&amp;lt;T&amp;gt; Backwards {
            get {
                &lt;b&gt;if&lt;/b&gt; (Last != &lt;b&gt;null&lt;/b&gt;) {
                    NodeType current = Last;
                    &lt;b&gt;do&lt;/b&gt; {
                        &lt;b&gt;yield&lt;/b&gt; &lt;b&gt;return&lt;/b&gt; current.Data;
                        current = current.Previous;
                    } &lt;b&gt;while&lt;/b&gt; (current != Last);
                }
            }
        }
    }&lt;/pre&gt;
&lt;p&gt;This looks almost identical to the previous &lt;span class=&quot;code&quot;&gt;
DoubleLinkedList&lt;/span&gt; that I posted last time but it no longer needs a cast in
&lt;span class=&quot;code&quot;&gt;Backwards&lt;/span&gt;. This is achieved by constraining types
passed as &lt;span class=&quot;code&quot;&gt;NodeType&lt;/span&gt; to descend from &lt;span class=&quot;code&quot;&gt;
DoubleNode&lt;/span&gt; instead of just &lt;span class=&quot;code&quot;&gt;Node&lt;/span&gt;. We can pass
&lt;span class=&quot;code&quot;&gt;NodeType&lt;/span&gt; to &lt;span class=&quot;code&quot;&gt;LinkedList&amp;lt;T, NodeType&amp;gt;&lt;/span&gt;
because the constrains on &lt;span class=&quot;code&quot;&gt;NodeType&lt;/span&gt; are compatible with
the constraints for &lt;span class=&quot;code&quot;&gt;NodeType&lt;/span&gt; in &lt;span class=&quot;code&quot;&gt;
LinkedList&amp;lt;T, NodeType&amp;gt;&lt;/span&gt;. I require that &lt;span class=&quot;code&quot;&gt;NodeType&lt;/span&gt;
derive from &lt;span class=&quot;code&quot;&gt;DoubleNode&lt;/span&gt; which, itself, derives from
&lt;span class=&quot;code&quot;&gt;Node&lt;/span&gt;. &lt;span class=&quot;code&quot;&gt;LinkedList&amp;lt;T, NodeType&amp;gt;&lt;/span&gt;
requires &lt;span class=&quot;code&quot;&gt;NodeType&lt;/span&gt; to derive from &lt;span class=&quot;code&quot;&gt;Node&lt;/span&gt;; requiring it
derive from &lt;span class=&quot;code&quot;&gt;DoubleNode&lt;/span&gt; ensures that is the case. I
will also make this easier to use, like &lt;span class=&quot;code&quot;&gt;LinkedList&amp;lt;T, NodeType&amp;gt;&lt;/span&gt;
above, by creating &lt;span class=&quot;code&quot;&gt;DoubleLink&amp;lt;T&amp;gt;&lt;/span&gt; from
&lt;span class=&quot;code&quot;&gt;DoubleLink&amp;lt;T, NodeType&amp;gt;&lt;/span&gt;, as in,&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;class&lt;/b&gt; DoubleLinkedList&amp;lt;T&amp;gt;: DoubleLinkedList&amp;lt;T, DoubleLinkedList&amp;lt;T&amp;gt;.MyNode&amp;gt; {
        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; MyNode: DoubleNode { }
    }&lt;/pre&gt;
&lt;p&gt;But, we still need a cast in the &lt;span class=&quot;code&quot;&gt;Next&lt;/span&gt; property.
The reason we need it is, even though we know that &lt;span class=&quot;code&quot;&gt;NodeType&lt;/span&gt;
derives from &lt;span class=&quot;code&quot;&gt;DoubleNode&lt;/span&gt;, the compiler only is sure
that the type of the object itself is &lt;span class=&quot;code&quot;&gt;DoubleNode&lt;/span&gt;. It
cannot prove that it must be a &lt;span class=&quot;code&quot;&gt;NodeType&lt;/span&gt; and there is
no way in C# to declare that it must be. Currently, in C#, we are stuck with the
cast. A couple languages have tried to deal
with this problem. One such language is
&lt;a href=&quot;http://www.cs.pomona.edu/~kim/README.html#Match&quot;&gt;LOOM&lt;/a&gt; by
&lt;a href=&quot;http://www.cs.pomona.edu/~kim/&quot;&gt;Kim Bruce&lt;/a&gt; and others. LOOM allows
declaring that a variable must be the same type as &lt;b&gt;&lt;span class=&quot;code&quot;&gt;
this&lt;/span&gt;&lt;/b&gt;. What that might look like in our example is the following,&lt;/p&gt;
&lt;pre&gt;        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; DoubleNode: Node {
            &lt;b&gt;this&lt;/b&gt; _previous;

            &lt;b&gt;public&lt;/b&gt; &lt;b&gt;override&lt;/b&gt; NodeType Next {
                get {
                    &lt;b&gt;return&lt;/b&gt; &lt;b&gt;base&lt;/b&gt;.Next;
                }
                set {
                    &lt;b&gt;base&lt;/b&gt;.Next = value;
                    value._previous = &lt;b&gt;this&lt;/b&gt;;
                }
            }

            &lt;b&gt;public&lt;/b&gt; &lt;b&gt;this&lt;/b&gt; Previous { &lt;b&gt;get&lt;/b&gt; { &lt;b&gt;return&lt;/b&gt; _previous; } }
        }&lt;/pre&gt;
&lt;p&gt;where I assume that the keyword &lt;b&gt;&lt;span class=&quot;code&quot;&gt;this&lt;/span&gt;&lt;/b&gt; is used
where the type is assumed to be the type of &lt;b&gt;&lt;span class=&quot;code&quot;&gt;this&lt;/span&gt;&lt;/b&gt;.
This concept also shows up in Eiffel as &lt;span class=&quot;code&quot;&gt;&lt;b&gt;like&lt;/b&gt; Current&lt;/span&gt;. Unfortunately, proving that &lt;span class=&quot;code&quot;&gt;NodeType&lt;/span&gt;
and &lt;span class=&quot;code&quot;&gt;DoubleNode&lt;/span&gt;&apos;s &lt;b&gt;&lt;span class=&quot;code&quot;&gt;this&lt;/span&gt;&lt;/b&gt;
type are identitical is a non-trivial problem and has the side affect of adding
exact typing to the language or leaves the type system unsound as Eiffel&apos;s
&lt;span class=&quot;code&quot;&gt;&lt;b&gt;like&lt;/b&gt; Current&lt;/span&gt; does.&lt;/p&gt;
&lt;p&gt;Another approach is to constrain the decedents of &lt;span class=&quot;code&quot;&gt;
DoubleNode&lt;/span&gt; to also to be decedents of &lt;span class=&quot;code&quot;&gt;NodeType&lt;/span&gt;.
The only requirement we have on &lt;b&gt;&lt;span class=&quot;code&quot;&gt;this&lt;/span&gt;&lt;/b&gt; is that it
be assignable to a variable of type &lt;span class=&quot;code&quot;&gt;NodeType&lt;/span&gt;. If we
could express that all concrete implementations of &lt;span class=&quot;code&quot;&gt;DoubleNode&lt;/span&gt;
are required to be decedents of &lt;span class=&quot;code&quot;&gt;NodeType&lt;/span&gt; this
requirement would be met. This is the approach that
&lt;a href=&quot;http://scala.epfl.ch/&quot;&gt;Scala&lt;/a&gt;, by
&lt;a href=&quot;http://lampwww.epfl.ch/~odersky/&quot;&gt;Martin Odersky&lt;/a&gt; and others, took to the problem. What this might
look like in C# is,&lt;/p&gt;
&lt;pre&gt;        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;abstract class&lt;/b&gt; DoubleNode: Node &lt;b&gt;requires&lt;/b&gt; NodeType {
            NodeType _previous;

            &lt;b&gt;public&lt;/b&gt; &lt;b&gt;override&lt;/b&gt; NodeType Next {
                get {
                    &lt;b&gt;return&lt;/b&gt; &lt;b&gt;base&lt;/b&gt;.Next;
                }
                set {
                    &lt;b&gt;base&lt;/b&gt;.Next = value;
                    value._previous = &lt;b&gt;this&lt;/b&gt;;
                }
            }

            &lt;b&gt;public&lt;/b&gt; NodeType Previous { &lt;b&gt;get&lt;/b&gt; { &lt;b&gt;return&lt;/b&gt; _previous; } }
        }&lt;/pre&gt;
&lt;p&gt;This states that descendants of &lt;span class=&quot;code&quot;&gt;DoubleNode&lt;/span&gt; are required to also be decedents of type
&lt;span class=&quot;code&quot;&gt;NodeType&lt;/span&gt; (or be &lt;span class=&quot;code&quot;&gt;NodeType&lt;/span&gt;, as
is more likely the case). Knowing this we can then remove the cast of &lt;b&gt;&lt;span class=&quot;code&quot;&gt;this&lt;/span&gt;&lt;/b&gt;
because we are guaranteed it is assignment compatible with &lt;span class=&quot;code&quot;&gt;NodeType&lt;/span&gt;. &lt;/p&gt;
&lt;p&gt;Whether either of these extensions make it into C# is anyone&apos;s guess. It is
still open whether the solutions presented are really better than the problem. Is
requiring the cast really that bad? I will leave that up to you to answer. Also
I glossed over some tricky problems that would need to be addressed to implement
either option, not the least of which are the changes to the CLR.&lt;/p&gt;
&lt;p&gt;This all started off innocently enough. We wanted to implement&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;interface&lt;/b&gt; ILinkedList&amp;lt;T&amp;gt;: IEnumerable&amp;lt;T&amp;gt; {
        &lt;b&gt;void&lt;/b&gt; Add(T value);
        &lt;b&gt;void&lt;/b&gt; Clear();
    }&lt;/pre&gt;
&lt;p&gt;and,&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;interface&lt;/b&gt; IDoubleLinkedList&amp;lt;T&amp;gt;: ILinkedList&amp;lt;T&amp;gt; {
        IEnumerable&amp;lt;T&amp;gt; Backwards { &lt;b&gt;get&lt;/b&gt;; }
    }&lt;/pre&gt;
&lt;p&gt;but things got complicated when we wanted to share code and
remove casts. This innocent looking problem is devilishly hard to express in a
type-checked object-oriented language.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/2006/04/14/Linked-List-IV-Removing-casts</link>
                <guid>http://removingalldoubt.com/programming/2006/04/14/Linked-List-IV-Removing-casts</guid>
                <pubDate>2006-04-14T13:56:00+00:00</pubDate>
        </item>

        <item>
                <title>The Thorpe has Landed</title>
                <description>&lt;p&gt;Danny Thorpe, a long time friend and colleague of mine, has a new, new job
and a new &lt;a href=&quot;http://blogs.msdn.com/dthorpe/&quot;&gt;blog&lt;/a&gt;. We worked together
for a long time at his old old job. His old new job (or new old job?) was at
some little known internet search company. His new new job is at a small
software company based somewhere in Washington; the state not the city. Danny
once gave me a lab coat when I got a title of Chief Scientist. I need to think
of some suitable gift for someone with an equally inaccurate &quot;manager&quot; in his
title...&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/2006/04/14/The-Thorpe-has-Landed</link>
                <guid>http://removingalldoubt.com/2006/04/14/The-Thorpe-has-Landed</guid>
                <pubDate>2006-04-14T12:50:00+00:00</pubDate>
        </item>

        <item>
                <title>Linked List III: Double-linked list</title>
                <description>&lt;p&gt;A straight forward implementation of a double-linked list that implements the 
interface,&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;interface&lt;/b&gt; IDoubleLinkedList&amp;lt;T&amp;gt;: ILinkedList&amp;lt;T&amp;gt; {
        IEnumerable&amp;lt;T&amp;gt; Backwards { get; }
    }
&lt;/pre&gt;
&lt;p&gt;can be implemented as,&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;class&lt;/b&gt; DoubleLinkedList&amp;lt;T&amp;gt;: IDoubleLinkedList&amp;lt;T&amp;gt;, IEnumerable&amp;lt;T&amp;gt; {

        &lt;b&gt;private&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; Node {
            &lt;b&gt;public&lt;/b&gt; T Data;
            &lt;b&gt;public&lt;/b&gt; Node Next;
            &lt;b&gt;public&lt;/b&gt; Node Previous;
        }

        &lt;b&gt;private&lt;/b&gt; Node _last;

        &lt;b&gt;public&lt;/b&gt; DoubleLinkedList() { }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Add(T value) {
            Node newNode = &lt;b&gt;new&lt;/b&gt; Node();
            newNode.Data = value;
            &lt;b&gt;if&lt;/b&gt; (_last == &lt;b&gt;null&lt;/b&gt;) {
                newNode.Next = newNode;
                newNode.Previous = newNode;
                _last = newNode;
            }
            &lt;b&gt;else&lt;/b&gt; {
                newNode.Next = _last.Next;
                _last.Next.Previous = newNode;
                _last.Next = newNode;
                newNode.Previous = _last;
                _last = newNode;
            }
        }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Clear() {
            _last = &lt;b&gt;null&lt;/b&gt;;
        }

        &lt;b&gt;public&lt;/b&gt; IEnumerator&amp;lt;T&amp;gt; GetEnumerator() {
            &lt;b&gt;if&lt;/b&gt; (_last != &lt;b&gt;null&lt;/b&gt;) {
                Node current = _last;
                &lt;b&gt;do&lt;/b&gt; {
                    current = current.Next;
                    yield &lt;b&gt;return&lt;/b&gt; current.Data;
                } &lt;b&gt;while&lt;/b&gt; (current != _last);
            }
        }

        IEnumerator IEnumerable.GetEnumerator() {
            &lt;b&gt;return&lt;/b&gt; GetEnumerator();
        }

        &lt;b&gt;public&lt;/b&gt; IEnumerable&amp;lt;T&amp;gt; Backwards {
            get {
                &lt;b&gt;if&lt;/b&gt; (_last != &lt;b&gt;null&lt;/b&gt;) {
                    Node current = _last;
                    &lt;b&gt;do&lt;/b&gt; {
                        yield &lt;b&gt;return&lt;/b&gt; current.Data;
                        current = current.Previous;
                    } &lt;b&gt;while&lt;/b&gt; (current != _last);
                }
            }
        }
    }&lt;/pre&gt;
&lt;p&gt;With a casual comparison of this implementation to the implementation of
&lt;span class=&quot;code&quot;&gt;LinkList&amp;lt;T&amp;gt;&lt;/span&gt; you will see that most of the methods are
the same. &lt;span class=&quot;code&quot;&gt;Clear()&lt;/span&gt; and &lt;span class=&quot;code&quot;&gt;GetEnumator()&lt;/span&gt;
are identical. &lt;span class=&quot;code&quot;&gt;Add()&lt;/span&gt; is nearly identical; in fact, it
would be identical if we changed the &lt;span class=&quot;code&quot;&gt;Node&lt;/span&gt;
implementation to&lt;/p&gt;
&lt;pre&gt;        &lt;b&gt;private&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; Node {
            &lt;b&gt;public&lt;/b&gt; T Data;
            &lt;b&gt;private&lt;/b&gt; Node _next;
            &lt;b&gt;public&lt;/b&gt; Node Previous;

            &lt;b&gt;public&lt;/b&gt; Node Next {
                get {
                    &lt;b&gt;return&lt;/b&gt; _next;
                }
                set {
                    _next = value;
                    value.Previous = &lt;b&gt;this&lt;/b&gt;;
                }
            }
        }&lt;/pre&gt;
&lt;p&gt;With this change to the &lt;span class=&quot;code&quot;&gt;Node&lt;/span&gt; class, the only
difference between &lt;span class=&quot;code&quot;&gt;LinkedList&amp;lt;T&amp;gt;&lt;/span&gt; and
&lt;span class=&quot;code&quot;&gt;DoubleLinked&amp;lt;T&amp;gt;&lt;/span&gt; is the &lt;span class=&quot;code&quot;&gt;Node&lt;/span&gt;
class and the &lt;span class=&quot;code&quot;&gt;Backwards&lt;/span&gt; property. It seems obvious we
should use inheritance to share code.
&lt;span class=&quot;code&quot;&gt;DoubleLinked&amp;lt;T&amp;gt;&lt;/span&gt; should derive from &lt;span class=&quot;code&quot;&gt;
LinkedList&amp;lt;T&amp;gt;&lt;/span&gt;. To allow code sharing we need
to make some changes to &lt;span class=&quot;code&quot;&gt;LinkedList&amp;lt;T&amp;gt;&lt;/span&gt;. First we
need to change &lt;span class=&quot;code&quot;&gt;Node&lt;/span&gt; implementation to allow
&lt;span class=&quot;code&quot;&gt;DoubleLink&amp;lt;T&amp;gt;&lt;/span&gt; to override the behavior of
&lt;span class=&quot;code&quot;&gt;Next&lt;/span&gt;,&lt;/p&gt;
&lt;pre&gt;        &lt;b&gt;protected&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; Node {
            &lt;b&gt;public&lt;/b&gt; T Data;
            &lt;b&gt;private&lt;/b&gt; Node _next;
            &lt;b&gt;public&lt;/b&gt; &lt;b&gt;virtual&lt;/b&gt; Node Next { get { &lt;b&gt;return&lt;/b&gt; _next; } set { _next = value; } }
        }&lt;/pre&gt;
&lt;p&gt;Then we need to add,&lt;/p&gt;
&lt;pre&gt;        &lt;b&gt;protected&lt;/b&gt; Node Last { get { &lt;b&gt;return&lt;/b&gt; _last; } }&lt;/pre&gt;
&lt;p&gt;to allow &lt;span class=&quot;code&quot;&gt;DoubleLinked&amp;lt;T&amp;gt;&lt;/span&gt; to access to
&lt;span class=&quot;code&quot;&gt;_last&lt;/span&gt; but not modify it. &lt;span class=&quot;code&quot;&gt;DoubleLink&amp;lt;T&amp;gt;&lt;/span&gt;
need &lt;span class=&quot;code&quot;&gt;Last&lt;/span&gt; to implement &lt;span class=&quot;code&quot;&gt;Backwards&lt;/span&gt;. Additionally we need to
introduce&lt;/p&gt;
&lt;pre&gt;        &lt;b&gt;protected&lt;/b&gt; &lt;b&gt;virtual&lt;/b&gt; Node CreateNode() { &lt;b&gt;return&lt;/b&gt; &lt;b&gt;new&lt;/b&gt; Node(); }&lt;/pre&gt;
&lt;p&gt;and change the first statement in the &lt;span class=&quot;code&quot;&gt;Add()&lt;/span&gt; method to,&lt;/p&gt;
&lt;pre&gt;            Node newNode = CreateNode();&lt;/pre&gt;
&lt;p&gt;to allow &lt;span class=&quot;code&quot;&gt;DoubleLinked&amp;lt;T&amp;gt;&lt;/span&gt; to override which class to
create for the nodes in the list. This allows &lt;span class=&quot;code&quot;&gt;DoubleLink&amp;lt;T&amp;gt;&lt;/span&gt;
to create &lt;span class=&quot;code&quot;&gt;DoubleNode&lt;/span&gt; instead of &lt;span class=&quot;code&quot;&gt;
Node&lt;/span&gt;. With these changes we can now change the implementation of
&lt;span class=&quot;code&quot;&gt;DoubleLink&amp;lt;T&amp;gt;&lt;/span&gt; implementation to,&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;class&lt;/b&gt; DoubleLinkedList&amp;lt;T&amp;gt;: LinkedList&amp;lt;T&amp;gt;, IDoubleLinkedList&amp;lt;T&amp;gt; {

        &lt;b&gt;protected&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; DoubleNode: Node {
            &lt;b&gt;public&lt;/b&gt; DoubleNode Previous;

            &lt;b&gt;public&lt;/b&gt; &lt;b&gt;override&lt;/b&gt; Node Next {
                get {
                    &lt;b&gt;return&lt;/b&gt; &lt;b&gt;base&lt;/b&gt;.Next;
                }
                set {
                    &lt;b&gt;base&lt;/b&gt;.Next = value;
                    ((DoubleNode)value).Previous = &lt;b&gt;this&lt;/b&gt;;
                }
            }
        }

        &lt;b&gt;public&lt;/b&gt; IEnumerable&amp;lt;T&amp;gt; Backwards {
            get {
                &lt;b&gt;if&lt;/b&gt; (Last != &lt;b&gt;null&lt;/b&gt;) {
                    DoubleNode current = (DoubleNode)Last;
                    &lt;b&gt;do&lt;/b&gt; {
                        yield &lt;b&gt;return&lt;/b&gt; current.Data;
                        current = current.Previous;
                    } &lt;b&gt;while&lt;/b&gt; (current != Last);
                }
            }
        }

        &lt;b&gt;protected&lt;/b&gt; &lt;b&gt;override&lt;/b&gt; LinkedList&amp;lt;T&amp;gt;.Node CreateNode() { &lt;b&gt;return&lt;/b&gt; &lt;b&gt;new&lt;/b&gt; DoubleNode();  }
    }&lt;/pre&gt;
&lt;p&gt;There we have it. We used inheritance to share code. Unfortunately, we had to
use casts to get this to work. The casts show we are now running into some
limitations in the expressiveness of the C# type
system. We know that all the items of the list will be of type &lt;span class=&quot;code&quot;&gt;
DoubleNode&lt;/span&gt; but we need to convince the compiler (and the CLR) with a cast
in the &lt;b&gt;&lt;span class=&quot;code&quot;&gt;set&lt;/span&gt;&lt;/b&gt; part of the &lt;span class=&quot;code&quot;&gt;Next&lt;/span&gt;
method and the &lt;span class=&quot;code&quot;&gt;Backwards&lt;/span&gt; property. It
would be nice to write this without casts. Next time we look at a way to rid
ourselves of one the cast in the &lt;span class=&quot;code&quot;&gt;Backwards&lt;/span&gt; property and the virtual &lt;span class=&quot;code&quot;&gt;CreateNode()&lt;/span&gt;
method. We will also look at some possible ways to increase the expressiveness
of C#&apos;s type system to allow us to get rid of the final cast.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/2006/04/10/Linked-List-III-Double-linked-list</link>
                <guid>http://removingalldoubt.com/programming/2006/04/10/Linked-List-III-Double-linked-list</guid>
                <pubDate>2006-04-10T18:26:00+00:00</pubDate>
        </item>

        <item>
                <title>Linked Lists II: Simple Generics</title>
                <description>&lt;p&gt;In the last entry we looked at implementing a collection class 
using a linked list data structure. This time I will show some ways to use that implementation
to implement the generic
interfaces introduced last time. The complete class looks like,&lt;/p&gt;

&lt;pre&gt;    &lt;b&gt;class&lt;/b&gt; LinkedList: IEnumerable {

        &lt;b&gt;private&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; Node {
            &lt;b&gt;public&lt;/b&gt; &lt;b&gt;object&lt;/b&gt; Data;
            &lt;b&gt;public&lt;/b&gt; Node Next;
        }

        &lt;b&gt;private&lt;/b&gt; Node _last;

        &lt;b&gt;public&lt;/b&gt; LinkedList() { }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Add(&lt;b&gt;object&lt;/b&gt; value) {
            Node newNode = &lt;b&gt;new&lt;/b&gt; Node();
            newNode.Data = value;
            &lt;b&gt;if&lt;/b&gt; (_last == &lt;b&gt;null&lt;/b&gt;) {
                newNode.Next = newNode;
                _last = newNode;
            }
            &lt;b&gt;else&lt;/b&gt; {
                newNode.Next = _last.Next;
                _last.Next = newNode;
                _last = newNode;
            }
        }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Clear() {
            _last = &lt;b&gt;null&lt;/b&gt;;
        }

        &lt;b&gt;public&lt;/b&gt; IEnumerator GetEnumerator() {
            &lt;b&gt;if&lt;/b&gt; (_last != &lt;b&gt;null&lt;/b&gt;) {
                Node current = _last;
                &lt;b&gt;do&lt;/b&gt; {
                    current = current.Next;
                    yield &lt;b&gt;return&lt;/b&gt; current.Data;
                } &lt;b&gt;while&lt;/b&gt; (current != _last);
            }
        }
    }&lt;/pre&gt;
&lt;p&gt;The interface we want to implement is,&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;interface&lt;/b&gt; ILinkedList&amp;lt;T&amp;gt;: IEnumerable&amp;lt;T&amp;gt; {
        &lt;b&gt;void&lt;/b&gt; Add(T value);
        &lt;b&gt;void&lt;/b&gt; Clear();
    }&lt;/pre&gt;
&lt;p&gt;One obvious, but bad, way to implement this interface is to
subclass &lt;span class=&quot;code&quot;&gt;LinkedList&lt;/span&gt; like,&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;class&lt;/b&gt; LinkedList&amp;lt;T&amp;gt;: LinkedList, ILinkedList&amp;lt;T&amp;gt;, IEnumerable&amp;lt;T&amp;gt; {

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Add(T value) { &lt;b&gt;base&lt;/b&gt;.Add(value); }

        IEnumerator&amp;lt;T&amp;gt; IEnumerable&amp;lt;T&amp;gt;.GetEnumerator() {
            &lt;b&gt;foreach&lt;/b&gt; (T item &lt;b&gt;in&lt;/b&gt; &lt;b&gt;this&lt;/b&gt;)
                yield &lt;b&gt;return&lt;/b&gt; item;
        }
    }&lt;/pre&gt;
&lt;p&gt;The reason this is so bad is because given,&lt;/p&gt;
&lt;pre&gt;    LinkedList&amp;lt;&lt;b&gt;string&lt;/b&gt;&amp;gt; list = &lt;b&gt;new&lt;/b&gt; LinkedList&amp;lt;&lt;b&gt;string&lt;/b&gt;&amp;gt;();&lt;/pre&gt;
&lt;p&gt;the following statements are both legal,&lt;/p&gt;
&lt;pre&gt;    list.Add(&quot;some string&quot;);
    list.Add(1);&lt;/pre&gt;
&lt;p&gt;The reason is because the first statement calls the &lt;span class=&quot;code&quot;&gt;
LinkedList&amp;lt;T&amp;gt;.Add(T value)&lt;/span&gt; above, as expected, but the second will call
&lt;span class=&quot;code&quot;&gt;LinkedList.Add(&lt;b&gt;object&lt;/b&gt; value)&lt;/span&gt;, because the
generic &lt;span class=&quot;code&quot;&gt;Add&lt;/span&gt; is overloaded with the the non-generic version. Even if it
was obscured by the new &lt;span class=&quot;code&quot;&gt;Add&lt;/span&gt; method you could still access
&lt;span class=&quot;code&quot;&gt;Add(&lt;b&gt;object&lt;/b&gt; value)&lt;/span&gt; by,&lt;/p&gt;
&lt;pre&gt;    LinkedList list2 = list;
    list2.Add(1);&lt;/pre&gt;
&lt;p&gt;We wanted the compiler to be able to flag when we add a
non-string to the list as an error; this implementation doesn&apos;t do that. A slightly better approach is
to use delegation instead of
inheritance, as in,&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;class&lt;/b&gt; LinkedListD&amp;lt;T&amp;gt;: ILinkedList&amp;lt;T&amp;gt;, IEnumerable&amp;lt;T&amp;gt; {
        &lt;b&gt;private&lt;/b&gt; LinkedList _list = &lt;b&gt;new&lt;/b&gt; LinkedList();

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Add(T value) { _list.Add(value); }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Clear() { _list.Clear(); }

        IEnumerator&amp;lt;T&amp;gt; IEnumerable&amp;lt;T&amp;gt;.GetEnumerator() {
            &lt;b&gt;foreach&lt;/b&gt; (T item &lt;b&gt;in&lt;/b&gt; _list)
                yield &lt;b&gt;return&lt;/b&gt; item;
        }

        IEnumerator IEnumerable.GetEnumerator() {
            &lt;b&gt;return&lt;/b&gt; _list.GetEnumerator();
        }
    }&lt;/pre&gt;
&lt;p&gt;But this is still not good enough because, not only can we no longer inherit the implementation of &lt;span class=&quot;code&quot;&gt;
Clear()&lt;/span&gt; and &lt;span class=&quot;code&quot;&gt;GetEnumerator()&lt;/span&gt;, we also use
more memory by having to create an instance of &lt;span class=&quot;code&quot;&gt;LinkedList&lt;/span&gt;
to wrap. Additionally, if we wanted to add an &lt;span class=&quot;code&quot;&gt;Insert()&lt;/span&gt;
method, that put values at the beginning of the list instead of the end, we would have to
manually update &lt;span class=&quot;code&quot;&gt;LinkedListD&lt;/span&gt;. A much better approach is to scrap the first implementation and
re-implement the class as a generic class, as in,&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;class&lt;/b&gt; LinkedList&amp;lt;T&amp;gt;: ILinkedList&amp;lt;T&amp;gt;, IEnumerable&amp;lt;T&amp;gt; {
        &lt;b&gt;private&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; Node {
            &lt;b&gt;public&lt;/b&gt; T Data;
            &lt;b&gt;public&lt;/b&gt; Node Next;
        }

        &lt;b&gt;private&lt;/b&gt; Node _last;

        &lt;b&gt;public&lt;/b&gt; LinkedList() { }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Add(T value) {
            Node newNode = &lt;b&gt;new&lt;/b&gt; Node();
            newNode.Data = value;
            &lt;b&gt;if&lt;/b&gt; (_last == &lt;b&gt;null&lt;/b&gt;) {
                newNode.Next = newNode;
                _last = newNode;
            }
            &lt;b&gt;else&lt;/b&gt; {
                newNode.Next = _last.Next;
                _last.Next = newNode;
                _last = newNode;
            }
        }

        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Clear() {
            _last = &lt;b&gt;null&lt;/b&gt;;
        }

        &lt;b&gt;public&lt;/b&gt; IEnumerator&amp;lt;T&amp;gt; GetEnumerator() {
            &lt;b&gt;if&lt;/b&gt; (_last != &lt;b&gt;null&lt;/b&gt;) {
                Node current = _last;
                &lt;b&gt;do&lt;/b&gt; {
                    current = current.Next;
                    yield &lt;b&gt;return&lt;/b&gt; current.Data;
                } &lt;b&gt;while&lt;/b&gt; (current != _last);
            }
        }

        IEnumerator IEnumerable.GetEnumerator() {
            &lt;b&gt;return&lt;/b&gt; GetEnumerator();
        }
    }&lt;/pre&gt;
&lt;p&gt;With this approach, if we really need a list with data type &lt;b&gt;
&lt;span class=&quot;code&quot;&gt;object&lt;/span&gt;&lt;/b&gt;, we can always use &lt;span class=&quot;code&quot;&gt;LinkedList&amp;lt;&lt;b&gt;object&lt;/b&gt;&amp;gt;&lt;/span&gt;
which gives us a class essentially the same class as what we started with.&amp;nbsp;
Sometimes it is just better to rewrite. Note that the BCL took a similar
approach. List&amp;lt;T&amp;gt; and Dictionary&amp;lt;K,T&amp;gt; do not inherit from ArrayList and
Hashtable.&lt;/p&gt;
&lt;p&gt;Next time I will try a first crack at implementing &lt;span class=&quot;code&quot;&gt;
IDoubleLinkedList&amp;lt;T&amp;gt;&lt;/span&gt;.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/2006/04/01/Linked-Lists-II-Simple-Generics</link>
                <guid>http://removingalldoubt.com/programming/2006/04/01/Linked-Lists-II-Simple-Generics</guid>
                <pubDate>2006-04-01T12:25:00+00:00</pubDate>
        </item>

        <item>
                <title>Linked Lists I: Simple implementation</title>
                <description>&lt;p&gt;Last time I investigated several ways to implement an expression evaluator
using a procedural style and several object oriented styles. I used this to
demonstrate a data structure that doesn&apos;t lend itself well to being representing
in an object oriented style. Another problematic structure is a linked list. It
is not so much the linked list itself but writing it is such a way that is both
type safe and also supports code sharing with, say, a doubly-linked list. What I want to implement is a class
that implements the following interface.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;    &lt;b&gt;interface&lt;/b&gt; ILinkedList&amp;lt;T&amp;gt;: IEnumerable&amp;lt;T&amp;gt; {
        &lt;b&gt;void&lt;/b&gt; Add(T value);
        &lt;b&gt;void&lt;/b&gt; Clear();
    }&lt;/pre&gt;

&lt;p&gt;And then a subclass of that class that implements,&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;    &lt;b&gt;interface&lt;/b&gt; IDoubleLinkedList&amp;lt;T&amp;gt;: ILinkedList&amp;lt;T&amp;gt; {
        IEnumerable&amp;lt;T&amp;gt; Backwards { &lt;b&gt;get&lt;/b&gt;; }
    }&lt;/pre&gt;
&lt;p&gt;which inherits the implementation of &lt;span class=&quot;code&quot;&gt;Add()&lt;/span&gt; and
&lt;span class=&quot;code&quot;&gt;Clear()&lt;/span&gt; and efficiently
implements &lt;span class=&quot;code&quot;&gt;Backwards&lt;/span&gt;. For now, I will ignore the generic parameter and just
implement a simple linked list. First, lets create a Node that will form the
nodes of the linked list.&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;class&lt;/b&gt; Node {
        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;object&lt;/b&gt; Data;
        &lt;b&gt;public&lt;/b&gt; Node Next;
    }&lt;/pre&gt;
&lt;p&gt;A linked list consists of a list of these nodes.&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;class&lt;/b&gt; LinkedList: IEnumerable {
        Node _last;
    }&lt;/pre&gt;
&lt;p&gt;When an item is added to the list we will create a new node to hold it and
create circular list where the &lt;span class=&quot;code&quot;&gt;_last&lt;/span&gt; instance variable
will always point to the last node. This way we can quickly find both the
last node and the first node of the list because, since the list is circular,
the last node&apos;s &lt;span class=&quot;code&quot;&gt;Next&lt;/span&gt; field points to the first node.
The reason I chose this particular way of implementing a linked list will be
obvious if you have watched
&lt;a href=&quot;http://channel9.msdn.com/ShowPost.aspx?PostID=159952#159952&quot;&gt;this video&lt;/a&gt;.&lt;/p&gt;
&lt;pre&gt;        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Add(&lt;b&gt;object&lt;/b&gt; value) {
            Node newNode = &lt;b&gt;new&lt;/b&gt; Node();
            newNode.Data = value;
            &lt;b&gt;if&lt;/b&gt; (_last == &lt;b&gt;null&lt;/b&gt;) {
                newNode.Next = newNode;
                _last = newNode;
            }
            &lt;b&gt;else&lt;/b&gt; {
                newNode.Next = _last.Next;
                _last.Next = newNode;
                _last = newNode;
            }
        }&lt;/pre&gt;
&lt;p&gt;Clearing the list is simple, just forget all the nodes.&lt;/p&gt;
&lt;pre&gt;        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; Clear() {
            _last = &lt;b&gt;null&lt;/b&gt;;
        }&lt;/pre&gt;
&lt;p&gt;I implemented &lt;span class=&quot;code&quot;&gt;IEnumerable&lt;/span&gt; interface by using the new
&lt;b&gt;&lt;span class=&quot;code&quot;&gt;yield&lt;/span&gt;&lt;/b&gt; feature of C# 2.0.
I start at the last node of the list, walk forward (to the first node) and yield an
element, each successive loop will yield another element until we have yielded the
last element (which is pointed to by &lt;span class=&quot;code&quot;&gt;_last&lt;/span&gt;).&lt;/p&gt;
&lt;pre&gt;        &lt;b&gt;public&lt;/b&gt; IEnumerator GetEnumerator() {
            &lt;b&gt;if&lt;/b&gt; (_last != &lt;b&gt;null&lt;/b&gt;) {
                Node current = _last;
                &lt;b&gt;do&lt;/b&gt; {
                    current = current.Next;
                    &lt;b&gt;yield&lt;/b&gt; &lt;b&gt;return&lt;/b&gt; current.Data;
                } &lt;b&gt;while&lt;/b&gt; (current != _last);
            }
        }&lt;/pre&gt;
&lt;p&gt;Now I have a reasonable implementation of a linked list. Next time we will
take several different approaches to implement the interfaces defined above.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/2006/03/26/Linked-Lists-I-Simple-implementation</link>
                <guid>http://removingalldoubt.com/programming/2006/03/26/Linked-Lists-I-Simple-implementation</guid>
                <pubDate>2006-03-26T12:07:00+00:00</pubDate>
        </item>

        <item>
                <title>A Tour of XAML - Part VI - Forward Compatibility (continued)</title>
                <description>&lt;p&gt;Returning to the Party example we where using, lets say you want to author a
document that has 10 kazoos when the version 2 assembly is present but replaces
them with 4 extra balloons if we only have the version 1 assembly. This can be
written using a markup compatibility AlternateContent block. This might look
like,&lt;/p&gt;
&lt;pre&gt;    &amp;lt;Party mc:Ignorable=&quot;v2&quot;
      xmln=&quot;...assembly-v1-uri...&quot;
      xmlns:v2=&quot;...assembly-v2-uri...&quot;
      xmlns:mc=&quot;http://schemas.micrsoft.com/winfx/2006/markup-compatibility&quot;&amp;gt;
        &amp;lt;Balloon Color=&quot;Red&quot; /&amp;gt;
        &amp;lt;Balloon Color=&quot;Blue&quot; /&amp;gt;
        &amp;lt;mc:AlternateContent&amp;gt;
            &amp;lt;mc:Choice Requires=&quot;v2&quot;&amp;gt;
                &amp;lt;Balloon Color=&quot;Green&quot; Shape=&quot;Dog&quot; /&amp;gt;
                &amp;lt;v2:Favor Kind=&quot;Kazoo&quot; Quantity=&quot;10&quot; /&amp;gt;
            &amp;lt;/mc:Choice&amp;gt;
            &amp;lt;mc:Fallback&amp;gt;
                &amp;lt;Balloon Color=&quot;Green&quot; /&amp;gt;
                &amp;lt;Balloon Color=&quot;Violet&quot; /&amp;gt;
                &amp;lt;Balloon Color=&quot;Orange&quot; /&amp;gt;
                &amp;lt;Balloon Color=&quot;Yellow&quot; /&amp;gt;
            &amp;lt;/mc:Fallback&amp;gt;
        &amp;lt;/mc:AlternateContent&amp;gt;
    &amp;lt;/Party&amp;gt;&lt;/pre&gt;

&lt;p&gt;When the version 1 assembly is present this is interpreted as,&lt;/p&gt;
&lt;pre&gt;    &amp;lt;Party xmln=&quot;...assembly-v1-uri...&quot;&amp;gt;
        &amp;lt;Balloon Color=&quot;Red&quot; /&amp;gt;
        &amp;lt;Balloon Color=&quot;Blue&quot; /&amp;gt;
        &amp;lt;Balloon Color=&quot;Green&quot; /&amp;gt;
        &amp;lt;Balloon Color=&quot;Violet&quot; /&amp;gt;
        &amp;lt;Balloon Color=&quot;Orange&quot; /&amp;gt;
        &amp;lt;Balloon Color=&quot;Yellow&quot; /&amp;gt;
    &amp;lt;/Party&amp;gt;&lt;/pre&gt;

&lt;p&gt;but in version 2 it is interpreted as,&lt;/p&gt;
&lt;pre&gt;    &amp;lt;Party xmlns=&quot;...assembly-v2-uri...&quot;&amp;gt;
        &amp;lt;Balloon Color=&quot;Red&quot; /&amp;gt;
        &amp;lt;Balloon Color=&quot;Blue&quot; /&amp;gt;
        &amp;lt;Balloon Color=&quot;Green&quot; Shape=&quot;Dog&quot; /&amp;gt;
        &amp;lt;Favor Kind=&quot;Kazoo&quot; Quantity=&quot;10&quot; /&amp;gt;
    &amp;lt;/Party&amp;gt;&lt;/pre&gt;

&lt;p&gt;The content of the Choice element is used when the
v2 namespace is available because it is mentioned in the Requires attribute.
This corresponds to when the version 2 assembly is in use. If v2 is not
available, the content of the Fallback element is used. AlternateContent
supports an arbitrary list of Choice elements and the Requires can be an
arbitrary list of prefix names. &lt;/p&gt;
&lt;p&gt;Of course exchanging balloons for kazoos is not a very compelling example. For a more practical example, let&apos;s
assume we have following classes in version 1 of an assembly,&lt;/p&gt;
&lt;pre&gt;    [ContentProperty(&quot;Content&quot;)]
    &lt;b&gt;class&lt;/b&gt; Paragraph {
        &lt;b&gt;private&lt;/b&gt; List&amp;lt;Inline&amp;gt; _content = new List&amp;lt;Inline&amp;gt;();
        &lt;b&gt;public&lt;/b&gt; List&amp;lt;Inline&amp;gt; Content { &lt;b&gt;get&lt;/b&gt; { &lt;b&gt;return&lt;/b&gt; _content; } }
    }

    &lt;b&gt;abstract&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; Inline { }

    [ContentProperty(&quot;Text&quot;)]
    &lt;b&gt;class&lt;/b&gt; Run : Inline {
        &lt;b&gt;string&lt;/b&gt; _text;
        &lt;b&gt;public&lt;/b&gt; &lt;b&gt;string&lt;/b&gt; Text { &lt;b&gt;get&lt;/b&gt; { &lt;b&gt;return&lt;/b&gt; _text; } &lt;b&gt;set&lt;/b&gt; { _text = value; } }
    }

    &lt;b&gt;class&lt;/b&gt; Image : Inline {
        &lt;b&gt;private&lt;/b&gt; Uri _source;
        &lt;b&gt;public&lt;/b&gt; Uri Source { &lt;b&gt;get&lt;/b&gt; { &lt;b&gt;return&lt;/b&gt; _source; } &lt;b&gt;set&lt;/b&gt; { _source = value; } }
    }&lt;/pre&gt;

&lt;p&gt;Now in the version 2 assembly we add a new class that looks like,&lt;/p&gt;
&lt;pre&gt;    &lt;b&gt;class&lt;/b&gt; Graph : Inline {
        &lt;b&gt;private&lt;/b&gt; List&amp;lt;Point&amp;gt; _points = &lt;b&gt;new&lt;/b&gt; List&amp;lt;Point&amp;gt;();
        &lt;b&gt;public&lt;/b&gt; List&amp;lt;Point&amp;gt; Points { &lt;b&gt;get&lt;/b&gt; { &lt;b&gt;return&lt;/b&gt; _points; } }
    }&lt;/pre&gt;

&lt;p&gt;Now we want to write a paragraph that uses the new Graph class when version 2
of the assembly is present, because, for example, it scales better, but in
version 1 we will just use a bitmap image of the graph. This might
look like,&lt;/p&gt;
&lt;pre&gt;    &amp;lt;Paragraph mc:Ignorable=&quot;v2&quot;
      xmln=&quot;...assembly-v1-uri...&quot;
      xmlns:v2=&quot;...assembly-v2-uri...&quot;
      xmlns:mc=&quot;http://schemas.micrsoft.com/winfx/2006/markup-compatibility&quot;&amp;gt;
        &amp;lt;Run&amp;gt;The sales projects for the next quarter are&amp;lt;/Run&amp;gt;
        &amp;lt;AlternateContent&amp;gt;
            &amp;lt;mc:Choice Requires=&quot;v2&quot;&amp;gt;
                &amp;lt;v2:Graph Data=&quot;1,2 2,3 3,5&quot; /&amp;gt;
            &amp;lt;/mc:Choice&amp;gt;
            &amp;lt;mc:Fallback&amp;gt;
                 &amp;lt;Image Source=&quot;SalesData.jpg&quot; /&amp;gt;
            &amp;lt;/mc:Fallback&amp;gt;
        &amp;lt;/mc:AlternateContent&amp;gt;
    &amp;lt;/Paragraph&amp;gt;&lt;/pre&gt;

&lt;p&gt;When the version 2 assembly is present, this is interpreted as,&lt;/p&gt;

&lt;pre&gt;    &amp;lt;Paragraph xmlns=&quot;...assembly-v2-uri...&quot;&amp;gt;
        &amp;lt;Run&amp;gt;The sales projects for the next quarter are&amp;lt;/Run&amp;gt;
        &amp;lt;Graph Data=&quot;1,2 2,3 3,5&quot; /&amp;gt;
    &amp;lt;/Paragraph&amp;gt;&lt;/pre&gt;

&lt;p&gt;but, when only version 1 available, it is interpreted as,&lt;/p&gt;
&lt;pre&gt;    &amp;lt;Paragraph xmln=&quot;...assembly-v1-uri...&quot;&amp;gt;
        &amp;lt;Run&amp;gt;The sales projects for the next quarter are&amp;lt;/Run&amp;gt;
        &amp;lt;Image Source=&quot;SalesData.jpg&quot; /&amp;gt;
    &amp;lt;/Paragraph&amp;gt;&lt;/pre&gt;

&lt;p&gt;using the bitmap instead of of the Graph class. This allows a document author
to produce a file that takes advantage of a new feature, such as Graph with its
better scaling properties, but still allows older versions to see a the graph at
a lower fidelity.&lt;/p&gt;

&lt;p&gt;These classes make a better example for another feature of markup
compatibility, the ProcessContent attribute. Let&apos;s add another class in the
version 2 assembly,&lt;/p&gt;
&lt;pre&gt;    [ContentProperty(&quot;Content&quot;)]
    &lt;b&gt;class&lt;/b&gt; Glow : Inline {
        &lt;b&gt;private&lt;/b&gt; List&amp;lt;Inline&amp;gt; _content = new List&amp;lt;Inline&amp;gt;();
        &lt;b&gt;public&lt;/b&gt; List&amp;lt;Inline&amp;gt; Content { &lt;b&gt;get&lt;/b&gt; { &lt;b&gt;return&lt;/b&gt; _content; } }
    }&lt;/pre&gt;

&lt;p&gt;This class allows us to make the content to appear to glow when drawn. This
allows us to write a document that looks like,&lt;/p&gt;
&lt;pre&gt;    &amp;lt;Paragraph mc:Ignorable=&quot;v2&quot;
      xmln=&quot;...assembly-v1-uri...&quot;
      xmlns:v2=&quot;...assembly-v2-uri...&quot;
      xmlns:mc=&quot;http://schemas.micrsoft.com/winfx/2006/markup-compatibility&quot;&amp;gt;
        &amp;lt;Run&amp;gt;The word&amp;lt;/Run&amp;gt;
        &amp;lt;v2:Glow&amp;gt;&amp;lt;Run&amp;gt;glow&amp;lt;/Run&amp;gt;&amp;lt;/v2:Glow&amp;gt;
        &amp;lt;Run&amp;gt;will appear to glow when it viewed with version 2.&amp;lt;/Run&amp;gt;
    &amp;lt;/Paragraph&amp;gt;&lt;/pre&gt;

&lt;p&gt;Unfortunately, with version 1, this is interpreted as,&lt;/p&gt;
&lt;pre&gt;    &amp;lt;Paragraph xmln=&quot;...assembly-v1-uri...&quot;&amp;gt;
        &amp;lt;Run&amp;gt;The word&amp;lt;/Run&amp;gt;
        &amp;lt;Run&amp;gt;will appear to glow when it viewed with version 2.&amp;lt;/Run&amp;gt;
    &amp;lt;/Paragraph&amp;gt;&lt;/pre&gt;
&lt;p&gt;The word &quot;glow&quot; disappeared because, by default, when an element is ignored its
content is ignored as well. This default can be modified by using the ProcessContent attribute. If the original document is modified to be,&lt;/p&gt;
&lt;pre&gt;    &amp;lt;Paragraph
      mc:Ignorable=&quot;v2&quot;
      mc:ProcessContent=&quot;v2:Glow&quot;
      xmln=&quot;...assembly-v1-uri...&quot;
      xmlns:v2=&quot;...assembly-v2-uri...&quot;
      xmlns:mc=&quot;http://schemas.micrsoft.com/winfx/2006/markup-compatibility&quot;&amp;gt;
        &amp;lt;Run&amp;gt;The word&amp;lt;/Run&amp;gt;
        &amp;lt;v2:Glow&amp;gt;&amp;lt;Run&amp;gt;glow&amp;lt;/Run&amp;gt;&amp;lt;/v2:Glow&amp;gt;
        &amp;lt;Run&amp;gt;will appear to glow when it viewed with version 2.&amp;lt;/Run&amp;gt;
    &amp;lt;/Paragraph&amp;gt;&lt;/pre&gt;
&lt;p&gt;This will tell the reader to just ignore the tags for Glow if it doesn&apos;t
understand it but leave the content intact. This is then interpreted as,&lt;/p&gt;
&lt;pre&gt;    &amp;lt;Paragraph xmln=&quot;...assembly-v1-uri...&quot;&amp;gt;
        &amp;lt;Run&amp;gt;The word&amp;lt;/Run&amp;gt;
        &amp;lt;Run&amp;gt;glow&amp;lt;/Run&amp;gt;
        &amp;lt;Run&amp;gt;will appear to glow when it viewed with version 2.&amp;lt;/Run&amp;gt;
    &amp;lt;/Paragraph&amp;gt;&lt;/pre&gt;

&lt;p&gt;for version 1, and will be interpreted as,&lt;/p&gt;
&lt;pre&gt;    &amp;lt;Paragraph xmlns:v2=&quot;...assembly-v2-uri...&quot;&amp;gt;
        &amp;lt;Run&amp;gt;The word&amp;lt;/Run&amp;gt;
        &amp;lt;Glow&amp;gt;&amp;lt;Run&amp;gt;glow&amp;lt;/Run&amp;gt;&amp;lt;/Glow&amp;gt;
        &amp;lt;Run&amp;gt;will appear to glow when it viewed with version 2.&amp;lt;/Run&amp;gt;
    &amp;lt;/Paragraph&amp;gt;&lt;/pre&gt;

&lt;p&gt;when version 2 is present which is exactly what we wanted.&lt;/p&gt;
&lt;p&gt;In general, forward compatibility allows previous versions of a document
reader to read a document intended for later versions and render the content at
a lower fidelity. This helps immensely when deploying a newer version of an
assembly. During initial deployment it might not be that wide spread. You can
have your users start taking advantage of the new features immediately because
older versions will still be able to read newer documents, at a lower fidelity,
even when they don&apos;t have the new assembly.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/xaml/2006/03/18/A-Tour-of-XAML-Part-VI-Forward-Compatibility-(continued)</link>
                <guid>http://removingalldoubt.com/programming/xaml/2006/03/18/A-Tour-of-XAML-Part-VI-Forward-Compatibility-(continued)</guid>
                <pubDate>2006-03-18T16:26:00+00:00</pubDate>
        </item>

        <item>
                <title>A Tour of XAML V: Forward compatibility</title>
                <description>&lt;p&gt;As we saw last time, XAML supports backwards compatibility. XAML also
supports forward compatibility. Forward compatibility is easiest to understand from through a set of examples. Take the balloon
example before. Say we want to write a XAML document that says, when you have
the version 2 assembly, I want a dog shaped balloon. If, however, you only have
the version 1 assembly, a default shaped balloon is fine. In other words, I want
a document that contains some version 2 specific references in it but it is OK to just ignore
them when you have a version 1 assembly. In our example, we want to mark the Shape property some way that
the version 1 reader will know it can ignore the property. In XAML you can use
the markup compatibility namespace to accomplish this. You can write,&amp;lt;/p&amp;gt;&lt;/p&gt;

&lt;pre&gt;    &amp;lt;Balloon Color=&quot;Red&quot; v2:Shape=&quot;Dog&quot;
      xmlns=&quot;...assembly-v1-uri...&quot;
      xmlns:v2=&quot;...assembly-v2-uri...&quot;
      xmlns:mc=&quot;http://schemas.micrsoft.com/winfx/2006/markup-compatibility&quot;
      mc:Ignorable=&quot;v2&quot; /&amp;gt;&lt;/pre&gt;
&lt;p&gt;This takes advantage of the Ignorable attribute of from the markup
compatibility namespace. mc:Ignorable=&quot;v2&quot; says that any attribute or element
from the namespace
associated with the &quot;v2&quot; prefix can be ignored and you would still have a
document that would have an acceptable meaning. If a XAML reader only has the
version 1 assembly, it would interpreted the above document as,&lt;/p&gt;
&lt;pre&gt;    &amp;lt;Balloon Color=&quot;Red&quot; xmlns=&quot;...assembly-v1-uri...&quot;  /&amp;gt;&lt;/pre&gt;
&lt;p&gt;If the reader has the version 2 assembly, it would interpret it as,&lt;/p&gt;
&lt;pre&gt;    &amp;lt;Balloon Color=&quot;Red&quot; Shape=&quot;Dog&quot; xmlns=&quot;...assembly-v2-uri...&quot; /&amp;gt;&lt;/pre&gt;
&lt;p&gt;mc:Ignorable can also applies to elements. To see example of this lets define a class, Party, that is
define as,&lt;/p&gt;
&lt;pre&gt;    [ContentProperty(&quot;Balloons&quot;)]
    &lt;b&gt;public&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; Party {
        List&amp;lt;Balloon&amp;gt; _balloons = &lt;b&gt;new&lt;/b&gt; List&amp;lt;Balloon&amp;gt;();
        &lt;b&gt;public&lt;/b&gt; List&amp;lt;Balloon&amp;gt; Balloons { &lt;b&gt;get&lt;/b&gt; { return _balloons; }
    }&lt;/pre&gt;
&lt;p&gt;This allows us to define a Party instance that contains balloons. For this
example, let&apos;s assume that Party was defined in the Version 1 assembly and is
unmodified in the Version 2 assembly. We can then
write a XAML document that looks like,&lt;/p&gt;
&lt;pre&gt;    &amp;lt;Party xmln=&quot;...assembly-v1-uri...&quot;&amp;gt;
        &amp;lt;Balloon Color=&quot;Red&quot; /&amp;gt;
        &amp;lt;Balloon Color=&quot;Blue&quot; /&amp;gt;
    &amp;lt;/Party&amp;gt;&lt;/pre&gt;
&lt;p&gt;We can now also write a document that will include a third dog shaped balloon
but only if we are using the version 2 assembly, and not include if we are still
using version 1. It looks like,&lt;/p&gt;
&lt;pre&gt;    &amp;lt;Party mc:Ignorable=&quot;v2&quot;
      xmln=&quot;...assembly-v1-uri...&quot;
      xmlns:v2=&quot;...assembly-v2-uri...&quot;
      xmlns:mc=&quot;http://schemas.micrsoft.com/winfx/2006/markup-compatibility&quot;&amp;gt;
        &amp;lt;Balloon Color=&quot;Red&quot; /&amp;gt;
        &amp;lt;Balloon Color=&quot;Blue&quot; /&amp;gt;
        &amp;lt;v2:Balloon Color=&quot;Green&quot; Shape=&quot;Dog&quot; /&amp;gt;
    &amp;lt;/Party&amp;gt;&lt;/pre&gt;

&lt;p&gt;This will be interpreted exactly as the prior document when reading it with
version 1. With version 2, however, it would be interpreted as if it was,&lt;/p&gt;
&lt;pre&gt;    &amp;lt;Party xmlns=&quot;...assembly-v2-uri...&quot;&amp;gt;
        &amp;lt;Balloon Color=&quot;Red&quot; /&amp;gt;
        &amp;lt;Balloon Color=&quot;Blue&quot; /&amp;gt;
        &amp;lt;Balloon Color=&quot;Green&quot; Shape=&quot;Dog&quot; /&amp;gt;
    &amp;lt;/Party&amp;gt;&lt;/pre&gt;

&lt;p&gt;We can also add a new a new class, such as Favor, that can be used in a
document such as,&lt;/p&gt;
&lt;pre&gt;    &amp;lt;Party mc:Ignorable=&quot;v2&quot;
      xmln=&quot;...assembly-v1-uri...&quot;
      xmlns:v2=&quot;...assembly-v2-uri...&quot;
      xmlns:mc=&quot;http://schemas.micrsoft.com/winfx/2006/markup-compatibility&quot;&amp;gt;
        &amp;lt;Balloon Color=&quot;Red&quot; /&amp;gt;
        &amp;lt;Balloon Color=&quot;Blue&quot; /&amp;gt;
        &amp;lt;v2:Balloon Color=&quot;Green&quot; Shape=&quot;Dog&quot; /&amp;gt;
        &amp;lt;v2:Favor Kind=&quot;Kazoo&quot; Quantity=&quot;10&quot; /&amp;gt;
    &amp;lt;/Party&amp;gt;&lt;/pre&gt;

&lt;p&gt;This doesn&apos;t affect the version 1 interpretation of the document, but adds 10 kazoos to
the version 2 interpretation. If the author determines that the kazoos are
really necessary to the interpretation of either the reference to v1 can be
removed and the document rewritten to,&lt;/p&gt;
&lt;pre&gt;    &amp;lt;Party xmlns=&quot;...assembly-v2-uri...&quot;&amp;gt;
        &amp;lt;Balloon Color=&quot;Red&quot; /&amp;gt;
        &amp;lt;Balloon Color=&quot;Blue&quot; /&amp;gt;
        &amp;lt;Balloon Color=&quot;Green&quot; Shape=&quot;Dog&quot; /&amp;gt;
        &amp;lt;Favor Kind=&quot;Kazoo&quot; Quantity=&quot;10&quot; /&amp;gt;
    &amp;lt;/Party&amp;gt;&lt;/pre&gt;

&lt;p&gt;or the the ignorable attribute can just be removed which amounts to an
equivalent document given subsumption described in the previous entry.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/xaml/2006/02/12/A-Tour-of-XAML-V-Forward-compatibility</link>
                <guid>http://removingalldoubt.com/programming/xaml/2006/02/12/A-Tour-of-XAML-V-Forward-compatibility</guid>
                <pubDate>2006-02-12T18:26:00+00:00</pubDate>
        </item>

        <item>
                <title>Tour of XAML IV: Backward compatibility</title>
                <description>&lt;p&gt;XAML has support for both forward and backward compatibility of XAML
documents. To understand backwards compatibility in a more formal sense, think
of a XAML reader combined with some set of assemblies as the predicate that
defines a set. If the reader with the given set of assemblies accepts a XAML
document
without error it is part of the set. If it rejects a document, it is not part of
the set. You can think of the XAML reader and assemblies as defining a set, &lt;i&gt;{r(s,x)|x}&lt;/i&gt; where &lt;i&gt;x&lt;/i&gt;
is the XAML file and &lt;i&gt;r(s,x)&lt;/i&gt; is the XAML reader and &lt;i&gt;s&lt;/i&gt; is a set of
assemblies. You can then describe the relationship between&amp;nbsp; two sets of
assemblies with respect to XAML. The assemblies imply set schemas that the XAML
reader uses to interpret the XAML document. The schema implied by the WPF
assemblies states there is a Button element and a DockPanel element and the
Button element can be included in a DockPanel as one of it children, for
example. The a set of implied schemas&amp;nbsp; &lt;i&gt;s&lt;sub&gt;2&lt;/sub&gt;&lt;/i&gt; is backwards compatible
with &lt;i&gt;s&lt;sub&gt;1&lt;/sub&gt;&lt;/i&gt; if &lt;i&gt;{r(s&lt;sub&gt;1&lt;/sub&gt;, x)|x} ∩ {r(s&lt;sub&gt;2&lt;/sub&gt;,
x)|x} = {r(s&lt;sub&gt;1&lt;/sub&gt;, x)|x}&lt;/i&gt; or, in other words, &lt;i&gt;s&lt;sub&gt;2&lt;/sub&gt;&lt;/i&gt; is
backwards compatible with &lt;i&gt;s&lt;sub&gt;1&lt;/sub&gt;&lt;/i&gt; if the reader will accept same XAML documents
with &lt;i&gt;s&lt;/i&gt;&lt;sub&gt;&lt;i&gt;2&lt;/i&gt;&lt;/sub&gt; as it accepts with
&lt;i&gt;s&lt;sub&gt;1&lt;/sub&gt;&lt;/i&gt;.&lt;/p&gt;
&lt;p&gt;XAML really doesn&apos;t have to do much to claim it supports
backwards compatibility because backwards compatibility is really a relationship
between two sets of XAML schemas (and the implying assemblies) than it is between XAML documents. XAML,
however, simplifies establishing this relationship by allowing one schema to declare it subsumes another
schema. That means that,
given both &lt;i&gt;s&lt;sub&gt;1&lt;/sub&gt;&lt;/i&gt; and &lt;i&gt;s&lt;sub&gt;2&lt;/sub&gt;&lt;/i&gt;, XAML allows &lt;i&gt;s&lt;sub&gt;2&lt;/sub&gt;&lt;/i&gt; to declare that any reference elements of
&lt;i&gt;s&lt;sub&gt;1&lt;/sub&gt;&lt;/i&gt; should be considered a reference to elements of &lt;i&gt;s&lt;sub&gt;2&lt;/sub&gt;&lt;/i&gt;.&lt;/p&gt;
&lt;p&gt;To get more concrete, suppose you had an assembly that declared a single type,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;public class Balloon {
    Color _color;

    public Color Color { get { return _color; } set { _color = value; } }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A XAML reader will infer a schema that would allow you to write a XAML
document like,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;Balloon Color=&quot;Red&quot; xmlns=&quot;...assembly-v1-uri...&quot; /&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now, lets say, in version 2 of the assembly you want to add a shape to the balloon
class to support different balloon shapes. In the version 2 assembly you can modify
the class to,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;public class Balloon {
    Color _color;
    Shape _shape;

    public Color Color { get { return _color; } set { _color = value; } }
    public Shape Shape { get { return _shape; } set { _shape = value; } }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now we can write a XAML document that refers to the new assembly by writing,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;Balloon Color=&quot;Red&quot; Shape=&quot;Dog&quot; xmlns=&quot;...assembly-v2-uri...&quot;/&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We, however, still want to read all XAML documents that refer to the previous
assembly as well using the new assembly. We can do this by declaring, in the new
assembly, that this assembly is compatible with the old assembly with a
XmlnsCompatibleWith(&quot;...assembly-v2-uri...&quot;,&quot;...assembly-v1-uri...&quot;) attribute.
When the XAML reader sees that attribute, it will silently interpret all references to &quot;...assembly-v1-uri...&quot; as if they where referring
to &quot;...assembly-v2-uri...&quot;. A XAML reader, presented with the version 2
assembly, will treat,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;Balloon Color=&quot;Red&quot; xmlns=&quot;...assembly-v1-uri...&quot; /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;exactly as if it was written as,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;Balloon Color=&quot;Red&quot; xmlns=&quot;...assembly-v2-uri...&quot; /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This allows all XAML documents referring to version 1 assembly to be readable
when you only have version 2 of the assembly. In fact, assuming you follow CLR
guidelines for evolving an assembly, any XAML file that was legal using the
version 1 assembly will be legal using the version 2 assembly.&lt;/p&gt;

</description>
                <link>http://removingalldoubt.com/programming/xaml/2006/02/02/Tour-of-XAML-IV-Backward-compatibility</link>
                <guid>http://removingalldoubt.com/programming/xaml/2006/02/02/Tour-of-XAML-IV-Backward-compatibility</guid>
                <pubDate>2006-02-02T13:50:00+00:00</pubDate>
        </item>

        <item>
                <title>Cider's January CTP</title>
                <description>&lt;p&gt;Cider has been available since December in CTP form. This is really old news
and reasons why I haven&apos;t blogged about it till now you can see my previous post
about my and my machines independent
&lt;a href=&quot;http://www.removingalldoubt.com/PermaLink.aspx/45335adf-f01a-4af1-b211-610c26529d00&quot;&gt;
vacations&lt;/a&gt;. To get the January CTP see the Channel 9 page
&lt;a href=&quot;http://channel9.msdn.com/wiki/default.aspx/Cider.JanuaryCTPReleaseNotes&quot;&gt;
http://channel9.msdn.com/wiki/default.aspx/Cider.JanuaryCTPReleaseNotes&lt;/a&gt;. You
can keep track of our developments through our Channel 9 home page at
&lt;a href=&quot;http://channel9.msdn.com/wiki/default.aspx/Cider.HomePage&quot;&gt;
http://channel9.msdn.com/wiki/default.aspx/Cider.HomePage&lt;/a&gt;. I will try to be
better about timely blogging updates but given my past history maybe it would be
better if you checked the home page now and then...&lt;/p&gt;
&lt;p&gt;This month we are focusing on the grid design experience. Even though we
shipped it in December we really stole, uh, er, borrowed the grid design
experience from Sparkle (aka the Expression Interactive Designer) which is also
now available in CTP form available from
&lt;a href=&quot;http://www.microsoft.com/downloads/details.aspx?familyid=ed9f5fb2-4cfc-4d2c-9af8-580d644e3d1d&amp;amp;displaylang=en&quot;&gt;
http://www.microsoft.com/downloads/details.aspx?familyid=ed9f5fb2-4cfc-4d2c-9af8-580d644e3d1d&amp;amp;displaylang=en&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;The Cider CTPs are not beta quality. I say that to make sure you know what to
expect. We are delivering bits early and often to give all of you a chance to
influence our development by giving us early feedback on our design and the
general direction we are taking. I encourage all of you to take advantage of
this. We are listening and care a lot about what you think.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/cider/2006/01/27/Ciders-January-CTP</link>
                <guid>http://removingalldoubt.com/programming/cider/2006/01/27/Ciders-January-CTP</guid>
                <pubDate>2006-01-27T19:59:00+00:00</pubDate>
        </item>

        <item>
                <title>Excuses, excuses</title>
                <description>&lt;p&gt;OK, this is my first blog of the new year and it is already the 24th of
January, but I have some fairly good excuses. I decided that I would take
December off from blogging since I was taking vacation and December is generally
a busy month. When I returned from vacation all refreshed and everything, my
laptop (which is the machine I use at home, where I blog) decided to take its
own vacation. It decided that it was only going to start if it was docked in its
port replicator. Apparently, the sensor for the port replicator got flaky; the
laptop would sometimes think it was docked when I was home. Since the laptop
requires more power when attached to the port replicator, the laptop will not
start when the power adapter is disconnected. The battery alone is not powerful
enough to power the replicator. Also, the normal power adapter does not supply
enough power to run the replicator so it needs a different adapter and the
laptop detects when the wrong adapter is connected. All this has worked great
for almost two years but a flaky port replicator sensor means the laptop will
think it doesn&apos;t have the correct adapter connected and it refuses to start. All
this means I was computer-less at home during most of this month. A truly
painful experience.&lt;/p&gt;
&lt;p&gt;So, now I have a new laptop. I decided to go with a Toshiba Tecra Tablet PC.
I have always wanted a tablet and now I have one. I don&apos;t find myself using the
tablet capabilities much but I believe I will over time as I get used to it.
Switching laptops is a pain! The biggest pain used to be installing all the
software I use. Now, with the most computers coming Office preinstalled, there
is less I need to install. I had to install Visual Studio and RSS Bandit and
then I was pretty much done. The biggest pain was transferring My Documents
folder and remembering all my internet passwords again. This brings me back to
my excuse (embarrassing as it is), I forgot the password I used for my own blog!
Obviously, I figured it out, but it took me a bit. Now I am up and going on my
new laptop, I will try to get back on a regular blog pace.&lt;/p&gt;
&lt;p&gt;With this entry I am also starting a new category, Meta. I will use it to
discuss the blog itself, so don&apos;t expect much technical content in this
category.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/meta/2006/01/24/Excuses-excuses</link>
                <guid>http://removingalldoubt.com/meta/2006/01/24/Excuses-excuses</guid>
                <pubDate>2006-01-24T22:10:00+00:00</pubDate>
        </item>

        <item>
                <title>A Solution to a Puzzle</title>
                <description>&lt;p&gt;A few months back &lt;a href=&quot;http://www.winethirty.com/default.aspx&quot;&gt;Randy
Kimmerly&lt;/a&gt; posted a &lt;a href=&quot;http://www.winethirty.com/blog.aspx?id=146&quot;&gt;blog&lt;/a&gt;
about one the puzzles on my desk. &lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://www.winethirty.com/images/cube_puzzle.jpg&quot; width=&quot;570px&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Twisted in the correct way it will form a 3x3x3 cube. It is a snake cube
puzzle that goes by the commercial name of Cubra&lt;font face=&quot;Times New Roman&quot;&gt;©&lt;/font&gt;.
Randy got excited about the puzzle not for the puzzle itself but by the
challenge of writing a program that will produce the solution. He wrote a very
clever C# program to produce a solution. I thought the solution would be more
concise in Prolog so I volunteered to produce a Prolog version. I had to dust off my &lt;a href=&quot;http://en.wikipedia.org/wiki/Prolog&quot;&gt;
Prolog&lt;/a&gt; skills, I hadn&apos;t used Prolog since I used to support Turbo Prolog for
&lt;a href=&quot;http://www.borland.com&quot;&gt;Borland&lt;/a&gt;, so my skills are a bit rusty. Here is my solution,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;linkTurns([f, f, t, f, t, t, t, t, f, t, t, t, t, t, t, t,
   t, f, t, t, t, f, t, t, t, f]).

inrange(0). inrange(1). inrange(2).
cell(X, Y, Z) :- inrange(X), inrange(Y), inrange(Z).

move(right, cell(X, Y, Z), cell(X1, Y, Z)) :- X1 is X + 1, cell(X1, Y, Z).
move(left, cell(X, Y, Z), cell(X1, Y, Z)) :- X1 is X - 1, cell(X1, Y, Z).
move(down, cell(X, Y, Z), cell(X, Y1, Z)) :- Y1 is Y + 1, cell(X, Y1, Z).
move(up, cell(X, Y, Z), cell(X, Y1, Z)) :- Y1 is Y - 1, cell(X, Y1, Z).
move(in, cell(X, Y, Z), cell(X, Y, Z1)) :- Z1 is Z + 1, cell(X, Y, Z1).
move(out, cell(X, Y, Z), cell(X, Y, Z1)) :- Z1 is Z - 1, cell(X, Y, Z1).

legalTurn(left, X) :- member(X, [up, down, in, out]).
legalTurn(right, X) :- member(X, [up, down, in, out]).
legalTurn(up, X) :- member(X, [left, right, in, out]).
legalTurn(down, X) :- member(X, [left, right, in, out]).
legalTurn(in, X) :- member(X, [left, right, up, down]).
legalTurn(out, X) :- member(X, [left, right, up, down]).

free(X, Y) :- member(X, Y), !, fail.
free(_, _).

solution(Cells, Moves, [], Cells, Moves).
solution([LastCell|UsedCells], [LastMove|OldMoves],
   [t|Turns], ResultCells, ResultMoves) :-
   legalTurn(LastMove, NewMove), move(NewMove, LastCell, NewCell),
   free(NewCell, UsedCells),   solution([NewCell, LastCell|UsedCells],
   [NewMove, LastMove|OldMoves], Turns, ResultCells, ResultMoves).
solution([LastCell|UsedCells], [LastMove|OldMoves],
   [f|Turns], ResultCells, ResultMoves) :-
   move(LastMove, LastCell, NewCell), free(NewCell, UsedCells),
   solution([NewCell, LastCell|UsedCells], [LastMove, LastMove|OldMoves],
   Turns, ResultCells, ResultMoves).

solution(Cells, Moves) :- linkTurns(L), solution([cell(0, 0, 0)],
   [right], L, Cells, Moves).


print_solution([], []).
print_solution([Cell|Cells], [Move|Moves]) :-
   print_solution(Cells, Moves), write(Move), put(9), write(&apos;to &apos;),
   write(Cell), nl.

goal :- solution(Cells, Moves), print_solution(Cells, Moves).
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The clause &lt;span class=&quot;code&quot;&gt;linkTurns/1&lt;/span&gt; represents where the string
of blocks turns and where it doesn&apos;t. &lt;span class=&quot;code&quot;&gt;inrange/1&lt;/span&gt;
represents the valid ranges the coordinates of a block can have, 0, 1 or 2. This
is used to build &lt;span class=&quot;code&quot;&gt;cell/3&lt;/span&gt; which represents the locations
the blocks can occupy in a 3x3 result. &lt;span class=&quot;code&quot;&gt;move/3&lt;/span&gt;
calculates how to get from one block to another.&amp;nbsp; &lt;span class=&quot;code&quot;&gt;
legalTurn/2&lt;/span&gt; generates legal turns. &lt;span class=&quot;code&quot;&gt;free/2&lt;/span&gt; helps
decide if a cell is already occupied.&amp;nbsp; &lt;span class=&quot;code&quot;&gt;solution/5&lt;/span&gt;
calculates the solution; and &lt;span class=&quot;code&quot;&gt;solution/2&lt;/span&gt; is a
simplified wrapper around&lt;span class=&quot;code&quot;&gt; solution/5&lt;/span&gt;. &lt;span class=&quot;code&quot;&gt;
print_solution/2&lt;/span&gt; is a utility clause to help print the solution in a some
what readable form. Finally &lt;span class=&quot;code&quot;&gt;goal&lt;/span&gt; is a clause that
kicks off the whole process (a dead giveaway to Turbo Prolog roots).
&lt;/p&gt;
&lt;p&gt;This will only print one solution (because of the implied cut in using I/O) even though there are two possible. To see
both use,&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;?- solution(Cells, Moves).
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You will notice that both solutions are reflexive of each other so you could
say there is really only one solution.&lt;/p&gt;
&lt;p&gt;I found an interesting site
&lt;a href=&quot;http://www.geocities.com/jaapsch/puzzles/snakecube.htm&quot;&gt;here&lt;/a&gt; that
discusses these puzzles as well as gives a break down of how many of these kinds
of puzzle there can be.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/prolog/puzzles/2005/12/17/A-Solution-to-a-Puzzle</link>
                <guid>http://removingalldoubt.com/programming/prolog/puzzles/2005/12/17/A-Solution-to-a-Puzzle</guid>
                <pubDate>2005-12-17T14:29:00+00:00</pubDate>
        </item>

        <item>
                <title>The Expression Problem: Part IV - Layers</title>
                <description>&lt;p&gt;So far I have presented three different approaches to solving the expression
problem, procedural, pure object-oriented, and a visitor pattern. This time I
will present a technique I will refer to as layers. Like the visitor pattern, it is a modification of the
pure object-oriented approach that takes advantage of partial classes. The
solution begins very similarly to the other object-oriented approaches, I create an abstract
class to represent an abstraction of expressions,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;abstract partial class Expr { }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Notice the addition of the &lt;code&gt;partial&lt;/code&gt; modifier. I will
take advantage of that shortly. For now I will just flesh out the rest of the
hierarchy in a way that should, by now, be very familiar. I created a class to
represent literals,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;partial class Literal: Expr {
  protected double Value;

  public Literal(double value) { Value = value; }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Notice that, as with the pure object-oriented approach, I can keep the field
used to hold the value as protected. This is also true for the class to
represent an abstraction of binary operators.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;abstract partial class BinaryOperator: Expr {
  protected Expr Left;
  protected Expr Right;

  public BinaryOperator(Expr left, Expr right) {
    Left = left;
    Right = right;
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The actual binary operators are just descendants of &lt;code&gt;
BinaryOperator&lt;/code&gt; with no additional data.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;partial class Add: BinaryOperator {
  public Add(Expr left, Expr right) : base(left, right) { }
}

partial class Subtract: BinaryOperator {
  public Subtract(Expr left, Expr right) : base(left, right) { }
}

partial class Multiply: BinaryOperator {
  public Multiply(Expr left, Expr right) : base(left, right) { }
}

partial class Divide: BinaryOperator {
  public Divide(Expr left, Expr right) : base(left, right) { }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So far, this is nearly identical to the pure object-oriented approach. Where
it differs is in how new operations are added.&amp;nbsp; To add the
&lt;code&gt;Evaluate()&lt;/code&gt; method, instead of modifying each class to add the methods, I can take advantage of the fact I left them
as partial classes and create additional parts that contain the methods instead.
First I created a new part of the &lt;code&gt;Expr&lt;/code&gt; class to
introduce the virtual method &lt;code&gt;Evaluate()&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;abstract partial class Expr {
  public abstract double Evaluate();
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Compiling now will generate several errors indicating that the other classes
do not implement the &lt;code&gt;Evaluate()&lt;/code&gt; method.&amp;nbsp; This is
want I wanted. Using &lt;code&gt;abstract&lt;/code&gt; in the above class part lets the compiler help me
determine when I have completed the implementation of the evaluate operation.
The rest of the class parts for &lt;code&gt;Evaluate()&lt;/code&gt; looks like,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;partial class Literal {
  public override double Evaluate() {
    return Value;
  }
}

abstract partial class BinaryOperator {
  public sealed override double Evaluate() {
    return EvaluateOp(Left.Evaluate(), Right.Evaluate());
  }

  protected abstract double EvaluateOp(double left, double right);
}

partial class Add {
  protected override double EvaluateOp(double left, double right) {
    return left + right;
  }
}

partial class Subtract {
  protected override double EvaluateOp(double left, double right) {
    return left - right;
  }
}

partial class Multiply {
  protected override double EvaluateOp(double left, double right) {
    return left * right;
  }
}

partial class Divide {
  protected override double EvaluateOp(double left, double right) {
    return left / right;
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I call each of the collections of class parts a layer. You can think
of them much like a transparency teachers uses on overhead projectors. You can
build up a class by combining layers of functionality. I usually keep each layer
in its own file named for the layer. In this case, Evaluator.cs might be a good
choice.&lt;/p&gt;
&lt;p&gt;To demonstrate building up a class with through layers I
will create a printing layer.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;abstract partial class Expr {
  public abstract void Print();
}

partial class Literal {
  public override void Print() {
    Console.Write(Value);
  }
}

abstract partial class BinaryOperator {
  public sealed override void Print() {
    Left.Print();
    PrintOp();
    Right.Print();
  }

  protected abstract void PrintOp();
}

partial class Add {
  protected override void PrintOp() {
    Console.Write(&quot; + &quot;);
  }
}

partial class Subtract {
  protected override void PrintOp() {
    Console.Write(&quot; - &quot;);
  }
}

partial class Multiply {
  protected override void PrintOp() {
    Console.Write(&quot; * &quot;);
  }
}

partial class Divide {
  protected override void PrintOp() {
    Console.Write(&quot; / &quot;);
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As in the other approaches, I will demonstrate how to add support for the
Power expression form. I have two choices, I can add it just as we did in the pure object-oriented approach,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class Power: BinaryExpr {
  public Power(Expr left, Expr right) : base(left, right) { }

  protected override double EvaluateOp(double left, double right) {
    return Math.Pow(left, right);
  }

  protected override void PrintOp() {
    Console.Write(&quot; ^ &quot;);
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;or I could divide it into multiple parts that can be co-located with the other
similar parts. The data part would look like,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;partial class Power: BinaryExpr {
  public Power(Expr left, Expr right) : base(left, right) { }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The part for the expression layer would look like,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;partial class Power: BinaryExpr {
  protected override double EvaluateOp(double left, double right) {
    return Math.Pow(left, right);
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;An the printing layer part would look like,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;partial class Power: BinaryExpr {
  protected override void PrintOp() {
    Console.Write(&quot; ^ &quot;);
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It is this flexibility that makes layers so appealing. You can decide, case
by case, which is the right way to modify the hierarchy.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/2005/12/03/The-Expression-Problem-Part-IV-Layers</link>
                <guid>http://removingalldoubt.com/programming/2005/12/03/The-Expression-Problem-Part-IV-Layers</guid>
                <pubDate>2005-12-03T17:48:00+00:00</pubDate>
        </item>

        <item>
                <title>The Expression Problem: Part III - Visitor Pattern</title>
                <description>&lt;div class=&quot;text&quot;&gt;&lt;p&gt;So far we have looked at two ways to represent the following expression
forms,&lt;/p&gt;
&lt;blockquote&gt;
&lt;table border=&quot;0&quot; id=&quot;table1&quot;&gt;
&lt;tbody&gt;&lt;tr&gt;
&lt;td width=&quot;100&quot;&gt;&lt;i&gt;number&lt;/i&gt;&lt;/td&gt;
&lt;td&gt;Literal&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;100&quot;&gt;&lt;i&gt;expr&lt;/i&gt; + &lt;i&gt;expr&lt;/i&gt;&lt;/td&gt;
&lt;td&gt;Add&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;75&quot;&gt;&lt;i&gt;expr&lt;/i&gt; - &lt;i&gt;expr&lt;/i&gt;&lt;/td&gt;
&lt;td&gt;Subtract&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;75&quot;&gt;&lt;i&gt;expr&lt;/i&gt; * &lt;i&gt;expr&lt;/i&gt;&lt;/td&gt;
&lt;td&gt;Multiply&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;75&quot;&gt;&lt;i&gt;expr&lt;/i&gt; / &lt;i&gt;expr&lt;/i&gt;&lt;/td&gt;
&lt;td&gt;Divide&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/blockquote&gt;
&lt;p&gt;The first was procedural and the second object-oriented. Using the
procedural approach, it was easier to add operations than data types. Using an
object-oriented approach, the opposite was true. What I will demonstrate now is
a technique that balances some of the advantages and disadvantages of both into
a hybrid approach called the
&lt;a href=&quot;http://en.wikipedia.org/wiki/Visitor_pattern&quot;&gt;Visitor Pattern&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;There are several ways to apply the visitor problem to expressions, each with
their own unique set of advantages and disadvantages, but I will only cover one
of these approaches.&lt;/p&gt;
&lt;p&gt;The visitor pattern is a modification of the the object-oriented approach
that allows an operation to be separated from the class hierarchy into
its own class. To demonstrate how a visitor pattern can be applied, I will first
created a visitor interface that will be implemented by all visitors. It looks like,&lt;/p&gt;

&lt;code&gt;&lt;pre&gt;
public interface IExprVisitor&amp;lt;R&amp;gt; {
  R Visit(Literal literal);
  R Visit(Add add);
  R Visit(Subtract subtract);
  R Visit(Multiply multiply);
  R Visit(Divide divide);
}
&lt;/pre&gt;&lt;/code&gt;

&lt;sup&gt;&lt;a name=&quot;ref-note1&quot; href=&quot;#note1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;

&lt;p&gt;Here I use a generic type to leave the type of the return result open. The
expression evaluator will use &lt;code&gt;double&lt;/code&gt; but &lt;code&gt;double&lt;/code&gt; isn&apos;t an appropriate return
result for printing, for example. Using a type parameter allows different visitors
to have their own result types.&lt;/p&gt;
&lt;p&gt;Now I will create the class hierarchy that will be visited. I will first
create an
abstract class to represent the expressions, just as I did in the previous object
oriented example.&lt;/p&gt;

&lt;code&gt;&lt;pre&gt;
public abstract class Expr {
  public abstract R Accept&amp;lt;R&amp;gt;(IExprVisitor&amp;lt;R&amp;gt; visitor);
}
&lt;/pre&gt;&lt;/code&gt;

&lt;p&gt;The method &lt;code&gt;Accept()&lt;/code&gt; is key to the visitor pattern.
It accepts the visitor and, in turn, calls the correct &lt;code&gt;
Visit()&lt;/code&gt;
method on the visitor, based on the type of the instance being visited. To do this, each descendant overrides
the &lt;code&gt;Accept()&lt;/code&gt; method and calls the correct method. To
show how this is done I will implement the Literal expression form. &lt;/p&gt;

&lt;code&gt;&lt;pre&gt;
public class Literal: Expr {
  public double Value;

  public Literal(double value) { Value = value; }

  public override R Accept&amp;lt;R&amp;gt;(IExprVisitor&amp;lt;R&amp;gt; visitor) {
    return visitor.Visit(this);
  }
}
&lt;/pre&gt;&lt;/code&gt;

&lt;p&gt;The above &lt;code&gt;Literal&lt;/code&gt; class is very similar to the
original pure object oriented version with the addition of the &lt;code&gt;
Accept()&lt;/code&gt; method. The implementation of the method is trivial, simply call
&lt;code&gt;visitor.Visit(this)&lt;/code&gt;. The compiler will determine that
&lt;code&gt;IExprVisitor&amp;lt;R&amp;gt;.Visitor(Literal literal)&lt;/code&gt; is the method
to bind to based on the type of the &lt;code&gt;this&lt;/code&gt; parameter.&lt;/p&gt;
&lt;p&gt;Next, as before, I introduce a base class to represent an abstraction of a
binary operator.&lt;/p&gt;

&lt;code&gt;&lt;pre&gt;
public abstract class BinaryOperator: Expr {
  public Expr Right;
  public Expr Left;

  public BinaryOperator(Expr left, Expr right) {
    Right = right;
    Left = left;
  }
}
&lt;/pre&gt;&lt;/code&gt;

&lt;p&gt;Notice that I do not yet implement the &lt;code&gt;Accept()&lt;/code&gt; method.
I could have, after adding &lt;code&gt;R Visit(BinaryOperator binop)&lt;/code&gt;
to the &lt;code&gt;IExprVisitor&lt;/code&gt; interface, but I didn&apos;t need it
for the visitors I wanted to create. I have found that it is usually best to only have
the visitors visit the concrete classes and avoid the abstract. A visitor can
always simulate visiting a abstract base class by calling a common routine in
each of the concrete class visitor methods (you will see this technique below in
the &lt;code&gt;Printer&lt;/code&gt; visitor). This leaves the visitor less
cluttered when the abstract class doesn&apos;t need to be visited separately. In other
words, my &lt;code&gt;Accept()&lt;/code&gt; methods only calls one &lt;code&gt;
Visit()&lt;/code&gt; method for each instance, and that for the most derived type. This has the
additional advantage of allowing the &lt;code&gt;Visit()&lt;/code&gt; method to
have a meaningful result, which wouldn&apos;t be possible if the &lt;code&gt;
Accept()&lt;/code&gt; method called more than one &lt;code&gt;Visit()&lt;/code&gt;
methods.&lt;/p&gt;
&lt;p&gt;Now that I have a base class for binary operators, I can now implement the
binary operator expression forms.&lt;/p&gt;

&lt;code&gt;&lt;pre&gt;
public class Add: BinaryOperator {
  public Add(Expr left, Expr right) : base(left, right) { }

  public override R Accept&amp;lt;R&amp;gt;(IExprVisitor&amp;lt;R&amp;gt; visitor) {
    return visitor.Visit(this);
  }
}

public class Subtract: BinaryOperator {
  public Subtract(Expr left, Expr right) : base(left, right) { }

  public override R Accept&amp;lt;R&amp;gt;(IExprVisitor&amp;lt;R&amp;gt; visitor) {
    return visitor.Visit(this);
  }
}

public class Multiply: BinaryOperator {
  public Multiply(Expr left, Expr right) : base(left, right) { }

  public override R Accept&amp;lt;R&amp;gt;(IExprVisitor&amp;lt;R&amp;gt; visitor) {
    return visitor.Visit(this);
  }
}

public class Divide: BinaryOperator {
  public Divide(Expr left, Expr right) : base(left, right) { }

  public override R Accept&amp;lt;R&amp;gt;(IExprVisitor&amp;lt;R&amp;gt; visitor) {
    return visitor.Visit(this);
  }
}
&lt;/pre&gt;&lt;/code&gt;

&lt;p&gt;The implementation of these should come as no surprise. They each
override the &lt;code&gt;Accept()&lt;/code&gt; method and call the visitor
method associated with their type.&lt;/p&gt;
&lt;p&gt;Now that we have the class hierarchy in place, I will now implement the
expression evaluator as a visitor.&lt;/p&gt;

&lt;code&gt;&lt;pre&gt;
public class Evaluator: IExprVisitor&amp;lt;double&amp;gt; {
  public Evaluator() { }

  public double Evaluate(Expr expr) {
    return expr.Accept(this);
  }

  double IExprVisitor&amp;lt;double&amp;gt;.Visit(Literal literal) {
    return literal.Value;
  }

  double IExprVisitor&amp;lt;double&amp;gt;.Visit(Add add) {
    return Evaluate(add.Left) + Evaluate(add.Right);
  }

  double IExprVisitor&amp;lt;double&amp;gt;.Visit(Subtract subtract) {
    return Evaluate(subtract.Left) - Evaluate(subtract.Right);
  }

  double IExprVisitor&amp;lt;double&amp;gt;.Visit(Multiply multiply) {
    return Evaluate(multiply.Left) * Evaluate(multiply.Right);
  }

  double IExprVisitor&amp;lt;double&amp;gt;.Visit(Divide divide) {
    return Evaluate(divide.Left) / Evaluate(divide.Right);
  }
}
&lt;/pre&gt;&lt;/code&gt;

&lt;p&gt;The expression evaluator can be used by creating a new visitor and calling
the &lt;code&gt;Evaluate()&lt;/code&gt; method of the evaluator, passing the root of the expression tree.
For example,&lt;/p&gt;

&lt;code&gt;&lt;pre&gt;
Expr expr = new Add(new Multiply(new Literal(10), new Literal(4)),
  new Literal(2));

Evalutor evaluator = new Evaluator();
double result = evaluator.Evaluate(expr);

Console.WriteLine(&quot;The answer is {0}&quot;, result);
&lt;/pre&gt;&lt;/code&gt;

&lt;p&gt;The visitor pattern makes it easy to add new operations. To demonstrate this,
as before, I will add a print operation. This looks very much like the
expression evaluator above.&lt;/p&gt;

&lt;code&gt;&lt;pre&gt;
public class Printer: IExprVisitor&amp;lt;object&amp;gt; {
  public Printer() { }

  public void Print(Expr expr) {
    expr.Accept(this);
  }

  object IExprVisitor&amp;lt;object&amp;gt;.Visit(Literal literal) {
    Console.Write(literal.Value);
    return null;
  }

  void PrintBinaryOperator(BinaryOperator binOp, char opChar) {
    Print(binOp.Left);
    Console.Write(&quot; {0} &quot;, opChar);
    Print(binOp.Right);
  }

  object IExprVisitor&amp;lt;object&amp;gt;.Visit(Add add) {
    PrintBinaryOperator(add, &apos;+&apos;);
    return null;
  }

  object IExprVisitor&amp;lt;object&amp;gt;.Visit(Subtract subtract) {
    PrintBinaryOperator(subtract, &apos;-&apos;);
    return null;
  }

  object IExprVisitor&amp;lt;object&amp;gt;.Visit(Multiply multiply) {
    PrintBinaryOperator(multiply, &apos;*&apos;);
    return null;
  }

  object IExprVisitor&amp;lt;object&amp;gt;.Visit(Divide divide) {
    PrintBinaryOperator(divide, &apos;/&apos;);
    return null;
  }
}
&lt;/pre&gt;&lt;/code&gt;

&lt;p&gt;As this shows, using a parameterized type for the visitor interface is awkward
when you don&apos;t have anything to return. What I would want to do is implement
&lt;code&gt;IExprVisitor&amp;lt;void&amp;gt;&lt;/code&gt; but &lt;code&gt;void&lt;/code&gt;
is not allowed as a type parameter so I use &lt;code&gt;object&lt;/code&gt;
instead. This means I have to return something, so I always return
&lt;code&gt;null&lt;/code&gt;. This should be seen as awkwardness introduced by
my choice of interface styles, not of the visitor pattern itself.&lt;/p&gt;
&lt;p&gt;The visitor pattern shares some of the advantages of the procedural approach,
in that it is easy to add new operations and new operations can be added
dynamically, but we lose the natural ability to add new data types that is
normally associated with an object-oriented approach as well as lose
representation encapsulation since the instance fields need to be visible to the
visitors. We kept the natural size optimization and the completeness
verification by the compiler, however. To demonstrate the difficult of adding a
new type, I will again add Power. The first step is to add the new
&lt;code&gt;Power&lt;/code&gt; type.&lt;/p&gt;

&lt;code&gt;&lt;pre&gt;
public class Power: BinaryOperator {
  public Power(Expr left, Expr right) : base(left, right) { }

  public override R Accept&amp;lt;R&amp;gt;(IExprVisitor&amp;lt;R&amp;gt; visitor) {
    return visitor.Visit(this);
  }
}
&lt;/pre&gt;&lt;/code&gt;

&lt;p&gt;Now we need to add a method to &lt;code&gt;IExprVisitor&amp;lt;R&amp;gt;&lt;/code&gt;
for the &lt;code&gt;Power&lt;/code&gt; type that each visitor will need to implement. It looks like,&lt;/p&gt;

&lt;code&gt;&lt;pre&gt;
R Visit(Power power);
&lt;/pre&gt;&lt;/code&gt;

&lt;p&gt;Next I need to add a method for in each visitor to handle the new class. For
&lt;code&gt;Evaluator&lt;/code&gt; it is,&lt;/p&gt;

&lt;code&gt;&lt;pre&gt;
double IExprVisitor&amp;lt;double&amp;gt;.Visit(Power power) {
  return Math.Pow(Evaluate(power.Left), Evaluate(power.Right));
}
&lt;/pre&gt;&lt;/code&gt;

&lt;p&gt;And and for the &lt;code&gt;Printer&lt;/code&gt; class it looks like.&lt;/p&gt;

&lt;code&gt;&lt;pre&gt;
object IExprVisitor&amp;lt;object&amp;gt;.Visit(Power power) {
  PrintBinaryOperator(power, &apos;^&apos;);
  return null;
}
&lt;/pre&gt;&lt;/code&gt;

&lt;p&gt;This is just one way to apply the visitor pattern to the expression problem.
Other ways have their own strengths and weaknesses by trading off different
advantages and disadvantages of the the procedural and object-oriented styles.
Some visitor pattern applications have their own unique advantages, such as being able
to mesh functionality by executing several visitors concurrently such as
evaluating and printing.&lt;/p&gt;
&lt;p&gt;Next time we will investigate a different modified object-oriented approach
that is sometimes dubbed groups but I will call layers.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;&lt;a name=&quot;note1&quot; href=&quot;#ref-note1&quot;&gt;1&lt;/a&gt; - This application of the visitor
pattern is patterned after one from &lt;a href=&quot;http://research.microsoft.com/~akenn/generics/gadtoop.pdf&quot;&gt;Generalized Algebraic Data Types and Object-Oriented Programming&lt;/a&gt;.
&lt;a href=&quot;#ref-note1&quot;&gt;&amp;lt;&amp;lt; back &amp;lt;&amp;lt;&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
</description>
                <link>http://removingalldoubt.com/programming/2005/11/25/The-Expression-Problem-Part-III-Visitor-Pattern</link>
                <guid>http://removingalldoubt.com/programming/2005/11/25/The-Expression-Problem-Part-III-Visitor-Pattern</guid>
                <pubDate>2005-11-25T16:16:00+00:00</pubDate>
        </item>

        <item>
                <title>The Expression Problem: Part II - Object-oriented</title>
                <description>&lt;p&gt;Last time we took a procedural approach to solve the &lt;i&gt;expression problem&lt;/i&gt;.
This time I will demonstrate an object-oriented approach. As a
reminder, we want to model the following forms of expressions,&lt;/p&gt;
&lt;blockquote&gt;
&lt;table border=&quot;0&quot; id=&quot;table1&quot;&gt;
&lt;tbody&gt;&lt;tr&gt;
&lt;td width=&quot;100&quot;&gt;&lt;i&gt;number&lt;/i&gt;&lt;/td&gt;
&lt;td&gt;Literal&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;100&quot;&gt;&lt;i&gt;expr&lt;/i&gt; + &lt;i&gt;expr&lt;/i&gt;&lt;/td&gt;
&lt;td&gt;Add&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;75&quot;&gt;&lt;i&gt;expr&lt;/i&gt; - &lt;i&gt;expr&lt;/i&gt;&lt;/td&gt;
&lt;td&gt;Subtract&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;75&quot;&gt;&lt;i&gt;expr&lt;/i&gt; * &lt;i&gt;expr&lt;/i&gt;&lt;/td&gt;
&lt;td&gt;Multiply&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;75&quot;&gt;&lt;i&gt;expr&lt;/i&gt; / &lt;i&gt;expr&lt;/i&gt;&lt;/td&gt;
&lt;td&gt;Divide&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/blockquote&gt;
&lt;p&gt;The most natural thing to do in an object-oriented language is to make each of the above
expression forms its own class. First, I will introduce a base class to provide an expression abstraction.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;abstract class Expr {
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now I will now create a sub-class for each expression.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class Literal: Expr {
  protected double Value;

  public Literal(double value) {
    Value = value;
  }
}

abstract class BinaryExpr: Expr {
  protected Expr Left;
  protected Expr Right;

  public BinaryExpr(Expr left, Expr right) {
    Left = left;
    Right = right;
  }
}

class Add: BinaryExpr {
  public Add(Expr left, Expr right) : base(left, right) { }
}

class Subtract: BinaryExpr {
  public Subtract(Expr left, Expr right) : base(left, right) { }
}

class Multiply: BinaryExpr {
  public Multiply(Expr left, Expr right) : base(left, right) { }
}

class Divide: BinaryExpr {
  public Divide(Expr left, Expr right) : base(left, right) { }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I created a base class for the binary expressions which I will take
  advantage of below. &lt;/p&gt;
&lt;p&gt;To evaluate the expression I will add an abstract virtual method,
  &lt;code&gt;Evaluate()&lt;/code&gt;, to &lt;code&gt;Expr&lt;/code&gt;. &lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;abstract class Expr {
    public abstract double Evaluate();
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then I will override the &lt;code&gt;Evaluate()&lt;/code&gt; method for
each of the above classes. Evaluating a literal is simply returning the value of
the literal so I modified the &lt;code&gt;Literal&lt;/code&gt; class to,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class Literal: Expr {
  protected double Value;

  public Literal(double value) {
    Value = value;
  }

  public override double Evaluate() {
    return Value;
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;All binary expressions need to evaluate their left-hand expression and
right-hand expression and then perform their operation on the results. To represent this, I
overrode &lt;code&gt;Evaluate()&lt;/code&gt; to evaluate both &lt;code&gt;Left&lt;/code&gt; and &lt;code&gt;Right&lt;/code&gt;
and then call a newly introduced &lt;code&gt;EvaluateOp()&lt;/code&gt;
method. I sealed the &lt;code&gt;Evaluate()&lt;/code&gt; method because I want
descendants to override &lt;code&gt;EvaluateOp()&lt;/code&gt; not
&lt;code&gt;Evaluate()&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;abstract class BinaryExpr: Expr {
  protected Expr Left;
  protected Expr Right;

  public BinaryExpr(Expr left, Expr right) {
    Left = left;
    Right = right;
  }

  public sealed override double Evaluate() {
    return EvaluateOp(Left.Evaluate(), Right.Evaluate());
  }

  protected abstract double EvaluateOp(double left, double right);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now I can implemented the concrete descendents of &lt;code&gt;BinaryExpr&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class Add: BinaryExpr {
  public Add(Expr left, Expr right) : base(left, right) { }

  protected override double EvaluateOp(double left, double right) {
    return left + right;
  }
}

class Subtract: BinaryExpr {
  public Subtract(Expr left, Expr right) : base(left, right) { }

  protected override double EvaluateOp(double left, double right) {
    return left - right;
  }
}

class Multiply: BinaryExpr {
  public Multiply(Expr left, Expr right) : base(left, right) { }

  protected override double EvaluateOp(double left, double right) {
    return left * right;
  }
}

class Divide: BinaryExpr {
  public Divide(Expr left, Expr right) : base(left, right) { }

  protected override double EvaluateOp(double left, double right) {
    return left / right;
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The advantages an object-oriented approach are,&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;New data types can be added without affecting any of the other data
types. &lt;/li&gt;
&lt;li&gt;Each data type is encapsulated, only the data type needs to have access
the instance variables.&lt;/li&gt;
&lt;li&gt;All the operations affecting the instance variables are in one place,
the methods of instance variable&apos;s class.&lt;/li&gt;
&lt;li&gt;New operations can be added as abstract methods. The compiler will then
generate an error if an operation is not implemented for one of the leaf
classes.&lt;/li&gt;
&lt;li&gt;Efficient storage is natural. Adding field to one of the expression
types do not affect the others.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;To demonstrate how easy it is to add a new data type I will again add support
for Power.&lt;/p&gt;
&lt;blockquote&gt;
&lt;table border=&quot;0&quot; id=&quot;table2&quot;&gt;
&lt;tbody&gt;&lt;tr&gt;
&lt;td width=&quot;100&quot;&gt;&lt;i&gt;expr&lt;/i&gt; ^ &lt;i&gt;expr&lt;/i&gt;&lt;/td&gt;
&lt;td&gt;Power&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/blockquote&gt;
&lt;p&gt; To do this I will add a class to represent the Power expression form.
This looks like,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class Power: BinaryExpr {
  public Power(Expr left, Expr right) : base(left, right) { }

  protected override double EvaluateOp(double left, double right) {
    return Math.Pow(left, right);
  }
  }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Note that the &lt;code&gt;Power&lt;/code&gt; data type can be added without
modifying the other classes. &lt;/p&gt;
&lt;p&gt;The disadvantages to an object-oriented approach are,&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;It is difficult to add new operations because adding an operation
requires modifying all the classes.&lt;/li&gt;
&lt;li&gt;The logic for each operation is spread over multiple classes and often
multiple files. This makes it cumbersome to get a complete picture what the
operation does.&lt;/li&gt;
&lt;li&gt;It is very difficult to dynamically add operations and can&apos;t be done
without careful planning.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;To demonstrate some of the difficulties of adding a new operation, I will add
the Print operation. First I will modify the base &lt;code&gt;Expr&lt;/code&gt;
class to add an abstract &lt;code&gt;Print()&lt;/code&gt; method.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;abstract class Expr {
  public abstract double Evaluate();
  public abstract void Print();
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Now I will override this method in each class similar to the way I did for
the &lt;code&gt;Evaluate()&lt;/code&gt; method.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class Literal: Expr {
  protected double Value;

  public Literal(double value) {
    Value = value;
  }

  public override double Evaluate() {
    return Value;
  }

  public override void Print() {
    Console.Write(Value);
  }
}

abstract class BinaryExpr: Expr {
  protected Expr Left;
  protected Expr Right;

  public BinaryExpr(Expr left, Expr right) {
    Left = left;
    Right = right;
  }

  public sealed override double Evaluate() {
    return EvaluateOp(Left.Evaluate(), Right.Evaluate());
  }

  protected abstract double EvaluateOp(double left, double right);

  public sealed override void Print() {
    Left.Print();
    PrintOp();
    Right.Print();
  }

  protected abstract void PrintOp();
}

class Add: BinaryExpr {
  public Add(Expr left, Expr right) : base(left, right) { }

  protected override double EvaluateOp(double left, double right) {
    return left + right;
  }

  protected override void PrintOp() {
    Console.Write(&quot; + &quot;);
  }
}

class Subtract: BinaryExpr {
  public Subtract(Expr left, Expr right) : base(left, right) { }

  protected override double EvaluateOp(double left, double right) {
    return left - right;
  }

  protected override void PrintOp() {
    Console.Write(&quot; - &quot;);
  }
}

class Multiply: BinaryExpr {
  public Multiply(Expr left, Expr right) : base(left, right) { }

  protected override double EvaluateOp(double left, double right) {
    return left * right;
  }

  protected override void PrintOp() {
    Console.Write(&quot; * &quot;);
  }
}

class Divide: BinaryExpr {
  public Divide(Expr left, Expr right) : base(left, right) { }

  protected override double EvaluateOp(double left, double right) {
    return left / right;
  }

  protected override void PrintOp() {
    Console.Write(&quot; / &quot;);
  }
}

class Power: BinaryExpr {
  public Power(Expr left, Expr right) : base(left, right) { }

  protected override double EvaluateOp(double left, double right) {
    return Math.Pow(left, right);
  }

  protected override void PrintOp() {
    Console.Write(&quot; ^ &quot;);
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As you can see, each of the classes needed to be modified to implement
&lt;code&gt;Print()&lt;/code&gt;. You can also note that the compiler
complained until each of the classes implemented the &lt;code&gt;Print()&lt;/code&gt;
operation. This is because we used an abstract method in the &lt;code&gt;
Expr&lt;/code&gt; class.&lt;/p&gt;
&lt;p&gt;If you compare the procedural approach vs. the object-oriented approach you
will notice that they are pretty much opposites. It is easy to add operations in
the procedural approach but difficult using an object-oriented approach. Adding
data types is difficult using in a procedural approach, but easy in an
object-oriented approach. Data needs to be public in procedural, but can be
private in object oriented. When deciding which approach to use you need to try
an predict what will occur more often, adding new data types or adding new
operations. What I will present next are some compromise solutions that balance
the advantages and disadvantages.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/2005/11/19/The-Expression-Problem-Part-II-Object-oriented</link>
                <guid>http://removingalldoubt.com/programming/2005/11/19/The-Expression-Problem-Part-II-Object-oriented</guid>
                <pubDate>2005-11-19T12:49:00+00:00</pubDate>
        </item>

        <item>
                <title>The Expression Problem: Part I - Procedural</title>
                <description>&lt;div class=&quot;text&quot;&gt;&lt;p&gt;Expressions are difficult to represent because they form a matrix of
operations and data types. Languages make it either easy to add new operations
but difficult to add data types or easy to add new data types but hard to add
new operations. This problem isn&apos;t limited to expressions, of course, but it is
a very clear and familiar example so I will use it to characterize the more
general problem. I will
present four ways to approach the problem, each with their particular strengths
and weaknesses. For my examples
I representing a simple expression language comprising of the following
expression forms,&lt;/p&gt;
&lt;blockquote&gt;
&lt;table border=&quot;0&quot; id=&quot;table1&quot;&gt;
&lt;tbody&gt;&lt;tr&gt;
&lt;td width=&quot;100&quot;&gt;&lt;i&gt;number&lt;/i&gt;&lt;/td&gt;
&lt;td&gt;Literal&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;100&quot;&gt;&lt;i&gt;expr&lt;/i&gt; + &lt;i&gt;expr&lt;/i&gt;&lt;/td&gt;
&lt;td&gt;Add&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;75&quot;&gt;&lt;i&gt;expr&lt;/i&gt; - &lt;i&gt;expr&lt;/i&gt;&lt;/td&gt;
&lt;td&gt;Subtract&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;75&quot;&gt;&lt;i&gt;expr&lt;/i&gt; * &lt;i&gt;expr&lt;/i&gt;&lt;/td&gt;
&lt;td&gt;Multiply&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;75&quot;&gt;&lt;i&gt;expr&lt;/i&gt; / &lt;i&gt;expr&lt;/i&gt;&lt;/td&gt;
&lt;td&gt;Divide&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/blockquote&gt;
&lt;p&gt;Some operations I want to perform on these expressions include
evaluating expressions and printing expressions. The operations and the expressions form a matrix
of functionality, expression kind by operation,&lt;/p&gt;
&lt;blockquote&gt;
&lt;table border=&quot;1&quot; id=&quot;table3&quot; style=&quot;border-collapse: collapse&quot; width=&quot;321&quot;&gt;
&lt;tbody&gt;&lt;tr&gt;
&lt;td width=&quot;72&quot;&gt;&amp;nbsp;&lt;/td&gt;
&lt;td&gt;Evaluate&lt;/td&gt;
&lt;td width=&quot;102&quot;&gt;Print&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;72&quot;&gt;Literal&lt;/td&gt;
&lt;td&gt;&lt;i&gt;evaluate literal&lt;/i&gt;&lt;/td&gt;
&lt;td width=&quot;102&quot;&gt;&lt;i&gt;print literal&lt;/i&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;72&quot;&gt;Add&lt;/td&gt;
&lt;td&gt;&lt;i&gt;evaluate add&lt;/i&gt;&lt;/td&gt;
&lt;td width=&quot;102&quot;&gt;&lt;i&gt;print add&lt;/i&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;72&quot;&gt;Subtract&lt;/td&gt;
&lt;td&gt;&lt;i&gt;evaluate subtract&lt;/i&gt;&lt;/td&gt;
&lt;td width=&quot;102&quot;&gt;&lt;i&gt;print subtract&lt;/i&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;72&quot;&gt;Multiply&lt;/td&gt;
&lt;td&gt;&lt;i&gt;evaluate multiply&lt;/i&gt;&lt;/td&gt;
&lt;td width=&quot;102&quot;&gt;&lt;i&gt;print multiply&lt;/i&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;72&quot;&gt;Divide&lt;/td&gt;
&lt;td&gt;&lt;i&gt;evaluate divide&lt;/i&gt;&lt;/td&gt;
&lt;td width=&quot;102&quot;&gt;&lt;i&gt;print divide&lt;/i&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/blockquote&gt;
&lt;p&gt;Languages either make it easier to add new columns,
that is operations, or add new rows, that is new expression kinds. Procedural
and functional languages typically make it easy to add new operations.
Object-oriented languages typically make it easier to add new expression kinds
represented as new data types. &lt;/p&gt;
&lt;p&gt;The first approach I will demonstrate is procedural. I define a data type,
&lt;code&gt;Expr&lt;/code&gt;, that will hold the operator and the operands.&lt;/p&gt;

&lt;code&gt;&lt;pre&gt;
enum Oper { Add, Subtract, Multiply, Divide, Literal }

class Expr {
  public Oper Oper;
  public Expr Left;
  public Expr Right;
  public double Value;

  public Expr(Oper oper, Expr left, Expr right) { ... }
  public Expr(double value) { ... }
}
&lt;/pre&gt;&lt;/code&gt;&lt;sup&gt;&lt;a href=&quot;#note1&quot; name=&quot;ref-note1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;

&lt;code&gt;&lt;pre&gt;
static double Evaluate(Expr e) {
  switch (e.Oper) {
    case Oper.Add: return Evaluate(e.Left) + Evaluate(e.Right);
    case Oper.Subtract: return Evaluate(e.Left) - Evaluate(e.Right);
    case Oper.Multiply: return Evaluate(e.Left) * Evaluate(e.Right);
    case Oper.Divide: return Evaluate(e.Left) / Evaluate(e.Right);
    case Oper.Literal: return e.Value;
    default: Debug.Fail(&quot;Unknown operator&quot;); return 0;
  }
}
&lt;/pre&gt;&lt;/code&gt;

    &lt;p&gt;The advantages to a procedural approach are,&lt;/p&gt;
    &lt;ol&gt;
    &lt;li&gt;It is very simple to add additional operations (such as
      &lt;code&gt;Evaluate()&lt;/code&gt;).&lt;/li&gt;
      &lt;li&gt;All the logic for an operation is centralized in one place, and often in a single
      routine.&lt;/li&gt;
      &lt;li&gt;New operations can be added without having the modify existing
      operations.&lt;/li&gt;
      &lt;li&gt;New operations can be also be added dynamically without affecting or
      needing to recompile existing operations.&lt;/li&gt;
      &lt;/ol&gt;
      &lt;p&gt;To demonstrate some of the advantages of using a procedural approach we will
      ```

&lt;code&gt;&lt;pre&gt;
static void Print(Expr e) {
  switch (e.Oper) {
    case Oper.Add: Print(e.Left); Console.Write(&quot; + &quot;); Print(e.Right); break;
    case Oper.Subtract: Print(e.Left); Console.Write(&quot; - &quot;); Print(e.Right); break;
    case Oper.Multiply: Print(e.Left); Console.Write(&quot; * &quot;); Print(e.Right); break;
    case Oper.Divide: Print(e.Left); Console.Write(&quot; / &quot;); Print(e.Right); break;
    case Oper.Literal: Console.Write(e.Value); break;
    default: Debug.Fail(&quot;Unknown operator&quot;); break;
  }
}
&lt;/pre&gt;&lt;/code&gt;&lt;sup&gt;&lt;a href=&quot;#note2&quot; name=&quot;ref-note2&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;

        &lt;p&gt;Adding the &lt;code&gt;Print&lt;/code&gt; operation on the &lt;code&gt;Expr&lt;/code&gt; type doesn&apos;t
        require any modifications to &lt;code&gt;Expr&lt;/code&gt; or
        &lt;code&gt;Evaluate()&lt;/code&gt;. &lt;/p&gt;
        &lt;p&gt;Some disadvantages to a procedural approach are,&lt;/p&gt;
        &lt;ol&gt;
        &lt;li&gt;It is difficult to add additional data types (in our example, each data
          type represents a different operator) because adding a new type affects
          all operations.&lt;/li&gt;
          &lt;li&gt;Each operation must account for every data type.&lt;/li&gt;
          &lt;li&gt;The internal representation of the data type must be accessible to every
          operation. This makes even minor changes to the data layout have far
          reaching effects and often leads to difficult to maintain implementations.&lt;/li&gt;
          &lt;li&gt;It is very difficult, and requires careful planning, to dynamically add
          data types.&lt;/li&gt;
          &lt;li&gt;Efficient storage of the data types requires variant record support in
          the language, as discussed in &lt;a href=&quot;#note1&quot;&gt;note 1&lt;/a&gt; below, (which can
            be simulated though inheritance in &lt;i&gt;object-oriented&lt;/i&gt; languages but I
            don&apos;t provide and example of that).&lt;/li&gt;
            &lt;/ol&gt;
            &lt;p&gt;To demonstrate the difficulty of adding a data type, let&apos;s add power support.
            &lt;/p&gt;
            
            &lt;blockquote&gt;
            &lt;table border=&quot;0&quot; id=&quot;table2&quot;&gt;
            &lt;tbody&gt;&lt;tr&gt;
            &lt;td width=&quot;100&quot;&gt;&lt;i&gt;expr&lt;/i&gt; ^ &lt;i&gt;expr&lt;/i&gt;&lt;/td&gt;
            &lt;td&gt;Power&lt;/td&gt;
            &lt;/tr&gt;
            &lt;/tbody&gt;&lt;/table&gt;
            &lt;/blockquote&gt;
            

            &lt;p&gt;This represents the expression &lt;i&gt;x&lt;sup&gt;n&lt;/sup&gt;&lt;/i&gt;. To implement this we first we need to modify the enumeration
            to add &lt;code&gt;Power&lt;/code&gt;,&lt;/p&gt;

&lt;code&gt;&lt;pre&gt;
enum Oper { Add, Subtract, Multiply, Divide, Literal, Power }
&lt;/pre&gt;&lt;/code&gt;
            &lt;p&gt;Next we need to add an additional &lt;i&gt;case statement&lt;/i&gt; to &lt;code&gt;Evaluate()&lt;/code&gt;&apos;s
            &lt;i&gt;switch statement&lt;/i&gt;,&lt;/p&gt;

&lt;code&gt;&lt;pre&gt;
case Oper.Power: return Math.Pow(left, right);
&lt;/pre&gt;&lt;/code&gt;

            &lt;p&gt;An finally we need to also add another &lt;i&gt;case statement&lt;/i&gt; to &lt;code&gt;Print&lt;/code&gt;&apos;s
            &lt;i&gt;switch statement &lt;/i&gt;that looks like,&lt;/p&gt;

&lt;code&gt;&lt;pre&gt;
case Oper.Power: Print.(e.Left); Console.Write(&quot; ^ &quot;); Print(e.Right); break;
&lt;/pre&gt;&lt;/code&gt;

            &lt;p&gt;Note we had to modify both operations and the &lt;code&gt;Oper&lt;/code&gt; enumeration. If we had several
            operations, we would have had
            to modify most, if not all, of them. Also, note that the compiler didn&apos;t help us
            find any of the places we need to modify since the code was legal after each of the above modifications.
            If we had made the changes to just &lt;code&gt;Evaluate()&lt;/code&gt; for
            example, the compiler wouldn&apos;t have complained, but we would receive a runtime
            error if we called &lt;code&gt;Print()&lt;/code&gt; with an expression tree
            that contains a &lt;code&gt;Power&lt;/code&gt; node.&lt;/p&gt;
            &lt;p&gt;To learn more about the expression problem and its history, see
            &lt;a href=&quot;http://www.cs.pomona.edu/~kim/&quot;&gt;Kim Bruce&lt;/a&gt;&apos;s excellent paper,
            &lt;a href=&quot;http://www.cs.pomona.edu/~kim/README.html#Wood&quot;&gt;Some Challenging Typing
            Issues in Object-Oriented Languages&lt;/a&gt;. Be warned, it will be a bit of a
            spoiler for some of what I will cover.&lt;/p&gt;&lt;hr /&gt;
            &lt;p&gt;&lt;a name=&quot;note1&quot; href=&quot;#ref-note1&quot;&gt;1&lt;/a&gt; - A casual observer will notice that
            &lt;code&gt;Expr&lt;/code&gt; is a bit wasteful in that it allocates
            room for a value for every node instead of only for literals. It also takes up
            space for &lt;code&gt;Left&lt;/code&gt; and &lt;code&gt;Right&lt;/code&gt; for literals. In C#, removing this waste is a bit awkward
            to express and makes the &lt;code&gt;Evaluate()&lt;/code&gt; method more
            complicated, so I ignored the waste in favor of simplicity.&lt;/p&gt;
            &lt;p&gt;In C++, this is a bit less awkward, and might look like,&lt;/p&gt;

&lt;code&gt;&lt;pre&gt;
struct Expr {
  Oper            oper;
  union {
    struct {
      Expr   *left;
      Expr   *right;
    };
    double      value;
  };
  Expr(Oper oper, Expr *left, Expr *right) { ... }
  Expr(double value) { ... }
};
&lt;/pre&gt;&lt;/code&gt;

              &lt;p&gt;with a very similar evaluation function, but for now we will stick with C#. &lt;a href=&quot;#ref-note1&quot;&gt;&amp;lt;&amp;lt; back &amp;lt;&amp;lt;&lt;/a&gt;&lt;/p&gt;
              &lt;p&gt;&lt;a name=&quot;note2&quot; href=&quot;#ref-note2&quot;&gt;2&lt;/a&gt; - For simplicity, I ignored operator precedence. Later I will present a version of
              this routine that handles precedence correctly. &lt;a href=&quot;#ref-note2&quot;&gt;&amp;lt;&amp;lt; back &amp;lt;&amp;lt;&lt;/a&gt;&lt;/p&gt;&amp;lt;/div&amp;gt;
&lt;/p&gt;&lt;/div&gt;
</description>
                <link>http://removingalldoubt.com/programming/2005/11/12/The-Expression-Problem-Part-I-Procedural</link>
                <guid>http://removingalldoubt.com/programming/2005/11/12/The-Expression-Problem-Part-I-Procedural</guid>
                <pubDate>2005-11-12T16:42:00+00:00</pubDate>
        </item>

        <item>
                <title>More Cider at the PDC</title>
                <description>&lt;p&gt;Yet more Cider stuff. Brian and Mark tag teamed together at the PDC to
present how to add design-time behavior to WPF (Avalon) controls. You can find
their presentation
&lt;a href=&quot;http://microsoft.sitestream.com/PDC05/TLN/TLN319_files/Default.htm#nopreload=1&amp;amp;autostart=1&quot;&gt;
here…&lt;/a&gt; (Maybe &lt;a href=&quot;http://www.corbinstreehouse.com/blog&quot;&gt;Corbin&lt;/a&gt;
will be able to
&lt;a href=&quot;http://www.removingalldoubt.com/commentview.aspx/f63c1229-365e-4fba-9765-d21d6a18f9b9&quot;&gt;
view this one&lt;/a&gt; ;-).&lt;/p&gt;

&lt;p&gt;I seem to not be in any of the recent videos. I am really not that bashful.
Here I am in the audience (Mark points to me). I wasn&apos;t on stage because I had
to leave early to catch a plane. I will probably show up sooner or later in one
of these.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/cider/2005/10/28/More-Cider-at-the-PDC</link>
                <guid>http://removingalldoubt.com/programming/cider/2005/10/28/More-Cider-at-the-PDC</guid>
                <pubDate>2005-10-28T13:36:00+00:00</pubDate>
        </item>

        <item>
                <title>Cider on Channel 9</title>
                <description>
</description>
                <link>http://removingalldoubt.com/programming/cider/2005/10/25/Cider-on-Channel-9</link>
                <guid>http://removingalldoubt.com/programming/cider/2005/10/25/Cider-on-Channel-9</guid>
                <pubDate>2005-10-25T20:14:00+00:00</pubDate>
        </item>

        <item>
                <title>Cider architecture</title>
                <description>&lt;p&gt;Brian Pepin has started to blog about Cider&apos;s architecture.
&lt;a href=&quot;http://www.urbanpotato.net/Default.aspx/document/2224&quot;&gt;Check it out...&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[EDIT 01/19/2015: This is now &lt;a href=&quot;http://pocketsilicon.com/?tag=/cider&quot;&gt; here &lt;/a&gt;]&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/cider/2005/10/25/Cider-architecture</link>
                <guid>http://removingalldoubt.com/programming/cider/2005/10/25/Cider-architecture</guid>
                <pubDate>2005-10-25T09:28:00+00:00</pubDate>
        </item>

        <item>
                <title>A Tour of XAML - Part III - Type converters</title>
                <description>&lt;p&gt;[EDIT 01/19/2015: This content is out of date.]&lt;/p&gt;

&lt;p&gt;Fundamentally XAML is an object initialization language. It creates object
and sets values to properties. XAML, however, is not limit to creating reference types. You can also create instances of any type
that has an associated type converter. For example, XAML can create an instance of a brush
with the following code,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;Brush&amp;gt;Red&amp;lt;/Brush&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;which creates a red solid color brush by calling the type converter
associated with Brush. This means that,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;Button&amp;gt;
  &amp;lt;Button.Background&amp;gt;
    &amp;lt;Brush&amp;gt;Red&amp;lt;/Brush&amp;gt;
  &amp;lt;Button.Background&amp;gt;
&amp;lt;/Button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;is equivilent to,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;Button Background=&quot;Red&quot; /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;XAML can be used to create value types, such as an integer, through its type
converter as in, &lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;s:Int32&amp;gt;27&amp;lt;/s:Int32&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;or a string,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;s:String&amp;gt;It is the time for all good men...&amp;lt;/s:String&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;by using string&apos;s type converter. Note that XAML always refers to the CLR
name for the type, not a language specific name such as &quot;int&quot; in C# or &quot;Integer&quot;
in Pascal. Both of these types refer to the System.Int32 class in the CLR and
XAML uses the CLR name, Int32. Note also that neither the XAML nor the WPF (Avalon) namespaces refer to the System
namespace in mscorlib.dll where these types live. This mean that somewhere in the document
you need to declare the prefix. This might look something like:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;?Mapping ClrNamespace=&quot;System&quot; XmlNamespace=&quot;System&quot;
    Assembly=&quot;mscorlib&quot;?&amp;gt;
&amp;lt;Window
    xmlns=&quot;http://schemas.microsoft.com/winfx/avalon/2005&quot;
    xmlns:s=&quot;System&quot;&amp;gt;
  &amp;lt;Button&amp;gt;
    &amp;lt;s:String&amp;gt;It is the time for all good men...&amp;lt;/s:String&amp;gt;
  &amp;lt;/Button&amp;gt;
&amp;lt;/Window&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Of course, for string, it is easier to write it,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;Window xmlns=&quot;http://schemas.microsoft.com/winfx/avalon/2005&quot;&amp;gt;
  &amp;lt;Button&amp;gt;It is the time for all good men...&amp;lt;/Button&amp;gt;
&amp;lt;/Window&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;but the string for is useful when you want multiple strings in a collection, say, for example, as the content of a list box,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;?Mapping ClrNamespace=&quot;System&quot; XmlNamespace=&quot;System&quot;
    Assembly=&quot;mscorlib&quot;?&amp;gt;
&amp;lt;Window
    xmlns=&quot;http://schemas.microsoft.com/winfx/avalon/2005&quot;
    xmlns:s=&quot;System&quot;&amp;gt;
  &amp;lt;ListBox&amp;gt;
    &amp;lt;s:String&amp;gt;It is the time for all good men...&amp;lt;/s:String&amp;gt;
    &amp;lt;s:String&amp;gt;to come to the aid of their country.&amp;lt;/s:String&amp;gt;
  &amp;lt;/ListBox&amp;gt;
&amp;lt;/Window&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note that if this had been written,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;Window xmlns=&quot;http://schemas.microsoft.com/winfx/avalon/2005&quot;&amp;gt;
  &amp;lt;ListBox&amp;gt;
    It is the time for all good men...
    to come to the aid of their country.
  &amp;lt;/ListBox&amp;gt;
&amp;lt;/Window&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;the list box would only contain one string, not two. This is because all
continuous text content is considered one string and not two. If you load this
in WPF you will also notice that the string get treated as &quot;It is time for all
good men... to come to the aid of their country&quot; where the spaces and line
breaks have been collapsed. I will discuss the rules for whitespace
normalization (collapsing) in a subsequent post. For now, if you are familiar
with HTML, what XAML does shouldn&apos;t come as any surprise.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/xaml/2005/10/16/A-Tour-of-XAML-Part-III-Type-converters</link>
                <guid>http://removingalldoubt.com/programming/xaml/2005/10/16/A-Tour-of-XAML-Part-III-Type-converters</guid>
                <pubDate>2005-10-16T16:38:00+00:00</pubDate>
        </item>

        <item>
                <title>We are back on the air</title>
                <description>&lt;p&gt;After a brief interlude, this site is now back up.
I use a server in &lt;a href=&quot;http://www.urbanpotato.net/&quot;&gt;Brian’s &lt;/a&gt;home to host
this blog. Brian gives some reasons for the temporary outage
&lt;a href=&quot;http://www.urbanpotato.net/Default.aspx/document/2210&quot;&gt;here&lt;/a&gt;. This,
of course doesn’t explain why I don’t have at least one real blog entry queued
up ready to go. Let’s just say, that Brian’s machine wasn’t the only thing under
the weather lately…&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/blog/2005/10/14/We-are-back-on-the-air</link>
                <guid>http://removingalldoubt.com/blog/2005/10/14/We-are-back-on-the-air</guid>
                <pubDate>2005-10-14T22:37:00+00:00</pubDate>
        </item>

        <item>
                <title>OOPSLA '05</title>
                <description>&lt;p&gt;Next week I will be attending
&lt;a href=&quot;http://www.oopsla.org:8080/2005/ShowPage.do?id=Home&amp;amp;referrer=www.removingalldoubt.com&quot;&gt;OOPSLA&lt;/a&gt;. This
will be my first technical conference where I will just be a regular attendee. I
have been to more conferences than I can count but I was always there on some
official reason either as a presenter or booth worker, or there for some special
event such as to launch a product or attend some award presentation. If you are
there too, feel free to stop me in the hall.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/oopsla/2005/10/14/OOPSLA-05</link>
                <guid>http://removingalldoubt.com/programming/oopsla/2005/10/14/OOPSLA-05</guid>
                <pubDate>2005-10-14T13:21:00+00:00</pubDate>
        </item>

        <item>
                <title>Cider demo'ed at the PDC</title>
                <description>&lt;p&gt;If you missed the PDC and Eric Rudder’s keynote address where Cider was
demonstrated you can watch it &lt;a href=&quot;http://msdn.microsoft.com/events/pdc/&quot;&gt;
here&lt;/a&gt;. Mark Boulter demonstrates it with Joe Marini demonstrating Sparkle.
Their part begins around 37 minutes into the keynote.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/cider/2005/10/04/Cider-demoed-at-the-PDC.aspx</link>
                <guid>http://removingalldoubt.com/programming/cider/2005/10/04/Cider-demoed-at-the-PDC.aspx</guid>
                <pubDate>2005-10-04T12:34:00+00:00</pubDate>
        </item>

        <item>
                <title>A Tour of XAML - Part II: Property Elements?</title>
                <description>&lt;p&gt;XAML provides two ways to set a property, either through a &lt;i&gt;Property
Attribute&lt;/i&gt; or through a &lt;i&gt;Property Element&lt;/i&gt;. We covered &lt;i&gt;Property
Attribute&lt;/i&gt;s last time, this time we will focus on the
&lt;i&gt;Property Element&lt;/i&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;i&gt;Property Element&lt;/i&gt; is an element that, instead of creating an object
instance, set the value of a property. The following is equivalent to the
example given in Part I but uses &lt;i&gt;Property Element&lt;/i&gt;s instead of &lt;i&gt;Property
Attribute&lt;/i&gt;s.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;Button xmlns=&quot;http://schemas.microsoft.com/winfx/avalon/2005&quot;&amp;gt;
  &amp;lt;Button.Content&amp;gt;
    Hello, World!
  &amp;lt;/Button.Content&amp;gt;
&amp;lt;/Button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The loader distinguishes a &lt;i&gt;Object Element&lt;/i&gt; from a &lt;i&gt;Property Element&lt;/i&gt;
by the presents of the &apos;.&apos; in the name. The first part of the name is a
reference to a type and the second part, after the dot, refers to the property
name (we will discuss exactly why this is when we discuss &lt;i&gt;Attached Properties&lt;/i&gt;). Here we use Button in both &lt;i&gt;Property Element&lt;/i&gt;s. &lt;i&gt;Property Element&lt;/i&gt;s
are necessary when the value of the property cannot be expressed as a string.
For example, you can use a &lt;i&gt;Property Element&lt;/i&gt; to set the content of a Button
to be another Button (not sure why you would want that, but suspend disbelieve for
  a second) as in,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;Button xmlns=&quot;http://schemas.microsoft.com/winfx/avalon/2005&quot;&amp;gt;
  &amp;lt;Button.Content&amp;gt;
    &amp;lt;Button Content=&quot;Hello, World!&quot; /&amp;gt;
  &amp;lt;/Button.Content&amp;gt;
&amp;lt;/Button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This can be written more explicitly as,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;Button xmlns=&quot;http://schemas.microsoft.com/winfx/avalon/2005&quot;&amp;gt;
  &amp;lt;Button.Content&amp;gt;
    &amp;lt;Button&amp;gt;
      &amp;lt;Button.Content&amp;gt;
        Hello, World!
      &amp;lt;/Button.Content&amp;gt;
    &amp;lt;/Button&amp;gt;
  &amp;lt;/Button.Content&amp;gt;
&amp;lt;/Button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As you can see, using &lt;i&gt;Property Element&lt;/i&gt;s can become a bit repetitive.
To mitigate this, one of the properties of a class can be designated as
the &lt;i&gt;Content Property&lt;/i&gt; which tells the load to assign direct content of the element to
that property. In the case of Button (and all ContentControls)
the &lt;i&gt;Content Property&lt;/i&gt; is, oddly enough, set to the property named
Content. This allows us to write something like,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;Button xmlns=&quot;http://schemas.microsoft.com/winfx/avalon/2005&quot;&amp;gt;
  Hello, World!
&amp;lt;/Button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;for the first example above, and,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;Button xmlns=&quot;http://schemas.microsoft.com/winfx/avalon/2005&quot;&amp;gt;
  &amp;lt;Button&amp;gt;Hello, World!&amp;lt;/Button&amp;gt;
&amp;lt;/Button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;for the second. Most Avalon classes have a &lt;i&gt;Content Property&lt;/i&gt; defined.
  For Panel&apos;s, such as DockPanel, Grid, and Canvas, their &lt;i&gt;Content Property&lt;/i&gt;
  is set to their Children collection. Any direct content of a Panel is assumed to
  be content intended for the Children property. This means the following,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;StackPanel xmlns=&quot;http://schemas.microsoft.com/winfx/avalon/2005&quot;&amp;gt;
  &amp;lt;Button&amp;gt;OK&amp;lt;/Button&amp;gt;
  &amp;lt;Button&amp;gt;Cancel&amp;lt;/Button&amp;gt;
&amp;lt;/StackPanel&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;is an abbreviated version of,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;StackPanel xmlns=&quot;http://schemas.microsoft.com/winfx/avalon/2005&quot;&amp;gt;
  &amp;lt;StackPanel.Children&amp;gt;
    &amp;lt;Button&amp;gt;
      &amp;lt;Button.Content&amp;gt;
        OK
      &amp;lt;/Button.Content&amp;gt;
    &amp;lt;/Button&amp;gt;
  &amp;lt;Button&amp;gt;
    &amp;lt;Button.Content&amp;gt;
      Cancel
    &amp;lt;/Button.Content&amp;gt;
  &amp;lt;/Button&amp;gt;
  &amp;lt;/StackPanel.Children&amp;gt;
&amp;lt;/StackPanel&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now, with &lt;i&gt;Property Element&lt;/i&gt;s and &lt;i&gt;Content Property Attribute&lt;/i&gt;s and&lt;i&gt;
  Object Element&lt;/i&gt; and &lt;i&gt;Property Attribute&lt;/i&gt; from Part I, we have covered
  the fundamentals of XAML. &lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/xaml/2005/09/28/A-Tour-of-XAML-Part-II-Property-Elements</link>
                <guid>http://removingalldoubt.com/programming/xaml/2005/09/28/A-Tour-of-XAML-Part-II-Property-Elements</guid>
                <pubDate>2005-09-28T20:36:00+00:00</pubDate>
        </item>

        <item>
                <title>A Tour of XAML - Part I: What is XAML?</title>
                <description>&lt;p&gt;Today I begin a tour of XAML. As &lt;a href=&quot;http://www.simplegeek.com&quot;&gt;Chris&lt;/a&gt;
said, I have been part of development of XAML. I came fairly late to the game,
however, more closely to the end of
the Avalon development cycle than at the beginning, and XAML was already fairly
well defined when I arrived. I did work to push some changes into the language, primarily
driving consistency and regularity in the language as well as put developing
features that support versioning and extensibility. We will cover some of those
things in our tour, but lets start at the beginning.&lt;/p&gt;

&lt;p&gt;The first question we will cover in our tour is what is XAML anyway? Simply,
XAML is an XML based object initialization language. Its job in life is to
create a tree of objects. For example, consider the following XAML file,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;Button Content=&quot;Hello, world!&quot;
  xmlns=&quot;http://schemas.microsoft.com/winfx/avalon/2005&quot; /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This creates an instance of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;System.Windows.Controls.Button&lt;/code&gt; and
sets the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Content1&lt;/code&gt; property to the sting value
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Hello, world!&quot;&lt;/code&gt;. The reason &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;System.Windows.Button&lt;/code&gt;
is selected and not, say &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;System.Windows.Forms.Button&lt;/code&gt;,
is because of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xmlns&lt;/code&gt; declaration in the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Button&lt;/code&gt; tag. The magic behind the URI
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;http://schemas.microsoft.com/winfx/avalon/2005&quot;&lt;/code&gt;
is hidden in the Avalon assemblies themselves. They contain an assembly
attribute that looks like,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[assembly: XmlnDefinition(
  &quot;http://schemas.microsoft.com/winfx/avalon/2005&quot;,
  &quot;System.Windows.Controls&quot;)]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It actually has several more definitions like this to associate several other
CLR namespaces with the Avalon XML namespace, but this is the one used above.
Because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PresentationFramework.dll&lt;/code&gt; is in the GAC or referenced
by the project if the XAML is compiled into assembly, the Avalon
loader will find the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XmlnsDefinition&lt;/code&gt; in the assembly
and it is able to determine that it is Avalon’s button we want to create.&lt;/p&gt;

&lt;p&gt;Now lets take a look at the phrase &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Content=&quot;Hello, world!&quot;&lt;/code&gt;. This causes the
loader to look up the property called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Content&lt;/code&gt; and
sets its value. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Content&lt;/code&gt; property is introduced in
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Button&lt;/code&gt;’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ContentControl&lt;/code&gt; base class and is of type
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;System.Object&lt;/code&gt;. Since there is no applicable type
converter for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;System.Object&lt;/code&gt;, the loader uses the default type of an
attribute, string, and calls the setter method for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Content&lt;/code&gt;.
Things are slightly more complicated than this, but this should give you the
basic idea. This gives us an instance of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;System.Windows.Controls.Button&lt;/code&gt;
class with its &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Content&lt;/code&gt; property set to the value
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Hello, world!&quot;&lt;/code&gt;, which is, hopefully, what we wanted.&lt;/p&gt;

&lt;p&gt;If we change the XAML to,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;Button Content=&quot;Hello, world!&quot; IsDefault=&quot;True&quot;
  xmlns=&quot;http://schemas.microsoft.com/winfx/avalon/2005&quot; /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;adding the phrase &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IsDefault=&quot;True&quot;&lt;/code&gt; which causes the loader to find the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IsDefault&lt;/code&gt; property introduced on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Button&lt;/code&gt; class itself. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IsDefault&lt;/code&gt;
property is of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;System.Boolean&lt;/code&gt; which has a type converter,
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;System.ComponentModel.BooleanConverter&lt;/code&gt;, associated with it.
The loader will pass the string, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;True&quot;&lt;/code&gt;, to the type
converter associated with the property (or, as in this case, the property’s
type) prior to setting the property’s value; after which we will have an
instance of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;System.Windows.Controls.Button&lt;/code&gt; class with its &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Content&lt;/code&gt;
property set to the value &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Hello, world!&quot;&lt;/code&gt;, and its &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IsDefault&lt;/code&gt;
property set to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;True&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This time we covered an &lt;em&gt;Object Element&lt;/em&gt;, an XML element used to create
an object, and we used a &lt;em&gt;Property Attribute&lt;/em&gt;, an XML element used to set
the value of an object instance’s property. Next time we will get use a
&lt;em&gt;Property Element&lt;/em&gt; and take advantage of the &lt;em&gt;Content Property Attribute&lt;/em&gt;.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/xaml/2005/09/24/A-Tour-of-XAML-Part-I-What-is-XAML</link>
                <guid>http://removingalldoubt.com/programming/xaml/2005/09/24/A-Tour-of-XAML-Part-I-What-is-XAML</guid>
                <pubDate>2005-09-24T15:25:00+00:00</pubDate>
        </item>

        <item>
                <title>Cider is Announced</title>
                <description>&lt;p&gt;Cider is now announced! In case you missed it at the PDC, Cider is the code
name for the Visual Studio Designer for WPF pronounced Avalon according to
Chris) that will be delivered in Orcas. We also announced the Expression product
line that includes a different designer called
&lt;a href=&quot;http://www.eweek.com/category2/0,1874,1361732,00.asp&quot;&gt;Sparkle&lt;/a&gt;.
Other than the obvious difference between Sparkle and Cider, Cider is in VS,
Sparkle is a stand-alone product, Cider will be a designer for developers,
Sparkle will be a designer targeted at professional designers.&lt;/p&gt;

&lt;p&gt;For those that were asking about what I was working on, I wasn’t trying to be
coy, I just didn’t want to spill the beans early.&lt;/p&gt;

&lt;p&gt;I will be more forthcoming in the months ahead and try to keep you up-to-date
with our progress.&lt;/p&gt;

&lt;p&gt;If I missed you at the PDC, I am sorry about that. Feel free to ask your
questions here or via e-mail (chuckj directed through microsoft.com).&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/pdc05/cider/xaml/2005/09/18/Cider-is-Announced</link>
                <guid>http://removingalldoubt.com/programming/pdc05/cider/xaml/2005/09/18/Cider-is-Announced</guid>
                <pubDate>2005-09-18T23:07:00+00:00</pubDate>
        </item>

        <item>
                <title>Off to the PDC</title>
                <description>&lt;p&gt;I am off to PDC. This will be my first PDC as an “insider” instead of a
regular attendee, I am looking forward to it. We are announcing some very
exciting things! I am not going to be presenting but I will be available to
chat. I plan to spend most of my time in the “Big Room”. If you don’t know what
that is now, you will once you get there. I should be easy to spot, I will be
the guy in a blue Microsoft shirt and tan pants… ;-).&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/pdc05/2005/09/09/Off-to-the-PDC</link>
                <guid>http://removingalldoubt.com/programming/pdc05/2005/09/09/Off-to-the-PDC</guid>
                <pubDate>2005-09-09T20:54:00+00:00</pubDate>
        </item>

        <item>
                <title>Comment Toggle</title>
                <description>&lt;p&gt;When writing a prototype for a syntax highlighter,
more years ago than I care to admit, I came across an interesting property of
the two character comment delimiters found in the C family of
languages as well as Pascal. I have shown this to several people and many had never seen it before
so I thought I would share it more widely.&lt;/p&gt;

&lt;p&gt;Take C# for example, you can quickly toggle between
two blocks of code by using the three characters &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/*/&lt;/code&gt;
in the middle. These three characters act as a comment toggle. If you are in a
block comment, it will terminate the comment, if not, it starts one. This combined
with the unconditional comment terminator sequence &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/* */&lt;/code&gt; which ensures that the characters follow are not in a block
comment, but is still a legal sequence of characters outside a comment, you can toggle between
blocks of code. For example,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/* */
Console.WriteLine(&quot;Option 1&quot;);
/*/
Console.WriteLine(&quot;Option 2&quot;);
/* */
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This will print “Option 1” to the console. The second
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WriteLine()&lt;/code&gt; is commented out. With a simple
one character change, removing the trailing “/” in the first comment, will cause
“Option 2” to print instead.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/* *
Console.WriteLine(&quot;Option 1&quot;);
/*/
Console.WriteLine(&quot;Option 2&quot;);
/* */
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The first &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WriteLine()&lt;/code&gt; is commented out and the
second is now not. I use this sometimes to quickly switch between two optional
implementations of routines when I am rewriting code, or when constructing test
applications.&lt;/p&gt;

&lt;p&gt;You can use the unconditional comment terminator to alternate between several
versions such as,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/* */
Console.WriteLine(&quot;Option 1&quot;);
/* *
Console.WriteLine(&quot;Option 2&quot;);
/* *
Console.WriteLine(&quot;Option 3&quot;);
/* */
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To select a different option you need to change two characters, such as removing the
trailing “/” of the first comment and adding one to the third, as in,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/* *
Console.WriteLine(&quot;Option 1&quot;);
/* *
Console.WriteLine(&quot;Option 2&quot;);
/* */
Console.WriteLine(&quot;Option 3&quot;);
/* */
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;which will cause “Option 3” to print instead of “Option 1”.&lt;/p&gt;

&lt;p&gt;Pascal can do the same thing with the its block comment as
in,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;(* *)
Writeln(&apos;Option 1&apos;);
(*)
Writeln(&apos;Option 2&apos;);
(* *)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note that some implementations of C and C++ support nested
comments and, therefore, the sequence &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/*/&lt;/code&gt;
always starts a comment instead of toggling, so this sequence is not as useful.&lt;/p&gt;

&lt;p&gt;The reason this was important to my prototype is a bit hard
to explain and is not nearly as interesting as the toggle itself.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/2005/09/03/Comment-Toggle</link>
                <guid>http://removingalldoubt.com/programming/2005/09/03/Comment-Toggle</guid>
                <pubDate>2005-09-03T10:47:00+00:00</pubDate>
        </item>

        <item>
                <title>Sorting Part III - HeapSort</title>
                <description>&lt;p&gt;Last time we looked at the QuickSort and found that it was much better, in most
cases, than the selection sort, but there was still a pathological case where
the QuickSort wasn’t better. Can we guarantee we &lt;em&gt;are&lt;/em&gt; better than the
O(N&lt;sup&gt;2&lt;/sup&gt;) of the selection sort?
Can we guarantee O(N log N)? To do this we need to find something in QuickSort
we can do better. The Achilles’ heal for the QuickSort is selecting the median
value of the partition. Several techniques have been developed to avoid the
worst case scenarios (like selecting three values and taking the median; in
other words, median of three, or just picking a random item in the partition)
but none of these guarantee O(N log N). We need a more radical approach.&lt;/p&gt;

&lt;p&gt;We need a way of preserving the comparisons; much like QuickSort does, but that
doesn’t use partitions. As is often the case, when you need a radical new
approach, you should try a different data structure (much like choosing a
different coordinate system in calculus). If we organize the array into a binary
tree, we can find the largest element in the array in O(N log N) comparisons.
This is because each element only needs to be compared with its parents, which
is O(log N) comparisons, and we need to do this for every element in the in the
array, O(N), giving us O(N log N). This sounds promising, but how do you
organize an array into a binary tree? This is easier than it sounds. If you
number an array from &lt;em&gt;1&lt;/em&gt; to &lt;em&gt;n&lt;/em&gt;, where each number is the index of
the elements in the array, you can treat the array as a binary tree by defining
the left node of any element &lt;em&gt;i&lt;/em&gt; to be at index &lt;em&gt;2i&lt;/em&gt; and the right
node to be at &lt;em&gt;2i+1&lt;/em&gt; (or the other way around, if you must, but I like to
think of the odd children as right). To create the desired tree, we need to
ensure that for every element in the array at index &lt;em&gt;i&lt;/em&gt;, it is the
greater than its children. In other words, for all elements of array &lt;em&gt;a&lt;/em&gt;,
&lt;em&gt;a&lt;sub&gt;i&lt;/sub&gt; ≥ a&lt;sub&gt;2i&lt;/sub&gt;&lt;/em&gt; for &lt;em&gt;2i ≤ n&lt;/em&gt; and
&lt;em&gt;a&lt;sub&gt;i&lt;/sub&gt; ≥ a&lt;sub&gt;2i+1&lt;/sub&gt;&lt;/em&gt; for &lt;em&gt;2i+1 ≤ n&lt;/em&gt;. An array that
obeys this constraint is called a heap (hence the name HeapSort).&lt;/p&gt;

&lt;p&gt;Forming a &lt;em&gt;heap&lt;/em&gt; is fairly straight forward and follows the informal description
given above; you swap each element, from &lt;em&gt;2&lt;/em&gt; to &lt;em&gt;n&lt;/em&gt;, with its parent until you
come across a parent that is greater than the element. The algorithm for this
might look something like,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;static void FormHeap(int[] a) {
  // form the heap
  for (int i = 1; i &amp;lt; a.Length; i++) {
    int j = i;
    while (j &amp;gt; 0) {
      int k = (j + 1) / 2 - 1;
      if (a[k] &amp;lt; a[j])
        Swap(a, j, k);
      else
        break;
      j = k;
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note, that since C# numbers the array from &lt;em&gt;0&lt;/em&gt; to &lt;em&gt;n-1&lt;/em&gt; instead of from &lt;em&gt;1&lt;/em&gt; to
&lt;em&gt;n&lt;/em&gt;, we need to subtract add one to the index &lt;em&gt;j&lt;/em&gt; and then subtract it again
while calculating the parent’s index.&lt;/p&gt;

&lt;p&gt;Now that we have the have the greatest element in the array, we know which
element should be at the end of the array. We need only swap it with the last
element of the array to put it there, but doing so will violate the &lt;em&gt;heap&lt;/em&gt;
constraint above since &lt;em&gt;a&lt;sub&gt;n&lt;/sub&gt;&lt;/em&gt; is guaranteed to be less than or equal to
both &lt;em&gt;a&lt;sub&gt;2&lt;/sub&gt;&lt;/em&gt; and &lt;em&gt;a&lt;sub&gt;3&lt;/sub&gt;&lt;/em&gt;. To restore order back to the &lt;em&gt;heap&lt;/em&gt; we
must push the new first element down in the heap until it is greater than or
equal to both of its children or it is childless. This can be done by selecting
the greatest child and swapping positions with it then repeating until it is
greater than the children or it has no children. Since we are following a single
line of succession, an element will only have, at most &lt;em&gt;log&lt;sub&gt;2&lt;/sub&gt;n&lt;/em&gt;
generations (or levels, as it is more traditional called) below it. This means
we can restore order to the &lt;em&gt;heap&lt;/em&gt; in O(log N) comparisons. This might look
like,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;static void ReformHeap(int[] a, int i) {
  int j = 0;
  while (true) {
    int k = (j + 1) * 2 - 1;
    if (k &amp;gt;= i)
      break;
    if (k + 1 &amp;gt;= i || a[k] &amp;gt; a[k + 1]) {
      if (a[j] &amp;gt; a[k])
        break;
      Swap(a, k, j);
      j = k;
    }
    else {
      if (a[j] &amp;gt; a[k + 1])
        break;
      Swap(a, k + 1, j);
      j = k + 1;
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We now need to do this &lt;em&gt;n&lt;/em&gt; times to completely sort the array, meaning that we are guaranteed to sort a &lt;em&gt;heap&lt;/em&gt; in O(N log N) comparisons. This gives us O(N log N) operations to form a &lt;em&gt;heap&lt;/em&gt; and O(N log N) operations to sort a &lt;em&gt;heap&lt;/em&gt; which means that we can sort any array, using a &lt;em&gt;heap&lt;/em&gt; in O(N log N) (since big O notation throws away the constant 2 in 2N log N). Putting this together gives us the &lt;em&gt;HeapSort&lt;/em&gt; invented by J.W.J. Williams,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;static void HeapSort(int[] a) {
  // form the heap
  for (int i = 1; i &amp;lt; a.Length; i++) {
    int j = i;
    while (j &amp;gt; 0) {
      int k = (j + 1) / 2 - 1;
      if (a[k] &amp;lt; a[j])
        Swap(a, j, k);
      else
        break;
      j = k;
    }
  }

  // Pick the top of the heap and place it last, reform the
  // heap, then take the new top and place it next to last.
  for (int i = a.Length - 1; i &amp;gt; 0; i--) {

    // The top of the heap contains the highest value, swap
    // it the last member
    Swap(a, 0, i);

    // Reform the heap it. It contains a relatively low
    // value on top. Replace it with the greatest child.
    int j = 0;
    while (true) {
      int k = (j + 1) * 2 - 1;
      if (k &amp;gt;= i)
        break;
      if (k + 1 &amp;gt;= i || a[k] &amp;gt; a[k + 1]) {
        if (a[j] &amp;gt; a[k])
          break;
        Swap(a, k, j);
        j = k;
      }
      else {
        if (a[j] &amp;gt; a[k + 1])
          break;
        Swap(a, k + 1, j);
        j = k + 1;
      }
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here we have it. An algorithm that is guaranteed to perform on the order of O(N log N) operations to sort an array! And here, you are apt to say, “Why doesn’t everyone use a HeapSort instead of a QuickSort if HeapSort is so much better?” Good question, I am glad you asked. There are several reasons why, but I will save that for another time.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/2005/08/18/Sorting-Part-III-HeapSort</link>
                <guid>http://removingalldoubt.com/programming/2005/08/18/Sorting-Part-III-HeapSort</guid>
                <pubDate>2005-08-18T09:55:00+00:00</pubDate>
        </item>

        <item>
                <title>Sorting Part II: QuickSort</title>
                <description>&lt;p&gt;As we noted in Part I, a selection sort is an easy sort to write but
unfortunately it performs on the order of
&lt;em&gt;n&lt;span style=&quot;font-size: 8pt;&quot;&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/span&gt;&lt;/em&gt; comparisons.
To produce an improved version we need to find a piece of information that the
selection sort is throwing away and take advantage of it.&lt;/p&gt;

&lt;p&gt;Selection sort will compare a value of the array with all of the other values
of the array to determine which is the smallest. It doesn’t take advantage to
the fact that comparing say,
&lt;em&gt;a&lt;span style=&quot;font-size: 8pt;&quot;&gt;&lt;sub&gt;10&lt;/sub&gt;&lt;/span&gt;&lt;/em&gt; to
&lt;em&gt;a&lt;span style=&quot;font-size: 8pt;&quot;&gt;&lt;sub&gt;20&lt;/sub&gt;&lt;/span&gt;&lt;/em&gt; and comparing
&lt;em&gt;a&lt;span style=&quot;font-size: 8pt;&quot;&gt;&lt;sub&gt;20&lt;/sub&gt;&lt;/span&gt;&lt;/em&gt; to, say,
&lt;em&gt;a&lt;span style=&quot;font-size: 8pt;&quot;&gt;&lt;sub&gt;40&lt;/sub&gt;&lt;/span&gt;&lt;/em&gt; could tell us
something about the relationship between
&lt;em&gt;a&lt;span style=&quot;font-size: 8pt;&quot;&gt;&lt;sub&gt;10&lt;/sub&gt;&lt;/span&gt;&lt;/em&gt; and
&lt;em&gt;a&lt;span style=&quot;font-size: 8pt;&quot;&gt;&lt;sub&gt;40&lt;/sub&gt;&lt;/span&gt;&lt;/em&gt;. What we could do
is take some element of &lt;em&gt;a&lt;/em&gt; and compare it to every other element in
&lt;em&gt;a&lt;/em&gt; and partition the array into two pieces, one containing all the
elements less than the selected element and all elements greater than or equal
to the selected element. By transitivity, we would know that each element in the
first partition is less than all elements in the second partition even if we
don’t compare them directly. We could then partition the partitions again and
again, in Xeno-esque fashion, until the array only contains partitions of one
element in length. Since each partition will be less than its successor, the
array will be sorted.&lt;/p&gt;

&lt;p&gt;An algorithm that takes this very approach is the QuickSort, invented by C.A.R.
Hoare. A version of which looks like the following,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;public class Sorts {

  public static void QuickSort(int[] a) {
    QSort(a, 0, a.Length - 1);
  }

  private static void QSort(int[] a, int l, int r) {
    // Partition the array into two parts, one with all values
    // greater than a certain value in the array, one with all
    // less.
    int v = a[(l + r) / 2];
    int i = l;
    int j = r;
    while (i &amp;lt; j) {
      // Find the first values that don&apos;t belong and swap
      // them. Repeat until all values are in the right
      // partitions.
      while (a[i] &amp;lt; v)
        i++;
      while (a[j] &amp;gt; v)
        j--;
      if (i if (j &amp;gt; i)
        Swap(a, i, j);
      i++;
      j--;
    }
  }

  // Recursively partition the array until we can&apos;t any
  // longer. When this happens, the array is sorted.
  if (j &amp;gt; l)
    QSort(a, l, j);
  if (r &amp;gt; i)
    QSort(a, i, r);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If we get lucky and always guess the median value of each of the partitions we
will only perform O(N log N) comparisons. The worst thing we can do is select a
value that, given an &lt;em&gt;n&lt;/em&gt; length partition will give us two partitions one
of length &lt;em&gt;n-1&lt;/em&gt; and the other of length 1. This would put us back where
we started; with the selection sort. This would happen if we selected the first
(or last) element in the partition and the array was already sorted. To avoid
this, the above code selects an element from the middle of the partition, so, if
the array is sorted, it will be the median. If the array is in random order, it
is not better or worse than the first element.&lt;/p&gt;

&lt;p&gt;This, however, doesn’t guarantee we will get the elusive O(N log N)
performance. Many cases we will be less than O(N log N), those cases where we
don’t magically pick the median. In some pathological cases, we could arrive
very close to O(N&lt;span style=&quot;font-size: 8pt;&quot;&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/span&gt;). An example
of which is an array ordered 1..&lt;em&gt;m&lt;/em&gt;,1..&lt;em&gt;m&lt;/em&gt; where &lt;em&gt;m&lt;/em&gt; is
&lt;em&gt;n/2&lt;/em&gt;. Next we will examine an algorithm that guarantees O(N log N)
performance.
&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/2005/08/13/sorting-part-ii-quicksort</link>
                <guid>http://removingalldoubt.com/programming/2005/08/13/sorting-part-ii-quicksort</guid>
                <pubDate>2005-08-13T22:53:00+00:00</pubDate>
        </item>

        <item>
                <title>Sorting Part I: Selection Sort</title>
                <description>&lt;p&gt;When I run across a particularly fast elegant algorithm I often find myself
asking, “Why it is fast?” How can I make my algorithms fast like the author of
this one did? I have found that the answer usually is found in how the algorithm
manages data. How does the algorithm preserve work and prevent recalculating
information? To step back a bit, there are only three ways to make some code go
faster, find a way to make what it is doing faster, find a way to do less, find
some other time to do it. The most elegant algorithms do the second well; they
do less. For a few entries I want to look at various algorithms to see what
makes them fast (or not so fast).&lt;/p&gt;

&lt;p&gt;The first set of algorithms I want to investigate are some of the most
fundamental, sorting. The simplest sort to write is the selection sort. In C# it
looks like,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;static void SelectionSort(int[] a) {
  for (int i = 0; i &amp;lt; a.Length - 1; i++)
    for (int j = i + 1; j &amp;lt; a.Length; j++)
      if (a[i] &amp;gt; a[j])
  Swap(a, i, j);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Swap&lt;/code&gt; looks like,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;static void Swap&amp;lt;T&amp;gt;(T[] a, int i, int j) {
  T v = a[i];
  a[i] = a[j];
  a[j] = v;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This sort finds the smallest item in the list and puts it first, and then it
finds the second smallest and puts it second, etc. Even though it is very easy
to write and relatively easy to verify, it isn’t very fast. We could improve
this by in-lining the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Swap()&lt;/code&gt; method. Additionally, we can limit the number of
swaps to a.Length by only swapping the value we know is the smallest. A version
that includes both optimizations is,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;static void ImprovedSelectionSort(int[] a) {
  for (int i = 0; i &amp;lt; a.Length - 1; i++) {
    int min = i;
    for (int j = i + 1; j &amp;lt; a.Length; j++)
      if (a[min] &amp;gt; a[j])
        min = j;
    if (i == min) continue;
    int v = a[i];
    a[i] = a[min];
    a[min] = v;
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;These optimizations are examples of making what we are doing faster.
Unfortunately, we still with do on the order of O(N&lt;sup&gt;2&lt;/sup&gt;) comparisons
even though we reduced the number of moves to O(N) and removed any overhead of
doing a call in the middle of the loop. What we really want to do is less. To do
less we need to come up with information we are not using or information we are
throwing away.&lt;/p&gt;

&lt;p&gt;Since we are trying to optimize the number of comparisons (since we got the
moves down to O(N)) we should examine them closely. We need to come up with a
way to make each comparison do more. One thing we should notice is the above
algorithm totally ignores the transitive nature of a compare. That is if A &amp;gt; B
and B &amp;gt; C then A &amp;gt; C. If we know that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a[10] &amp;gt; a[11]&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a[11] &amp;gt; a[12]&lt;/code&gt; we
don’t need to physically do the comparison between &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a[10]&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a[12]&lt;/code&gt; to know
at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a[10] &amp;gt; a[12]&lt;/code&gt;, but how do we take advantage of that? In my next “sorting”
post I will investigate an algorithm that takes advantage of this.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/2005/08/11/sorting-part-i-selection-sort</link>
                <guid>http://removingalldoubt.com/programming/2005/08/11/sorting-part-i-selection-sort</guid>
                <pubDate>2005-08-11T23:59:00+00:00</pubDate>
        </item>

        <item>
                <title>The simplegeek effect</title>
                <description>&lt;p&gt;It is truly amazing! My blog has been up since Saturday and has been getting trickle of traffic. Get a mention on ChrisAn’s blog and watch it go! We should coin a new term:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;The simple&lt;span style=&quot;color: red&quot;&gt;geek&lt;/span&gt; effect&lt;/strong&gt; - the massive increase in blog readership because it was mentioned on www.simplegeek.com.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I sort of knew that would happen. Chris has a huge following. My passing comment about my blog to him this morning did have an ulterior motive. Thanks, Chris, for taking the hint and the nice words.&lt;/p&gt;

&lt;p&gt;[EDIT 2015-01-15: Links redacted]&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/blog/2005/08/05/the-simplegeek-effect</link>
                <guid>http://removingalldoubt.com/blog/2005/08/05/the-simplegeek-effect</guid>
                <pubDate>2005-08-05T21:32:00+00:00</pubDate>
        </item>

        <item>
                <title>My Life as a GUI Builder Builder</title>
                <description>&lt;p&gt;Frankly I am not sure how to respond to
Beware The GUI Builder. I have spent nearly all my
programming career building what this article describes
as GUI builder (I have always called them designers, but
that’s me, I hate the term GUI). It is not often you
come across an opinion piece that condemns nearly your
entire body of work as worse than worthless (unless you
are a lawyer or politician, that is). Let me see if I
can find myself and respond.&lt;/p&gt;

&lt;p&gt;At this point, if you haven’t already, read the
referenced article, if just for the clever poem.
My response will make more sense that way (well,
hopefully, anyway), and I apologize for my lack
of a poem (well, not really; be glad I didn’t try
one).&lt;/p&gt;

&lt;h2 id=&quot;major-failings-of-this-application-genre&quot;&gt;“major failings of this application genre”&lt;/h2&gt;
&lt;p&gt;Don’t be fooled (OK, you can be fooled if you want but
I don’t have to like it). This article is really just
a diatribe about the short-coming of a particular
unnamed Java based SWT GUI builder with passing
references to Swing builders. Many of the flaws of
“GUI Builders” it points out are not really faults
of the entire genre, but, rather, flaws in the
combination of Java, SWT, and this unnamed GUI
builder. I wish the author would have named it, at least
I could then verify the claims made in the piece, but
I will trudge ahead anyway, blissful in my ignorance.
Many of the flaws are common to a wide range of
designers, but not all, and certainly not mine, of
course ;-).&lt;/p&gt;

&lt;h2 id=&quot;reliance-on-static-code-analysis&quot;&gt;Reliance on Static Code Analysis&lt;/h2&gt;
&lt;p&gt;Not all designers rely on “static code analysis”.
Many, such as all the ones I have worked on, store
the result of the designer in as a serialized image of
the form. This is what code generating form designers
do as well, such as WinForms, but the format of the
serialization of the form is in code. I also have to
quibble with the description of how the loading of the
form is done, “By ‘static code analysis’, I refer to
those aspects of the code’s behavior that can be
determined from consideration of it’s structure
alone, without regard to it’s runtime behavior.”
I am not sure what this means. All the code-generating
form designers I am familiar with have a language
interpreter that can execute a subset of the
serialization language. The subset is usually restricted
from executing flow control logic, but not always as
in Visual dBASE. The reasons for these limitations is
to simplify translating gestures in the designer to
changes in the code that was generated. In an example
given in the article, it would be difficult for a
designer to figure out the mapping between the array,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;string[] colors = { &quot;Red&quot;, &quot;Orange&quot;, &quot;Yellow&quot;,  &quot;Green&quot;, &quot;Blue&quot;,
  &quot;Indigo&quot;, &quot;Violet&quot; };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;and the value passed to the setText() method in the for loop (which isn’t actually there in the example, but should be). But, in my mind, this is no excuse, if the language allows it the designer should support it. My solution in the past is to use a language that only allows what the designer supports. In the case of Delphi it was the .DFM file format, and currently, in my “new” job, it is XAML. In these cases, the language is designed in such a way that it is limited to supporting only that which the designer can figure out. This is one of the several reasons I don’t like the code generator style of serialization, programmers are tempted to futz with it and the will always use a feature of the serialization language the designer doesn’t support. The authors has a point, Java does not make very good serialization language (nor does any other general purpose language).&lt;/p&gt;

&lt;p&gt;Going back to the example used in this section (instead of skipping ahead as above), localization and handling dynamic values are typically where designers excel, not fall down. If you separate the logic of the form from the initialization state, you can have localizers just edit the initialization information, not your code (good reason not to code-gen, by the way). The article refers to resource bundles in “capable” GUI builders but it goes beyond that. The entire initialization state should be localizable, not just strings or what the developer might feel generous about allowing the poor localizer to fiddle with. The localizer might need to entirely reorient the form for a right-to-left language, reorganize the form in a more culturally friendly order, etc. If layout is done in code this is notoriously difficult, if not impossible, for localization teams to deal with. Most localization teams I have had the pleasure to interact with require the framework to have a form designer for these very reasons. Don’t layout a form in code. Just don’t. What assumption you have about how the form will look is culturally biased. Writing layout in code just makes the bias impossible to remove. Localization is a tough, thankless job. Don’t make it harder.&lt;/p&gt;

&lt;p&gt;As for dynamic values based on “male” and “female”, is easy to do a one-liner, but data binding is what is really needed here. A form should follow good MVC (Model-View-Controller) principles, where appropriate, and this example screams model-view. If you have a model that controls changes the salutation options, a good class library will have a data-bound version of a radio group that will update accordingly. This is not a GUI builder problem, it is a framework/data-binding problem.&lt;/p&gt;

&lt;h2 id=&quot;masking-understanding-and-inhibiting-learning&quot;&gt;Masking Understanding and Inhibiting Learning&lt;/h2&gt;
&lt;p&gt;This whole section is arguing against a false premise. No designer, ever, was written with the idea that the user would never have to code again. (Well, I have to admit, the marketing teams for them often have said that, but they were lying). Good designers are written to smooth and facilitate the transition between designing a form and writing code, allowing the user to do those things that are better done in a designer in the designer, and leave those things better done in code, done in code. They also where never intended be the only way you learn the framework (although they do help a lot) or take the place of good framework knowledge. Their job is to make using the framework easier, period. The author has a good point, most of your time is not spent in the designer, you spend most of you time in code. That is when the designer really shines, when you don’t have to use it much to get the job done.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;How do you visually represent these infinitely variable sets of constraints amongst components, and in such a way that they can be configured with mouse clicks and keyboard operations? The answer is ‘With great difficulty’.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is a criticism of frameworks, not designers. The framework should present a model where this is straight forward. The designer should make using it easy to use and easily discoverable. Also, remember, doing this in code might be easier for you, it just makes other people’s jobs impossible. You are not the only one that needs to control the layout of the form. A localizer might have a different idea of where the OK button should go. And, to be blunt, if your code has “infinitely variable sets of constraints” on anything, you should consider a different line of work.&lt;/p&gt;

&lt;h2 id=&quot;missed-opportunities-for-reuse&quot;&gt;Missed Opportunities for Reuse&lt;/h2&gt;
&lt;p&gt;I covered most of this above, use data-binding; understand data-binding; love data-binding. The second part, the address form example, is a job for nested forms or data templates. Each of the address should be a an instance of a single nested form or data template (depending on the framework you choose). This is where data-binding shines again since moving data in an out of controls is what data-binding does. This is a case where you shouldn’t need to write much code. The code is shared if you use the sharing techniques provided by the framework and the designer.&lt;/p&gt;

&lt;h2 id=&quot;dependency-upon-another-tool&quot;&gt;Dependency Upon Another Tool&lt;/h2&gt;
&lt;p&gt;Ideally the framework and the designer come from the same company and are rev’ed at the same time. If you get your designer from two different places, the author has a point. In my opinion, the designer and the framework should be developed together with full knowledge of each other. In Java, these are typically provided separately for reasons I will never understand.&lt;/p&gt;

&lt;h2 id=&quot;signs-that-your-predecessors-used-a-gui-builder&quot;&gt;Signs That Your Predecessors Used A GUI Builder&lt;/h2&gt;
&lt;p&gt;Most of these are either artifacts of code generation, covered above, or just signs that your predecessor didn’t leave because he wanted to. You can write bad code in any language and and using any tool. These are common problems, granted, but well written code is not common. As a retort, here are some signs that your predecessor didn’t use a GUI designer,&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The form looks hideously ugly; something only a Unix user would love.&lt;/li&gt;
  &lt;li&gt;You are considering rewriting it because you cannot change it without breaking something in the layout.&lt;/li&gt;
  &lt;li&gt;There are dead spots, places where the controls are obviously missing on the form. This happened because it was just easier to hide the control than figure out how to re-layout the form or because the predecessor forgot to save the “prototype”.&lt;/li&gt;
  &lt;li&gt;Your not sure the form you are looking at in the UI or what code creates it.&lt;/li&gt;
  &lt;li&gt;The localization team curses when your predecessor’s name is mentioned.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;OK, so mine are not as witty…&lt;/p&gt;

&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;They have some utility for mocking up interfaces. You can create static “snapshots” of your interface and show them to users, perhaps employing printouts of them as paper prototypes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is truly damning with faint praise. If this is all you use a designer for, go get a better designer. Seriously. They exist and hopefully you now know what to look for in order to find a good one.&lt;/p&gt;
</description>
                <link>http://removingalldoubt.com/programming/2005/07/30/my-life-as-a-gui-builder-builder</link>
                <guid>http://removingalldoubt.com/programming/2005/07/30/my-life-as-a-gui-builder-builder</guid>
                <pubDate>2005-07-30T23:57:00+00:00</pubDate>
        </item>

        <item>
                <title>Never Say Never</title>
                <description>&lt;p&gt;Even though I have been quoted as saying I would never have a blog,
welcome to my blog. It teaches me never to say never.&lt;/p&gt;

&lt;p&gt;If this blog looks familiar it is because I ripped off the implementation from
Chris Anderson. Like Chris, I will not restrict my self just to work life but
delve into other topics as well. I plan to keep them well separated so if you
don’t want my personal version of a “sump-pump” story, relax, you can filter it
out.&lt;/p&gt;

&lt;p&gt;I had visions of my first (non-introductory) post being earth shattering,
enlightening, and page-turning riveting example of clear technical writing about
an extremely interesting topic. Well, as it turns out, it is a rant. Oh, well;
maybe I can be enlightening later…&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;EDIT: 2015-01-15 Original links redacted.
www.simplegeek.com is no longer up.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
                <link>http://removingalldoubt.com/blog/2005/07/30/never-say-never</link>
                <guid>http://removingalldoubt.com/blog/2005/07/30/never-say-never</guid>
                <pubDate>2005-07-30T23:37:00+00:00</pubDate>
        </item>


</channel>
</rss>
