<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:blogChannel="http://backend.userland.com/blogChannelModule" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" 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:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" version="2.0">
  <channel>
    <title>jp.hamilton</title>
    <description>software craftsman</description>
    <link>http://www.jphamilton.net/</link>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <generator>BlogEngine.Net Syndication Generator 1.0.0.0 (http://dotnetblogengine.net/)</generator>
    <language>en-US</language>
    <blogChannel:blogRoll>http://www.jphamilton.net/opml.axd</blogChannel:blogRoll>
    <blogChannel:blink>http://www.dotnetblogengine.net/syndication.axd</blogChannel:blink>
    <dc:creator>J.P. Hamilton</dc:creator>
    <dc:title>jp.hamilton</dc:title>
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/JpHamilton" type="application/rss+xml" /><item>
      <title>Striving for Test Readability</title>
      <description>&lt;p&gt;I have heard many times that tests are like documentation for other developers. The argument is that formal documentation (word documents, spreadsheets, etc.) rarely stays in sync with the current state of an application. Therefore, unit tests act as a kind of up to date documentation (provided that the tests are all passing, of course). Tests tell developers who are not familiar with the code, how an application should work and what the expectations are. But consider this test:&lt;/p&gt;  &lt;p&gt;   &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:fe795e8f-7937-4f76-9c71-87dd17a3151a" class="wlWriterEditableSmartContent"&gt;&lt;pre name="code" class="c#"&gt;[TestFixture]
public class when_user_is_logging_in
{
    [Test]
    public void only_three_login_attempts_are_allowed()
    {
        ILoginView view = MockRepository.GenerateStub&amp;lt;ILoginView&amp;gt;();
        view.Stub(x =&amp;gt; x.User).Return("bad");
        view.Stub(x =&amp;gt; x.Password).Return("bad");

        ILoginService loginService = MockRepository.GenerateMock&amp;lt;ILoginService&amp;gt;();
        loginService.Expect(x =&amp;gt; x.UserExists("bad", "bad")).Return(false);

        LoginPresenter presenter = new LoginPresenter(loginService);
        presenter.Initialize(view);

        view.Raise(x =&amp;gt; x.TryLogin += null, this, EventArgs.Empty);
        view.Raise(x =&amp;gt; x.TryLogin += null, this, EventArgs.Empty);
        view.Raise(x =&amp;gt; x.TryLogin += null, this, EventArgs.Empty);

        view.AssertWasCalled(x =&amp;gt; x.Close());
    }
}&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;

&lt;p&gt;Right away it’s pretty clear that “&lt;em&gt;when a user is logging in, only three login attempts are allowed&lt;/em&gt;”. But things quickly go south after that. What mocking frameworks give us in convenience, they sure take away in readability. Regarding the above example - it’s written using Rhino Mocks 3.5. Many developers not familiar with mocking may be surprised to learn that this is actually the “good” syntax. Things used to be a heck of a lot worse. However, I still think that there are some things that could be done to clean these tests up, while still adhering to spirit of “tests that act like documentation”. My goal here, is to do this with as little ceremony as possible. I am not interested in writing a whole lot of new code in order to achieve readability. I just want to explore some simple ideas. When looking at the examples here, try to consider that there may be 10 or more additional tests which are not being shown. This will give you a better gauge as to whether the effort expended here is really worth it.&lt;/p&gt;

&lt;h3&gt;The Clean Up&lt;/h3&gt;

&lt;p&gt;ILoginView have User and Password properties that needs to return different values depending upon the test. To clean up the current implementation, we could do something over-the-top, like this:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:c7daba62-3f29-460b-ba5a-815263f714fe" class="wlWriterEditableSmartContent"&gt;&lt;pre name="code" class="c#"&gt;// add this method to test class
private static ILoginView CreateLoginView
{
    get
    {
        return MockRepository.GenerateStub&amp;lt;ILoginView&amp;gt;();
    }
}

