<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Code Of The Week</title>
        <link>http://www.ayende.com/Blog/category/455.aspx</link>
        <description>Code Of The Week</description>
        <language>en-US</language>
        <copyright>Ayende Rahien</copyright>
        <managingEditor>Ayende@ayende.com</managingEditor>
        <generator>Subtext Version 1.9.3.51</generator>
        <item>
            <title>Elegant Code</title>
            <link>http://ayende.com/Blog/archive/2008/03/27/Elegant-Code.aspx</link>
            <description>&lt;p&gt;I just finished writing this, and I find it very pleasing to look at:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; EntitiesToRepositories
{
	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Register(
		IWindsorContainer windsorContainer,
		ISessionFactory sessionFactory,
		Type repository,
		&lt;span style="color: #0000ff"&gt;params&lt;/span&gt; Assembly[] assemblies
		)
	{
		&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(IRepository&amp;lt;&amp;gt;).IsAssignableFrom(repository))
			&lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ArgumentException("&lt;span style="color: #8b0000"&gt;Repository must be a type inheriting from IRepository&amp;lt;T&amp;gt;, " + &lt;br /&gt;                                    "and must be an open generic type. Sample: typeof(NHRepository&amp;lt;&amp;gt;).&lt;/span&gt;");

		&lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (IClassMetadata meta &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; sessionFactory.GetAllClassMetadata().Values)
		{
			Type mappedClass = meta.GetMappedClass(EntityMode.Poco);
			&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (mappedClass == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
				&lt;span style="color: #0000ff"&gt;continue&lt;/span&gt;;
			&lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (Type interfaceType &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; mappedClass.GetInterfaces())
			{
				&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(IsDefinedInAssemblies(interfaceType, assemblies)==&lt;span style="color: #0000ff"&gt;false&lt;/span&gt;)
					&lt;span style="color: #0000ff"&gt;continue&lt;/span&gt;;
				windsorContainer.Register(
					Component.For(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(IRepository&amp;lt;&amp;gt;).MakeGenericType(interfaceType))
						.ImplementedBy(repository.MakeGenericType(interfaceType))
						.CustomDependencies(Property.ForKey("&lt;span style="color: #8b0000"&gt;ConcreteType&lt;/span&gt;").Eq(mappedClass))
					);
			}
		}
	}

	&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; IsDefinedInAssemblies(Type type, Assembly[] assemblies)
	{
		&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; Array.IndexOf(assemblies, type.Assembly) != -1;
	}
}&lt;/pre&gt;&lt;/blockquote&gt;&lt;img src="http://ayende.com/Blog/aggbug/10124.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/03/27/Elegant-Code.aspx</guid>
            <pubDate>Thu, 27 Mar 2008 09:03:13 GMT</pubDate>
            <wfw:comment>http://ayende.com/Blog/comments/10124.aspx</wfw:comment>
            <comments>http://ayende.com/Blog/archive/2008/03/27/Elegant-Code.aspx#feedback</comments>
            <slash:comments>23</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10124.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Tricky Code</title>
            <link>http://ayende.com/Blog/archive/2007/12/31/Tricky-Code.aspx</link>
            <description>&lt;p&gt;Without running the code, what is the result of this masterpiece?&lt;/p&gt; &lt;blockquote&gt;&lt;pre&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Program
{
	&lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Main(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[] args)
	{
		DoSomething("&lt;span style="color: #8b0000"&gt;a&lt;/span&gt;","&lt;span style="color: #8b0000"&gt;b&lt;/span&gt;");
	}

	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; DoSomething&amp;lt;T&amp;gt;(IList&amp;lt;T&amp;gt; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;)
	{
		Console.WriteLine(&lt;span style="color: #0000ff"&gt;set&lt;/span&gt;.Count);
	}

	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; DoSomething&amp;lt;T&amp;gt;(&lt;span style="color: #0000ff"&gt;params&lt;/span&gt; T[] items)
	{
		List&amp;lt;T&amp;gt; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt; = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; List&amp;lt;T&amp;gt;();
		&lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (T t &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; items)
		{
			&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (t == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
				&lt;span style="color: #0000ff"&gt;continue&lt;/span&gt;;
			&lt;span style="color: #0000ff"&gt;set&lt;/span&gt;.Add(t);
		}
		DoSomething(&lt;span style="color: #0000ff"&gt;set&lt;/span&gt;);
	}
}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;It surprised the hell out of me until I figured out what was going on, then I was very amused. This code works exactly as it should be, to produce a very different result than the expected one.&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/9982.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2007/12/31/Tricky-Code.aspx</guid>
            <pubDate>Mon, 31 Dec 2007 11:31:07 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2007/12/31/Tricky-Code.aspx#feedback</comments>
            <slash:comments>14</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/9982.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Code that makes me laugh</title>
            <link>http://ayende.com/Blog/archive/2007/12/17/Code-that-makes-me-laugh.aspx</link>
            <description>&lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Codethatmakesmelaugh_1258E/image.png" atomicselection="true"&gt;&lt;img height="128" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Codethatmakesmelaugh_1258E/image_thumb.png" width="520" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/9955.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2007/12/17/Code-that-makes-me-laugh.aspx</guid>
            <pubDate>Mon, 17 Dec 2007 18:52:39 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2007/12/17/Code-that-makes-me-laugh.aspx#feedback</comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/9955.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Going into a new code base</title>
            <link>http://ayende.com/Blog/archive/2007/12/01/Going-into-a-new-code-base.aspx</link>
            <description>&lt;p&gt;I had a chance at DevTeach to just take an hour and start looking at a large code base. I made the claim several times in the past that no matter the quality of the developers and the quality of the code base, there is no way someone can just sit down on a code base and start working on that.&lt;/p&gt; &lt;p&gt;I tried to grok the code without help, and I failed. I could probably do that, but it would take several days. That was especially true since it was in Windows Forms, and that is not a field that I have done much at. Not knowing anything about the actual domain was a big issue as well.&lt;/p&gt; &lt;p&gt;After about 30 minutes of sitting down and going over the code with the developer, I got a whole new level of understanding about the application. How it works, how the structure is built. I look at a test and can work how this is used, etc.&lt;/p&gt; &lt;p&gt;I still have very little knowledge about that application works, but after 30 minutes, I am confident in my ability to both grok the code and work with it. In fact, after getting the introduction to the code base, I can say that this is probably one of the better code bases that I have seen.&lt;/p&gt; &lt;p&gt;One of the more interesting parts was that when I looked at each class, it was when I took a step back and looked at the &lt;em&gt;interactions&lt;/em&gt; it gotten really interesting.&lt;/p&gt; &lt;p&gt;It was a pleasure to read the code.&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/9884.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2007/12/01/Going-into-a-new-code-base.aspx</guid>
            <pubDate>Fri, 30 Nov 2007 23:46:52 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2007/12/01/Going-into-a-new-code-base.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/9884.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Pushing the limits</title>
            <link>http://ayende.com/Blog/archive/2007/09/17/Pushing-the-limits.aspx</link>
            <description>&lt;p&gt;Okay, I just finished writing this piece of code, I am not quite sure how to treat this. It is either elegantly concise or horribly opaque.&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Pushingthelimits_D5B9/image_1.png" atomicselection="true"&gt;&lt;img height="252" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Pushingthelimits_D5B9/image_thumb_1.png" width="749" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;I suspect both.&lt;/p&gt; &lt;p&gt;Somehow, you can probably wee that I have a C++ background, and even though the CLR doesn't give me the entire thing, you can still push it fairly far.&lt;/p&gt; &lt;p&gt;Of course, it still makes my head feel like this sometimes:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Pushingthelimits_D5B9/image_2.png" atomicselection="true"&gt;&lt;img height="240" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Pushingthelimits_D5B9/image_thumb_2.png" width="197" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/9673.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2007/09/17/Pushing-the-limits.aspx</guid>
            <pubDate>Mon, 17 Sep 2007 12:14:35 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2007/09/17/Pushing-the-limits.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/9673.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Can you figure it out? Functional caching</title>
            <link>http://ayende.com/Blog/archive/2007/08/04/Can-you-figure-it-out-Functional-caching.aspx</link>
            <description>&lt;p&gt;Okay, here is a challenge, &lt;em&gt;without running the code&lt;/em&gt;, what do you think should be the result of this program, and why?&lt;/p&gt;&lt;pre&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Program
{
	&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Main(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[] args)
	{
		Fetch&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt; method = Get;
		method = AddCaching(method);

		method();
		method();
		method();
	}

	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; Get()
	{
		Console.WriteLine("&lt;span style="color: #8b0000"&gt;Called&lt;/span&gt;");
		&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; 1;
	}

	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;delegate&lt;/span&gt; T Fetch&amp;lt;T&amp;gt;();

	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; Fetch&amp;lt;T&amp;gt; AddCaching&amp;lt;T&amp;gt;(Fetch&amp;lt;T&amp;gt; fetch)
	{
		T cachedObject = &lt;span style="color: #0000ff"&gt;default&lt;/span&gt;(T);
		&lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; isCached = &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;;
		&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;delegate&lt;/span&gt;
		{
			&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (isCached == &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;)
			{
				cachedObject = fetch();
				isCached = &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;;
			}
			&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; cachedObject;
		};
	}
}&lt;/pre&gt;&lt;img src="http://ayende.com/Blog/aggbug/9581.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2007/08/04/Can-you-figure-it-out-Functional-caching.aspx</guid>
            <pubDate>Sat, 04 Aug 2007 12:44:24 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2007/08/04/Can-you-figure-it-out-Functional-caching.aspx#feedback</comments>
            <slash:comments>13</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/9581.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Did you know: Find out if an exception was thrown from a finally block!</title>
            <link>http://ayende.com/Blog/archive/2007/06/20/Did-you-know-Find-out-if-an-exception-was-thrown.aspx</link>
            <description>&lt;p&gt;This is a &lt;strong&gt;big biggie&lt;/strong&gt; for me, because it enables a much nicer syntax for a lot of stuff. But first, let us show this:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ExceptionDetector())
{
	&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Random().Next(1,10)%2 == 0)
          &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Exception();
}&lt;/pre&gt;
&lt;p&gt;How can you tell, from the ExceptionDetector, if an exception was thrown or not? Well, conventional wisdom, and what &lt;strong&gt;I&lt;/strong&gt; thought about until 15 minutes ago, says that you can't. I want to thank Daniel Fortunov, for teaching me this trick:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; ExceptionDetector : IDisposable
{
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Dispose()
    {
        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (Marshal.GetExceptionCode()==0)
            Console.WriteLine("&lt;span style="color: #8b0000"&gt;Completed Successfully!&lt;/span&gt;");
        &lt;span style="color: #0000ff"&gt;else&lt;/span&gt;
            Console.WriteLine("&lt;span style="color: #8b0000"&gt;Exception!&lt;/span&gt;");
    }
}
&lt;/pre&gt;Amazing!&lt;img src="http://ayende.com/Blog/aggbug/9481.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2007/06/20/Did-you-know-Find-out-if-an-exception-was-thrown.aspx</guid>
            <pubDate>Tue, 19 Jun 2007 21:47:53 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2007/06/20/Did-you-know-Find-out-if-an-exception-was-thrown.aspx#feedback</comments>
            <slash:comments>18</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/9481.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Understanding Bad Code</title>
            <link>http://ayende.com/Blog/archive/2007/06/18/Understanding-Bad-Code.aspx</link>
            <description>&lt;p&gt;&lt;a href="http://weblogs.asp.net/fbouma/"&gt;Frans&lt;/a&gt;' &lt;a href="http://weblogs.asp.net/fbouma/archive/2007/06/18/maintainable-software-why-you-can-t-live-without-proper-solid-documentation.aspx"&gt;contribution&lt;/a&gt; to the conversation about maintainable code deserve its own post, but I would like to mention this part in particular:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;you will not [understand the code]. Not now, not ever. And not only you, but everyone out there who writes code, thus that includes me as well, will &lt;i&gt;not &lt;/i&gt;be able to read code and understand it immediately.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;You know what? That is &lt;em&gt;not&lt;/em&gt; limited to bad code. I had hard time grokking good code bases, simply because of their size and complexity (NHibernate and Windsor comes to mind). Other code bases are as large, but they have easier approachability, probably because they are dealing with less complex domain an tend to have a wide coverage rather than deep (MonoRail comes to mind).&lt;/p&gt; &lt;p&gt;A while ago I was involved with an effort to migrate an ancient system to SQL Server 2005. The system compromised of over 100,000 lines of code, spread over some thousands of files scattered randomly in a case sensitive file system (you can guess why this is significant). The code base was about 85% SQL and 15% bash shell scripts. The database in question was a core system and contained slightly over 4,000 tables. One of the core tables was called tmp1_PlcyDma and was used to do business critical processing. That code base took data driven code generation to a level I have never seen before. I gave up trying to track down 7(!) levels of code-&amp;gt;generating code-&amp;gt;execute code-&amp;gt;generating code-&amp;gt;rinse-&amp;gt;repeat&lt;/p&gt; &lt;p&gt;To say that the code base was bad is quite an understatement. To mention that the only place where I could run the code was by using a telnet console into a test environment that was &lt;em&gt;not&lt;/em&gt; identical to production is only the start. I could mention no debugging, runtime of ~5 hours, test time of ~3 hours, etc. The code grew organically over a ten years period and you could track the developer progress from merely annoying to criminally insane (he invented his own group by construct, using triple nested cursors and syntax so obscure that even the DBA that worked with the system for the last 5 years had no idea what was going on).&lt;/p&gt; &lt;p&gt;Perhaps the thing that I remember most from this project that we had a bug that kept two people hunting after it for &lt;em&gt;three weeks.&lt;/em&gt; The issue was a missing ';'. Oh, and the criteria for success in this project was successful migration, with bug-per-bug compatibility, and no one really knew what it did, including the authors(!).&lt;/p&gt; &lt;p&gt;But, you know what, after a month or so of looking at the code, it got to the point where I could look at something like pc_cl_mn.sql and know that it would contains the monthly policy calculation, and that this piece of code was doing joins manually via cursors again, that plcy_tr_tmp.sql was the "indexes priming" script, etc, etc.&lt;/p&gt; &lt;p&gt;The code was still a horror, but once you understood that the authors of this code had a... "special" way of looking at databases, you got to the point where you could get the point of the code in an hour instead of a day, and then move that to a saner approach.&lt;/p&gt; &lt;p&gt;So, what does this horrifying story has to do with Frans' point above?&lt;/p&gt; &lt;p&gt;The premise that you can read and understand code immediately is highly dependant on what you are familiar with. I know of &lt;em&gt;no one&lt;/em&gt; that can just sit in front of an unfamiliar  code base and start producing value within the first ten minutes. But on a good code base, you should be able to be able to start producing value &lt;em&gt;very quickly.&lt;/em&gt;&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/9476.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2007/06/18/Understanding-Bad-Code.aspx</guid>
            <pubDate>Mon, 18 Jun 2007 20:36:52 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2007/06/18/Understanding-Bad-Code.aspx#feedback</comments>
            <slash:comments>4</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/9476.aspx</wfw:commentRss>
        </item>
        <item>
            <title>rails:ConditionalRender - still trying to get WebForms to work right</title>
            <link>http://ayende.com/Blog/archive/2007/06/06/railsConditionalRender--still-trying-to-get-WebForms-to-work-right.aspx</link>
            <description>&lt;p&gt;&lt;/p&gt; &lt;p&gt;Well, as it turns out, there are &lt;em&gt;some&lt;/em&gt; things that you can do to make WebForms more palatable. Smart use of ITemplate and some additional magic can take you a fairly long way. I am still severely limited (splitting behavior between markup &amp;amp; code behind is extremely annoying) in what I can do, but it  is good to know that it is possible:&lt;/p&gt; &lt;div style="border-right: rgb(153,153,153) 1px solid; padding-right: 4px; border-top: rgb(153,153,153) 1px solid; padding-left: 4px; padding-bottom: 4px; border-left: rgb(153,153,153) 1px solid; width: 100%; padding-top: 4px; border-bottom: rgb(153,153,153) 1px solid; background-color: rgb(255,255,225)"&gt;&lt;pre&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #c71585"&gt;rails&lt;/span&gt;:&lt;span style="color: #800000"&gt;ConditionalRenderer&lt;/span&gt; &lt;span style="color: #ff0000"&gt;runat&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;"server"&lt;/span&gt; 
    &lt;span style="color: #ff0000"&gt;Condition&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;"&amp;lt;%# ((Policy)Container.DataItem).Expired %&amp;gt;"&lt;/span&gt;
    &lt;span style="color: #ff0000"&gt;DataItem&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;"&amp;lt;%# Container.DataItem %&amp;gt;"&lt;/span&gt; &lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;WhenTrue&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
        Policy has expired!
    &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;WhenTrue&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;    
    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;WhenFalse&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
        Policy is valid until &lt;span style="color: black; background-color: #ffff00"&gt;&amp;lt;%&lt;/span&gt;# Eval("ExpiryDate") &lt;span style="color: black; background-color: #ffff00"&gt;%&amp;gt;&lt;/span&gt;
    &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;WhenFalse&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #c71585"&gt;rails&lt;/span&gt;:&lt;span style="color: #800000"&gt;ConditionalRenderer&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class="CodeFormatContainer"&gt; &lt;/div&gt;
&lt;div class="CodeFormatContainer"&gt;I am using it inside a repeater to keep the presentation logic in a single place.&lt;/div&gt;
&lt;div class="CodeFormatContainer"&gt; &lt;/div&gt;
&lt;div class="CodeFormatContainer"&gt;The code to make this happen is something like this (typed from memory, you need about 80 lines to get it right).&lt;/div&gt;
&lt;div class="CodeFormatContainer"&gt; &lt;/div&gt;
&lt;div style="border-right: rgb(153,153,153) 1px solid; padding-right: 4px; border-top: rgb(153,153,153) 1px solid; padding-left: 4px; padding-bottom: 4px; border-left: rgb(153,153,153) 1px solid; width: 100%; padding-top: 4px; border-bottom: rgb(153,153,153) 1px solid; background-color: rgb(255,255,225)"&gt;&lt;pre&gt;[ParseChildren(&lt;span style="color: #0000ff"&gt;true&lt;/span&gt;)]
&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; ConditionalRenderer : Control, INamingContainer
{
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; Condition { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt; { .. } &lt;span style="color: #0000ff"&gt;set&lt;/span&gt; { .. } }

    [Templatecontainer(ConditionalRenderer)]
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ITemplate WhenTrue { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt; { .. } &lt;span style="color: #0000ff"&gt;set&lt;/span&gt; { .. } } 
    [Templatecontainer(ConditionalRenderer)]
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ITemplate WhenFalse { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt; { .. } &lt;span style="color: #0000ff"&gt;set&lt;/span&gt; { .. } }

    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; DataItem { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt; { .. } &lt;span style="color: #0000ff"&gt;set&lt;/span&gt; { .. } }
    
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; EnsureChildControls()
    {
        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (Condition)
            WhenTrue.InstansiateIn(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;);
        &lt;span style="color: #0000ff"&gt;else&lt;/span&gt;
            WhenFalse.InstansiateIn(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;);
    }
}&lt;/pre&gt;&lt;/div&gt;&lt;img src="http://ayende.com/Blog/aggbug/9414.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2007/06/06/railsConditionalRender--still-trying-to-get-WebForms-to-work-right.aspx</guid>
            <pubDate>Wed, 06 Jun 2007 19:51:40 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2007/06/06/railsConditionalRender--still-trying-to-get-WebForms-to-work-right.aspx#feedback</comments>
            <slash:comments>6</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/9414.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Unmaintainable Code</title>
            <link>http://ayende.com/Blog/archive/2007/05/21/Unmaintainable-Code.aspx</link>
            <description>&lt;p&gt;    I have several ways of categorizing code. Production quality, test, demo, and utilities. When I am talking about &lt;a href="https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/trunk/Utilities"&gt;utilities&lt;/a&gt;, I am talking about little programs that I use to do all sorts of small tasks. There I rarely worry about maintainability, and today there was a gem &lt;a href="https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/trunk/Utilities/BuildEditor/BuildEditor/BuildEditor.cs"&gt;here&lt;/a&gt;:&lt;/p&gt;&lt;div style="BORDER-RIGHT: #999999 1px solid; PADDING-RIGHT: 4px; BORDER-TOP: #999999 1px solid; PADDING-LEFT: 4px; PADDING-BOTTOM: 4px; BORDER-LEFT: #999999 1px solid; WIDTH: 100%; PADDING-TOP: 4px; BORDER-BOTTOM: #999999 1px solid; BACKGROUND-COLOR: #ffffe1"&gt;    &lt;p&gt;        &lt;span style="COLOR: #000000"&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;delegate&lt;/span&gt; Control BuildSpecialCase(TextBox textbox);&lt;/span&gt;    &lt;/p&gt;    &lt;p&gt;        &lt;span style="COLOR: #000000"&gt;&lt;span style="COLOR: #0000ff"&gt;private&lt;/span&gt; BuildSpecialCase GetSpecialCaseControl(&lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt; name)&lt;br /&gt;        {&lt;br /&gt;         &lt;span style="COLOR: #0000ff"&gt;if&lt;/span&gt; (name.IndexOf(&lt;span style="COLOR: #848284"&gt;"ConnectionString"&lt;/span&gt;, StringComparison.InvariantCultureIgnoreCase) != -1)&lt;br /&gt;         {&lt;br /&gt;          &lt;span style="COLOR: #0000ff"&gt;return&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;delegate&lt;/span&gt;(TextBox t)&lt;br /&gt;          {&lt;br /&gt;           Button change = &lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt; Button();&lt;br /&gt;           change.Click += &lt;span style="COLOR: #0000ff"&gt;delegate&lt;/span&gt; { t.Text = PromptForConnectionString(t.Text); };&lt;br /&gt;           change.Dock = DockStyle.Right;&lt;br /&gt;           change.Text = &lt;span style="COLOR: #848284"&gt;"..."&lt;/span&gt;;&lt;br /&gt;           change.Width = 30;&lt;br /&gt;           &lt;span style="COLOR: #0000ff"&gt;return&lt;/span&gt; change;&lt;br /&gt;          };&lt;br /&gt;         }&lt;br /&gt;         &lt;span style="COLOR: #0000ff"&gt;if&lt;/span&gt; (name.IndexOf(&lt;span style="COLOR: #848284"&gt;"File"&lt;/span&gt;, StringComparison.InvariantCultureIgnoreCase) != -1)&lt;br /&gt;         {&lt;br /&gt;          &lt;span style="COLOR: #0000ff"&gt;return&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;delegate&lt;/span&gt;(TextBox t)&lt;br /&gt;          {&lt;br /&gt;           Button change = &lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt; Button();&lt;br /&gt;           change.Click += &lt;span style="COLOR: #0000ff"&gt;delegate&lt;/span&gt;        &lt;br /&gt;           {&lt;br /&gt;            OpenFileDialog dialog = &lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt; OpenFileDialog();&lt;br /&gt;            dialog.Multiselect = &lt;span style="COLOR: #0000ff"&gt;false&lt;/span&gt;;&lt;br /&gt;            dialog.FileName = t.Text;&lt;br /&gt;            &lt;span style="COLOR: #0000ff"&gt;if&lt;/span&gt; (dialog.ShowDialog(&lt;span style="COLOR: #0000ff"&gt;this&lt;/span&gt;) == DialogResult.OK)&lt;br /&gt;            {&lt;br /&gt;             t.Text = dialog.FileName;&lt;br /&gt;            }&lt;br /&gt;           };&lt;br /&gt;           change.Dock = DockStyle.Right;&lt;br /&gt;           change.Text = &lt;span style="COLOR: #848284"&gt;"..."&lt;/span&gt;;&lt;br /&gt;           change.Width = 30;&lt;br /&gt;           &lt;span style="COLOR: #0000ff"&gt;return&lt;/span&gt; change;&lt;br /&gt;          };&lt;br /&gt;         }&lt;br /&gt;         &lt;span style="COLOR: #0000ff"&gt;if&lt;/span&gt; (name.IndexOf(&lt;span style="COLOR: #848284"&gt;"Folder"&lt;/span&gt;, StringComparison.InvariantCultureIgnoreCase) != -1 ||&lt;br /&gt;          name.IndexOf(&lt;span style="COLOR: #848284"&gt;"Dir"&lt;/span&gt;, StringComparison.InvariantCultureIgnoreCase) != -1)&lt;br /&gt;         {&lt;br /&gt;          &lt;span style="COLOR: #0000ff"&gt;return&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;delegate&lt;/span&gt;(TextBox t)&lt;br /&gt;          {&lt;br /&gt;           Button change = &lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt; Button();&lt;br /&gt;           change.Click += &lt;span style="COLOR: #0000ff"&gt;delegate&lt;/span&gt;        &lt;br /&gt;           {&lt;br /&gt;            FolderBrowserDialog dialog = &lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt; FolderBrowserDialog();&lt;br /&gt;            dialog.SelectedPath = t.Text;&lt;br /&gt;            &lt;span style="COLOR: #0000ff"&gt;if&lt;/span&gt; (dialog.ShowDialog(&lt;span style="COLOR: #0000ff"&gt;this&lt;/span&gt;) == DialogResult.OK)&lt;br /&gt;            {&lt;br /&gt;             t.Text = dialog.SelectedPath;&lt;br /&gt;            }&lt;br /&gt;           };&lt;br /&gt;           change.Dock = DockStyle.Right;&lt;br /&gt;           change.Text = &lt;span style="COLOR: #848284"&gt;"..."&lt;/span&gt;;&lt;br /&gt;           change.Width = 30;&lt;br /&gt;           &lt;span style="COLOR: #0000ff"&gt;return&lt;/span&gt; change;&lt;br /&gt;          };&lt;br /&gt;         }&lt;br /&gt;         &lt;span style="COLOR: #0000ff"&gt;return&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;null&lt;/span&gt;;&lt;br /&gt;        }&lt;br /&gt;        &lt;/span&gt;    &lt;/p&gt;&lt;/div&gt;&lt;p&gt;    Can you figure out what is going on? &lt;/p&gt;&lt;p&gt;    As long as we are talking about unmaintainable code, here an example from code meant for production that I just wrote:&lt;/p&gt;&lt;div style="BORDER-RIGHT: #999999 1px solid; PADDING-RIGHT: 4px; BORDER-TOP: #999999 1px solid; PADDING-LEFT: 4px; PADDING-BOTTOM: 4px; BORDER-LEFT: #999999 1px solid; WIDTH: 100%; PADDING-TOP: 4px; BORDER-BOTTOM: #999999 1px solid; BACKGROUND-COLOR: #ffffe1"&gt;&lt;span style="COLOR: #000000"&gt;IDictionary&amp;lt;Customer, IDictionary&amp;lt;&lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt;, IList&amp;lt;Policy&amp;gt;&amp;gt;&amp;gt; namedPoliciesByCustomers = &lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt; ...;&lt;/span&gt;&lt;/div&gt;&lt;p&gt;    But for that I have tests :-)&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/9315.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2007/05/21/Unmaintainable-Code.aspx</guid>
            <pubDate>Mon, 21 May 2007 15:08:24 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2007/05/21/Unmaintainable-Code.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/9315.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>