One-line lazy initialization

Is this too obscure?

public class MyObject
{
    private MyExpensiveObject _myExpensiveObject;

    public MyExpensiveObject ExpensiveObject
    {
        get { return _myExpensiveObject ??
                     (_myExpensiveObject = new MyExpensiveObject()); }
    }
}

RogueSharp

I just pushed a pet project of mine up to GitHub today. It’s a port of the original game of Rogue from C to C#. It’s not complete–it doesn’t save your progress and let you continue on later, but it is playable.

Now that the actual port is done, I plan on cleaning up the code. Like most software written in C in the 80s, it relies on way too many global variables, uses an abundance of switch statements on object types, and has no abstraction between the game logic and the UI. Since it’s on GitHub, that should leave a nice history of the refactorings I try to apply to it. Of course, before I can try to do any refactoring, I need to get some tests in place. That should be fun considering how much it relies on direct access to the curses library and a random number generator.

The repository is here. Feel free to fork it to help out if you like.

A Useful Fiddler Bookmarklet

IE 7 and up are hard-coded to bypass the proxy when the host is “localhost”. When Fiddler is running, you can change localhost to “ipv4.fiddler” to trick your browser into talking to the Fiddler proxy as documented here.

Since manually changing the host every time I want to test an ASP.NET or Silverlight application is so tedious, I wrote this little bookmarklet to do that work for me:

javascript:(function(){window.location=window.location.toString().replace('localhost', 'ipv4.fiddler');})()

Paste that (as a single line) into the URL box of a favorite in IE and you’re one click away from tracing the requests between your browser and your local development web server.

I’ve been doing a lot of Silverlight work recently so have found this especially useful to debug the interactions between the client and the server.

AutoRunner Downloads

I took some time tonight to throw together a build script for producing proper releases of AutoRunner. If you don’t feel like compiling it yourself, you can get a pre-compiled version here.

I used ILMerge to merge the Growl for Windows assemblies into the executable so it’s basically a single file now.

By the way, last night on Twitter, Steve Bohlen pointed me to this port of the original autotest to .NET. I took a look at the code and it was much more complex than I was looking for. It actually builds and runs your tests every time you save which is much more often than I want.

I had AutoRunner turned on all day at work today and was loving how it would catch me breaking the tests when I wasn’t expecting it to. =)

AutoRunner

I recently came across this awesome code kata performance by Corey Haines here.

Besides enjoying and learning from his actual performance, I was really impressed by his use of a Ruby tool called autotest. (I’m not sure, but it looks like it has become autospec.)

Not being a Ruby developer, I wanted the same thing for .NET. I did some searching, but my Google-fu failed me so I spent an hour hacking together my own.

The result is called AutoRunner (I know–way creative) and its source is available on GitHub.

If you want it, you have to download the source and compile it yourself for now. (UPDATE: You can now download it here!) Run it from your favorite console (PowerShell, right?) without any arguments to see what options it accepts.

AutoRunner is a little more general purpose than autotest/autospec is. Basically, it can run any executable when any file changes.

What I wanted it for was to run nunit-console.exe whenever my current tests assembly was rebuilt. To do that, I just invoke it with the right arguments.

If you have Growl for Windows running, it will send it a notification which is pure eye candy and not necessary to actually get it to run your tests.

It’s not a Visual Studio add-in. It’s just a plain old console application. Using Visual Studio’s External Tools feature, however, it’s almost as good as an add-in. I set up an external tool with the appropriate arguments and it’s good to go for all of my projects.

To set this up for yourself, you’d create a new external tool with its command set to the path where you built AutoRunner.exe and its arguments set to something like the following (I’ve separated the options on their own lines, but you wouldn’t do that in Visual Studio):

--target $(BinDir)\$(TargetName)$(TargetExt)
--exe C:\path\to\nunit-console.exe
--pass "$(TargetName) FTW!"
--fail "Oh noes! $(TargetName) is FAIL!"