// add internal class to hold extension methods
internal static class when_user_is_logging_in_extensions
{
    public static ILoginView WithUser(this ILoginView view, string user)
    {
        view.Stub(x =&amp;gt; x.User).Return(user);
        return view;
    }

    public static ILoginView AndPassword(this ILoginView view, string password)
    {
        view.Stub(x =&amp;gt; x.Password).Return(password);
        return view;
    }
}

// Now, I have a readable fluent interface
ILoginView view = CreateLoginView.WithUser("BADUSER").AndPassword("BADPASSWORD");
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Readable, sure, but seriously, that’s a lot of code. All of the convenience of using Rhino Mocks is lost. To compromise, I would probably just do something like this:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:945dc27a-51e7-4bf1-bc4a-40118507257a" class="wlWriterEditableSmartContent"&gt;&lt;pre name="code" class="c#"&gt;// in test class
private static ILoginView CreateLoginView(string withUser, string withPassword)
{
    ILoginView view = MockRepository.GenerateStub&amp;lt;ILoginView&amp;gt;();
    view.Stub(x =&amp;gt; x.User).Return(withUser);
    view.Stub(x =&amp;gt; x.Password).Return(withPassword);
    return view;
}

// in test
ILoginView view = CreateLoginView("BadUser", "BadPassword");
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here, I will let the method name, parameter names, and IntelliSense act as documentation. Like the last example, it requires a bit of code, too, but in this case ReSharper handles the Extract Method for me. Renaming CreateLoginView to create_login_view makes the IntelliSense story even better (in my opinion), but I will leave that alone for now.&lt;/p&gt;

&lt;p&gt;For the login service, I will use the same technique:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:f1c42c16-cf4e-4847-be89-621c3ad8351e" class="wlWriterEditableSmartContent"&gt;&lt;pre name="code" class="c#"&gt;// in test class
private static ILoginService CreateLoginService(bool shouldAuthenticate)
{
    var loginService = MockRepository.GenerateStub&amp;lt;ILoginService&amp;gt;();
    loginService.Expect(x =&amp;gt; x.UserExists("", ""))
        .IgnoreArguments()
        .Return(shouldAuthenticate);
    return loginService;
}

