ListExtensions

Posted January 20th 2010 by Matt Berther

I prefer to use the abstract IList<T> interface in my public APIs, rather than passing around concrete List<T> implementations. However, the one thing I’ve always missed was the awesome .ForEach syntax which was available on the List<T> implementation.

Today, I realized that I can use extension methods to add methods to interfaces.

public static class ListExtensions
{
    public static void ForEach<T>(this IList<T> source, Action<T> action)
    {
        foreach (T item in source)
            action(item);
    }
}

This now allows me to do things like:

public static void Main(string[] args)
{
    IList<string> items = GetItems();
    items.ForEach(Console.WriteLine);
}

private static IList<string> GetItems()
{
    return new List<string> { "abc", "123" };
}

I much prefer this syntax to the foreach construct.

The SwappableFile

Posted January 4th 2010 by Matt Berther

I was doing some testing on a javascript abstraction that we created earlier. In order to test that the validation was being properly called, I wanted to drop in a new target javascript and then use WatiN to execute and validate the various calls to that javascript.

I came up with (what I think is) a neat semantic for doing this. The SwappedFile implements the IDisposable interface, so you can wrap it up nicely in a using statement and make sure that the original file is reverted when completed.

class SwappedFile : IDisposable
{
    private readonly string originalFile;

    public SwappedFile(string originalFile, string newFile)
    {
        if (String.IsNullOrEmpty(originalFile)) throw new ArgumentNullException("originalFile");
        if (String.IsNullOrEmpty(newFile)) throw new ArgumentNullException("newFile");

        this.originalFile = originalFile;

        if (!File.Exists(String.Format("{0}-original", originalFile)))
        {
            File.Move(originalFile, String.Format("{0}-original", originalFile));
        }

        File.Copy(newFile, originalFile, true);
    }

    public void Dispose()
    {
        File.Delete(originalFile);
        File.Move(String.Format("{0}-original", originalFile), originalFile);
    }
}

Usage is simple:

using (new SwappedFile("original-file.txt", "new-file.txt"))
{
    // some operations using the new file
}

I kinda like this, plus it beats having all of my code cluttered up with file manipulation statements.

Problems running cucumber with nokogiri

Posted September 24th 2009 by Matt Berther

I installed cucumber last night and began trying to get some of the scenarios to work. One thing that kept plaguing me was the error message that kept coming up about using an old version of libxml2:

HI. You’re using libxml2 version 2.6.16 which is over 4 years old and has plenty of bugs. We suggest that for maximum HTML/XML parsing pleasure, you upgrade your version of libxml2 and re-install nokogiri. If you like using libxml2 version 2.6.16, but don’t like this warning, please define the constant I_KNOW_I_AM_USING_AN_OLD_AND_BUGGY_VERSION_OF_LIBXML2 before requiring nokogiri.

It turns out that this was relatively simply to correct on OSX Leopard by executing the following commands:

sudo gem uninstall nokogiri
curl -O ftp://xmlsoft.org/libxml2/libxml2-2.7.3.tar.gz
tar -zxvf libxml2-2.7.3.tar.gz
cd libxml2-2.7.3
./configure
make
sudo make install
sudo gem install nokogiri -- --with-xml2-include=/usr/local/include/libxml2 --with-xml2-lib=/usr/local/lib

Although I did not see any problems from using the old version, I did get frustrated by receiving the error all the time. Problem solved.

IIS7 and static files

Posted July 16th 2009 by Matt Berther

So, the other day I was trying to debug a problem with one of our web applications. I had gone through the steps of making the appropriate changes to my IIS configuration for this application, so that it utilized the Classic .NET App Pool and everything seemed to be running properly. That is, until I noticed that some javascript files and some images were not rendering properly.

As I utilized Firebug to try and understand what was going on, I saw that the javascript and image files were being sent with a content-length header of 0 bytes. Curious, what was causing this for some files but not all? I finally realized that the difference was that the files not being sent were static files.

Thoroughly befuddled now, I began to scour the web.config file to see if something had gotten committed that caused this to happen. This was not the case. Ultimately what I found was that Microsoft, in their infinite wisdom, decided that, when picking the default settings for installing IIS7 on Windows 7 RC, people probably did not have static content that they would like to publish on the web, and therefore left the static file handler turned off. Seriously! WTF?!

Anyways, to turn this on, go to Start | Control Panel | Programs and Features | Turn Windows Features On or Off | Internet Information Services | World Wide Web Services | Common Http Features and check the Static Content box.

Chuck Norris: The Programmer

Posted June 15th 2009 by Matt Berther

Saw this from codesqueeze earlier today… Hilarious stuff. :)

  1. When Chuck Norris throws exceptions, it’s across the room.
  2. All arrays Chuck Norris declares are of infinite size, because Chuck Norris knows no bounds.
  3. Chuck Norris doesn’t have disk latency because the hard drive knows to hurry the hell up.
  4. Chuck Norris writes code that optimizes itself.
  5. Chuck Norris can’t test for equality because he has no equal.
  6. Chuck Norris doesn’t need garbage collection because he doesn’t call .Dispose(), he calls .DropKick().
  7. Chuck Norris’s first program was kill -9.
  8. Chuck Norris burst the dot com bubble.
  9. All browsers support the hex definitions #chuck and #norris for the colors black and blue.
  10. MySpace actually isn’t your space, it’s Chuck’s (he just lets you use it).
  11. Chuck Norris can write infinite recursion functions — and have them return.
  12. Chuck Norris can solve the Towers of Hanoi in one move.
  13. The only pattern Chuck Norris knows is God Object.
  14. Chuck Norris finished World of Warcraft.
  15. Project managers never ask Chuck Norris for estimations — ever.
  16. Chuck Norris doesn’t use web standards as the web will conform to him.
  17. “It works on my machine” always holds true for Chuck Norris.
  18. Whiteboards are white because Chuck Norris scared them that way.
  19. Chuck Norris doesn’t do Burn Down charts, he does Smack Down charts.
  20. Chuck Norris can delete the Recycling Bin.
  21. Chuck Norris’s beard can type 140 wpm.
  22. Chuck Norris can unit test entire applications with a single assert.
  23. Chuck Norris doesn’t bug hunt as that signifies a probability of failure, he goes bug killing.
  24. Chuck Norris’s keyboard doesn’t have a Ctrl key because nothing controls Chuck Norris.
  25. When Chuck Norris is web surfing websites get the message “Warning: Internet Explorer has deemed this user to be malicious or dangerous. Proceed?”