I Love it When Technical Book Authors Have a Sense of Humor

I am going through Pro ASP.NET MVC 3 Framework by Adam Freeman and Steven Sanderson (@StevenSanderson) (Apress). Enjoying it so far – good technical writing, good level of detail mixed with useful examples of different implementation options.

And most importantly, they have a good sense of humor. From Page 381 (my highlights):

Quote from Pro ASP.NET MVC 3 (Sanderson & Freeman), page 381

If you don’t want to click on the link, they are talking about best practices for url schemas using MVC, and give an example of a link to Amazon as something not to do (I realize the irony of my linking to Amazon above). They then include in an aside:

Note To be very clear, we have only the highest respect for Amazon, who sells more of our books than everyone else combined. We know for a fact that each and every member of the Amazon team is a strikingly intelligent and beautiful person. Not one of them would be so petty as to stop selling our books over something so minor as criticism of their URL format. We love amazon. We adore Amazon. We just wish they would fix their URLs

.Another good one: The authors are talking about using MVC to create a REST API, where the same action name in a given controller can be overridden to handle HTTP Get/Post/Delete requests (page 476, my emphasis):

Now each StaffMember entity in our application can be uniquely addressed using a URL of the form Staff/123, and a client can use the GET, POST, and DELETE HTTP methods to perform operations on those addresses. This is called a RESTful API, except by people who claim to be experts in REST, who will tell you that nothing is truly RESTful except their own code. (We joke—sort of).

Gotta love it.

Crucible Code Review Wishlist

I have been trying out Crucible Code Review software from Atlassian (halfway through the trial) with a number of teams in my department. The competition in this area is pretty sparse, and each one has its other issues:

  • Kiln only works for Mercurial (which we may move to some day, but for now we are using SVN so we need a solution that works with that as also)
  • Review Board seems to be very popular, but just looking at the list of the dependencies gives me indigestion when considering an install on our Windows Server environment.
  • Code Collaborator is way too expensive without giving much extra (50 users in Crucible = $2200, 50 named users in Code Collaborator = $24,450)
  • Code Review Tool doesn’t have a very professional looking site, hardly any documentation and no plugin potential and no Mercurial support
  • Specatare‘s and Parasoft‘s websites are even sparser and gives almost no info (aside from a few screenshots) – not enough to make me spend the time trying it out.

On the other hand, thus far Crucible has delivered on the main functionality: establishing a medium for easily performing code reviews. The price is very fair and there is very decent documentation. We will probably go with it, just for those reasons alone.

That said, I have a number of issues with Crucible – posting them here in the hopes that someone from Atlassian will see this and can do something about some or all of them. Continue reading

Horrible Page Design at JPost.com

I just loaded up a link at jpost.com talking about how the Rami Levy supermarket chain predicts that by 2012 20% of food purchases in Israel will be online. However, when I loaded the link, it took me a minute to actually find the content. Can you see the problem here?

To make it a little bit clearer, I have added highlights – Ads are in yellow, In-site navigation is in green, and the content is in blue. The content occupies a tiny strip down the center of the page (maybe 30-40% of the total width of the page). This is surrounded on all sides several layers: Continue reading

Clearing out a Mysterious Table Lock (LCK_M_S) in Sql Server 2008

A user reported today that they were trying to query a table with 181 records and the output stopped at 178 and just hung there. I tried to replicate this and the same thing happened – also couldn’t run a Select (*) on the table. So I pulled up the Activity Monitor for the server and observed that there was only one query in process – the hung query. It listed the process as being suspended with a Wait Type of LCK_M_S. Some quick searching told me that the task was waiting to acquire a shared lock. However, it was not obvious what was locking the table. What to do?

After some more searching, I found two more posts that gave advice on what to do in the given scenario:

Based on these, I did the following:

    1. Ran Exec sp_who2 through Sql Management Studio. This gave me a list of all current processes. There was only one open process on this database at the time, so it was the likely culprit.
    2. Next, ran the code in the Sql Server Locks article above to show the process that was locking my table. Bingo – process 52.
USE DBNAME; GO;

DECLARE @dbid INT ; --what's the dbid for DBNAME?
SET @dbid = DB_ID() ;

DECLARE @objectid INT ; --what's objectid for our demo table?
SET @objectid = OBJECT_ID(N'TABLENAME') ;