// in test
ILoginService loginService = CreateLoginService(false);
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To finish up, I will add a few extension methods by creating an internal static class at the bottom of my test file:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:c7fdcd6e-aad8-4a70-bbf4-864f3ffb615b" class="wlWriterEditableSmartContent"&gt;&lt;pre name="code" class="c#"&gt;internal static class when_user_is_logging_in_extensions
{
    public static void PushOkButton(this ILoginView view)
    {
        view.Raise(x =&amp;gt; x.TryLogin += null, view, EventArgs.Empty);
    }

    public static void ShouldBeClosed(this ILoginView view)
    {
        view.AssertWasCalled(x =&amp;gt; x.Close());
    }
}
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;With all of the changes in place, my test now looks like this:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:78df86cb-73dc-43b8-ad45-6d0c6ad0f735" class="wlWriterEditableSmartContent"&gt;&lt;pre name="code" class="c#"&gt;[Test]
public void only_three_login_attempts_are_allowed()
{
    ILoginView view = CreateLoginView("BadUser", "BadPassword");
    ILoginService loginService = CreateLoginService(false);

    LoginPresenter presenter = new LoginPresenter(loginService);
    presenter.Initialize(view);

    view.PushOkButton();
    view.PushOkButton();
    view.PushOkButton();
    
    view.ShouldBeClosed();
}
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;I am not advocating that anyone go to this trouble; context, preference, and discretion must be considered. However, I think there is great value in having readable tests. For some, the starting example might be perfectly readable. For others, not so much. As with everything, take what you like and leave the rest. &lt;/p&gt;</description>
      <link>http://www.jphamilton.net/post/Striving-for-Test-Readability.aspx</link>
      <author>jp</author>
      <comments>http://www.jphamilton.net/post/Striving-for-Test-Readability.aspx#comment</comments>
      <guid>http://www.jphamilton.net/post.aspx?id=1a135a5c-7433-47aa-993f-10adc93627f2</guid>
      <pubDate>Thu, 21 May 2009 03:58:26 -0700</pubDate>
      <dc:publisher>jp</dc:publisher>
      <pingback:server>http://www.jphamilton.net/pingback.axd</pingback:server>
      <pingback:target>http://www.jphamilton.net/post.aspx?id=1a135a5c-7433-47aa-993f-10adc93627f2</pingback:target>
      <slash:comments>5</slash:comments>
      <trackback:ping>http://www.jphamilton.net/trackback.axd?id=1a135a5c-7433-47aa-993f-10adc93627f2</trackback:ping>
      <wfw:comment>http://www.jphamilton.net/post/Striving-for-Test-Readability.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.jphamilton.net/syndication.axd?post=1a135a5c-7433-47aa-993f-10adc93627f2</wfw:commentRss>
    </item>
    <item>
      <title>Application Extensibility and Embedded Scripting</title>
      <description>&lt;p&gt;When we talk about application extensibility, we are often concerned with the structure of the code itself. Following known design principles and patterns and using techniques like inversion of control can be the foundation of a flexible, extensible, and testable code base. However, these concerns happen at compile time and are mostly static (plug-in style architectures being somewhat of an exception).&amp;#160; In many large applications with complex business rules, there is often a need to modify a program while it is running. Searching “business rules engine” will give you no shortage of hits, but I think these types of solutions are, for the most part, not flexible enough. In this developer’s humble opinion, nothing is more flexible than code. So, if you can stop pretending that your end users “might want to change a thing or two on their own”, then you can stop pretending that code is a bad thing (I say this facetiously:) &lt;em&gt;note to self: must write a rant about those who think code is bad&lt;/em&gt;).&lt;/p&gt;  &lt;p&gt;I have written about how easy it is to &lt;a href="http://www.jphamilton.net/post/Dynamic-Compilation.aspx"&gt;compile C# at runtime&lt;/a&gt; before. I used this technique on a former project to create a “formula engine” for calculating bid-related things like mark-up, transportation costs, discounts, and so forth. I should note that these formulas were never meant to be written by end users, but rather, another developer. During one of several design sessions for this part of the application, we were made aware that formulas required a lot of context and that they could change rather frequently. Dynamic compilation proved to be a good solution and the foundation for the whole thing was coded in two days. There is nothing better than showing your client, who thought a feature would take 2 months, a working prototype in 2 days.&lt;/p&gt;  &lt;h3&gt;Embedding a Scripting Language&lt;/h3&gt;  &lt;p&gt;As it turns out, it is even easier (and more flexible) to embed a &lt;a href="http://www.codeplex.com/dlr"&gt;DLR language&lt;/a&gt; into a .NET application. My personal favorite is IronPython, but the following steps should be similar for other DLR languages. The first thing you need to do is references to the following assemblies, which are included with the DLR:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;IronPython&lt;/li&gt;    &lt;li&gt;IronPython.Modules&lt;/li&gt;    &lt;li&gt;Microsoft.Scripting&lt;/li&gt;    &lt;li&gt;Microsoft.Scripting.Core&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Once you have added these references, it is relatively easy to start executing IronPython code. Here is a simple class that I used to test out some ideas for this article:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:f781762d-0254-4ebb-a736-94886bcc8109" class="wlWriterEditableSmartContent"&gt;&lt;pre name="code" class="c#"&gt;public class PyEval
{
    private ScriptEngine scriptEngine;

    public PyEval()
    {
        scriptEngine = Python.CreateEngine();
    }

    public object Execute(string yourPythonCode)
    {
        StringBuilder script = new StringBuilder();
        script.AppendLine("import clr");
        // additional imports would go here
        script.AppendLine(yourPythonCode);

        return scriptEngine.Execute(script.ToString());
    }
   
}&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;At the most basic level, the only thing required is an instance of ScriptEngine. Code can be executed by calling the Execute method. One item of interest is line #13. Here, the script is being modified before execution so that CLR extensions are available within the execution context. You will add additional imports to reference other parts of the .NET framework or assemblies within your application. The IronPython tutorial has plenty of examples on interacting with the CLR so I am going to skip that discussion.&lt;/p&gt;