You can use whatever test runner you like, of course. Please note that you must have a file from your tests project open or selected in Solution Explorer when you activate the tool or your AutoRunner instance will be watching the wrong DLL!

It doesn’t support plug-ins the way autotest does and most of its functionality is hard-coded for now. If anybody finds it useful, let me know and maybe we can work on improving it together.

SharpTestsEx

I’ve been using Fabio Maulo’s NUnitEx project to get fluent assertions on a personal project recently and have been loving it.

He then went and moved on to a new project called SharpTestsEx, which he intended to be framework-agnostic, but currently only worked with MSTest which prevented me from being able to use it (since I never saw a compelling reason to switch to MSTest).

Fabio was kind enough to let me make the changes necessary to remove the dependency on MSTest. A also made framework-specific versions of SharpTestsEx for MSTest, NUnit, and xUnit. The framework-specific versions aren’t really necessary, but they make the error messages a tiny bit prettier if you use the right one for the test framework you’re using.

You can read Fabio’s announcement here.

I plan on updating my personal project to using SharpTestsEx next.

I just realized I never posted about that project; I’ll have to get around to that soon. If you’re curious, it’s a behaviour-driven development framework for .NET called BehaveN. I’m using it at my work and we’re loving it.

Resurrecting Old Posts

Chris Kapilla reminded me in a comment that the World Wide Web is made of links. When I lost all the content in my weblog, I left a bunch of dangling links out there (like those in this Verify JavaScript with JSLint during build using Nant post).

So I went and found an old copy of my Verifying JavaScript with JSLint and Visual Studio post in Google’s cache and added it back (by hand!). I don’t expect I’ll be doing that for all of my old posts, but since somebody found that one interesting, I thought I’d put a little bit of effort into bringing it back.

Nesting NHibernate Mapping Files

The problem: Hiding Your hbm.xml files in Visual Studio (or not)

The solution: How to nest NHibernate mapping files in Visual Studio projects

I’m doing this in Visual Studio 2008. I don’t know if this is a “bug” or not, but it sure is annoying. The fix Timo posted does work, but it requires editing your .csproj file by hand which is both tedious and error-prone.

TestDriven.NET Keyboard Shortcuts

I’m so sick of Ctrl+Tab’ing back to my test file, finding the test I want to run, right-clicking it with the mouse, and selecting Run Test(s) or Test With.

How come nobody told me that TestDriven.NET came with keyboard shortcuts? They have to be manually mapped, but once I did that, I’ve found them indispensible.

  • Alt+1 : TestDriven.NET.RunTests
  • Alt+2 : TestDriven.NET.RerunWithDefault
  • Alt+3 : TestDriven.NET.Debugger
  • Alt+4 : TestDriven.NET.RerunWithDebugger

I especially love the two shortcuts that repeat the most recent test run for me. Yeah, I know you can right-click in any file and select Repeat Test Run, but if I want to alternate between running inside or outside the debugger, I have to go find the test again. With my shortcuts, I can just hit Alt+2 or Alt+4 no matter where I am.

Splitting CamelCase With Regular Expressions

On my new project, I needed to split a method name up into its constituent parts.

For Ruby programmers, this is easy: just split on underscores. For us .NET programmers, we need something a little fancier since we like to SquashOurMethodNamesTogetherLikeThis.

Here’s a little regular expression that can do exactly that:

(?<!^)(?=[A-Z])

That basically says to match right before every capital letter unless the capital letter is at the beginning of the string.

You can use the Regex.Split method to do the actual work of turning the method name into a string array:

Regex splitter = new Regex(@"(?<!^)(?=[A-Z])");
string[] words = splitter.Split(methodName);

My friend, Michael Kennedy, demoed in class recently how to use LINQPad to test regular expressions. It came in really handy while working on this:

linqpad-with-regex

I really need to click that “Activate Autocompletion” button so I can give Joseph Albahari the money he deserves for creating such a useful tool.

By the way, I used to think that “camel case” was for words like “camelCase” and “pascal case” was for words like “PascalCase”, but Wikipedia doesn’t make that distinction.