--look at locking in the DB
SELECT  resource_type
       ,resource_database_id
       ,resource_associated_entity_id
       ,request_mode
       ,request_type
       ,request_session_id
FROM    sys.dm_tran_locks
WHERE   resource_database_id = @dbid ;

--limit the results to only the offending table
SELECT  *
FROM    sys.dm_tran_locks
WHERE   resource_database_id = @dbid AND
        resource_associated_entity_id = @objectid ;

After I had confirmed the identity of the process, I ran Kill 52 to kill the offending process. Everything immediately started working again with the table.

(After the fact, went back through the code with my team and found the spot where someone was running db.Connection.BeginTransation() on a data context that already had an open transaction, causing an error: “SqlConnection does not support parallel transactions“. And to make things worse, this was done outside of the try/catch/finally structure that ensured that any open transactions would be committed. Oy.)

As Pinal Dave writes:

As mentioned earlier, if you kill something important on your production server, there’s a great possibility that you’ll face some serious integrity issues, so I there’s no way I advise use this method. As the title goes, this is a dirty solution so you must utilize this only if you are confident.

It is important to keep that in mind. But that said, this technique for identifying and killing a hung transaction can definitely come in handy. (And be sure to backup your DB and transaction logs regularly!)

Web Surfing Helps at Work

I have been claiming this for years. Web Surfing Helps at Work, Study Says:

According to a new study, Web browsing can actually refresh tired workers and enhance their productivity, compared to other activities such as making personal calls, texts or emails, let alone working straight through with no rest at all….Because Web-surfing can aid productivity, the researchers caution employers against over-restricting workers’ Web access. They recommend that managers allow time for limited personal Web browsing “since it has a salubrious impact on employees’ productivity,” while limiting access to personal emails.

It has happened to me many times that I am stuck on a problem and just come up with a solution. Then I spend a few minutes “wasting time” on some website and the answer comes to me. Nice to see it substantiated in a study.

Numeric String Sort in C#

Scenario: you have a List<string> collection where the contents could be alpha, numeric, or alphanumeric. If you just sort the collection using the built-in Sort() method, it will use string.CompareTo(), treating each item in the collection as a string. Thus a collection with the following values ["1", "44", "22", 4", "5"] will end up being sorted as ["1", "22", "4", "44", "5"] – even though you might want the items to be treated as numbers (in which case they would be sorted as ["1", "4", "5", "22", "44"].

The solution to this is to use a custom comparer class. I put the following together:

public class NumericStringSort : IComparer {

    int IComparer.Compare(string a, string b) {
        decimal aDec;
        decimal bDec;
        if (decimal.TryParse(a, out aDec) && decimal.TryParse(b, out bDec)) {
            return aDec.CompareTo(bDec);
        } else {
            return a.CompareTo(b);
        }
    }

    public static IComparer NumericStringSorter() {
        return (IComparer) new NumericStringSort();
    }
}

As you can see, the class implements the IComparer interface. If both of the strings can be converted into decimals, then the decimal comparison is used. Otherwise the string comparison is used.

You can see a usage example through the following set of unit tests (as you can see, it also handles mixed alphanumeric sets gracefully, sorting all numeric items to the front of the list in numeric order, followed by all alpha entries in alphabetical order):

[TestFixture]
public class NumericStringSortTests {

    [Test]
    public void TestNumericSort_AllNumeric() {
        List items = new List { "1", "44", "22", "4", "5" };
        items.Sort(NumericStringSort.NumericStringSorter());
        List expectedItems = new List { "1", "4", "5", "22", "44" };
        Assert.IsTrue(items.SequenceEqual(expectedItems));
    }

    [Test]
    public void TestNumericSort_MixesAlphaNumeric() {
        List items = new List { "a", "c", "d", "b", "1", "2", "22", "3" };
        items.Sort(NumericStringSort.NumericStringSorter());
        List expectedItems = new List { "1", "2", "3", "22", "a", "b", "c", "d" };
        Assert.IsTrue(items.SequenceEqual(expectedItems));
    }

    [Test]
    public void TestNumericSort_AllAlpha() {
        List items = new List { "a", "c", "d", "b" };
        items.Sort(NumericStringSort.NumericStringSorter());
        List expectedItems = new List { "a", "b", "c", "d" };
        Assert.IsTrue(items.SequenceEqual(expectedItems));
    }
}

This is something that I have needed on previous occasions – perhaps it may be of use to someone else.