&lt;h4&gt;Scope&lt;/h4&gt;

&lt;p&gt;Things get interesting when variables from your application are made available to the script. This is done using ScriptScope, and is a very straightforward process. Here is the class from the last example with a few modifications. You’ll have to use your imagination a bit to wash away the hard coding, but you should get the idea:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:aef807a6-a9c7-4e08-a210-03e253c192c6" class="wlWriterEditableSmartContent"&gt;&lt;pre name="code" class="c#"&gt;public class PyEval
{
    private ScriptEngine scriptEngine;
    private ScriptScope scriptScope;
    
    public PyEval()
    {
        scriptEngine = Python.CreateEngine();
        scriptScope = scriptEngine.CreateScope()
    }

    public object Execute(string yourPythonCode)
    {
        StringBuilder script = new StringBuilder();
        script.AppendLine("import clr");
        // additional imports would go here
        script.AppendLine(yourPythonCode);

        scriptScope.SetVariable("category", "drillbit");
        scriptScope.SetVariable("cost", 400);
        scriptScope.SetVariable("quantity", 1);
        
        return scriptEngine.Execute(script.ToString());
    }
    
}&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now, your script will have access to variables named category, cost, quantity:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:6cfc724d-adf7-4967-9f5b-61c49479d952" class="wlWriterEditableSmartContent"&gt;&lt;pre name="code" class="py"&gt;def CalculateDiscount():
	if (category == "drillbit") and (cost * quantity &amp;gt; 399):
		return (cost * quantity) * .90
	else:
		return cost * quantity

CalculateDiscount()&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;What is not apparent thus far is how this is all hooked together within an application. What I have done in the past (since I have been doing rich clients exclusively) is to embed the &lt;a href="http://www.icsharpcode.net/OpenSource/SD/"&gt;SharpDevelop&lt;/a&gt; editor into my application, which supports IronPython. Scripts are loaded and saved to a database. Fundamentally, this is all there is to it. &lt;/p&gt;

&lt;p&gt;The primary challenge is to define the parts of your application where you want to make scripting available. It’s always good to have a specific context and then write explicit code around that concept. One example that comes to mind is a pricing engine for an online retailer that calculates discounts based on certain conditions - which are constantly changing of course. This is a specific place in some application that has highly defined boundaries.&lt;/p&gt;

&lt;p&gt;Another idea that I am planning on exploring in the near future is extending this concept to DSL’s. I haven’t done any investigations yet, but I am wondering if there would be any benefits to creating a DSL using IronPython and then making that DSL the primary scripting tool for an application. If the focus was tight enough, I imagine it would be possible to create something simple enough for a power user to engage in:)&lt;/p&gt;</description>
      <link>http://www.jphamilton.net/post/Application-Extensibility-and-Embedded-Scripting.aspx</link>
      <author>jp</author>
      <comments>http://www.jphamilton.net/post/Application-Extensibility-and-Embedded-Scripting.aspx#comment</comments>
      <guid>http://www.jphamilton.net/post.aspx?id=dcbed073-1294-4289-9a98-6b3bb16dccf5</guid>
      <pubDate>Mon, 18 May 2009 04:04:45 -0700</pubDate>
      <dc:publisher>jp</dc:publisher>
      <pingback:server>http://www.jphamilton.net/pingback.axd</pingback:server>
      <pingback:target>http://www.jphamilton.net/post.aspx?id=dcbed073-1294-4289-9a98-6b3bb16dccf5</pingback:target>
      <slash:comments>1</slash:comments>
      <trackback:ping>http://www.jphamilton.net/trackback.axd?id=dcbed073-1294-4289-9a98-6b3bb16dccf5</trackback:ping>
      <wfw:comment>http://www.jphamilton.net/post/Application-Extensibility-and-Embedded-Scripting.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.jphamilton.net/syndication.axd?post=dcbed073-1294-4289-9a98-6b3bb16dccf5</wfw:commentRss>
    </item>
    <item>
      <title>Coding Dojo and Code Kata Resources</title>
      <description>&lt;h3&gt;Practice Material&lt;/h3&gt;
&lt;p&gt;
I am hosting regular coding dojo&amp;#39;s (group practice) and doing regular code kata&amp;#39;s (solo practice). So, I am always in the need of new material.
&lt;/p&gt;
&lt;p&gt;
Here is my current list of &amp;quot;go to&amp;quot; sites when I am looking for a programming challenge. If you know of any of others, please send them my way:
&lt;/p&gt;
&lt;p&gt;
1. &lt;a href=" http://uva.onlinejudge.org/"&gt;UVa Online Judge&lt;/a&gt;&lt;br /&gt;
2. &lt;a href="http://sites.google.com/site/tddproblems/"&gt;TDD Problems&lt;/a&gt;&lt;br /&gt;
3. &lt;a href="http://www.rubyquiz.com/"&gt;Ruby Quiz&lt;/a&gt;&lt;br /&gt;
4. &lt;a href="http://www.topcoder.com/tc"&gt;Top Coder&lt;/a&gt;&lt;br /&gt;
5. &lt;a href="http://projecteuler.net/"&gt;Project Euler&lt;/a&gt;&lt;br /&gt;
6. &lt;a href="http://codekata.pragprog.com/"&gt;CodeKata&lt;/a&gt;&lt;br /&gt;
7. &lt;a href="http://www.spoj.pl/"&gt;SPOJ&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;&lt;span class="Apple-style-span" style="font-size: 13px; font-weight: bold"&gt;Some other folks doing coding dojo&amp;#39;s&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
If you want to read about coding dojo&amp;#39;s, you are definitely going to want to check out these bloggers:
&lt;/p&gt;
&lt;p&gt;
1. &lt;a href="http://www.markhneedham.com/blog/category/coding-dojo/"&gt;Mark Needham&lt;/a&gt;&lt;br /&gt;
2. &lt;a href="http://isanchez.net/tag/coding-dojo/"&gt;Ivan Sanchez&lt;/a&gt;&lt;br /&gt;
3. &lt;a href="http://www.dtsato.com/blog/2008/10/21/source-of-problems-for-your-coding-dojo/"&gt;Danilo Sato&lt;/a&gt;
&lt;/p&gt;
</description>
      <link>http://www.jphamilton.net/post/Coding-Dojo-and-Code-Kata-Resources.aspx</link>
      <author>JP</author>
      <comments>http://www.jphamilton.net/post/Coding-Dojo-and-Code-Kata-Resources.aspx#comment</comments>
      <guid>http://www.jphamilton.net/post.aspx?id=5d39cc37-d254-4db0-8fd6-659c1fd36f33</guid>
      <pubDate>Fri, 01 May 2009 05:45:00 -0700</pubDate>
      <dc:publisher>JP</dc:publisher>
      <pingback:server>http://www.jphamilton.net/pingback.axd</pingback:server>
      <pingback:target>http://www.jphamilton.net/post.aspx?id=5d39cc37-d254-4db0-8fd6-659c1fd36f33</pingback:target>
      <slash:comments>2</slash:comments>
      <trackback:ping>http://www.jphamilton.net/trackback.axd?id=5d39cc37-d254-4db0-8fd6-659c1fd36f33</trackback:ping>
      <wfw:comment>http://www.jphamilton.net/post/Coding-Dojo-and-Code-Kata-Resources.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.jphamilton.net/syndication.axd?post=5d39cc37-d254-4db0-8fd6-659c1fd36f33</wfw:commentRss>
    </item>
  </channel>
</rss>
