I have been browsing through the source for the Subtext Blog Engine, and have come across a lot of cool ideas, and also, a lot of code that is showing its age. Subtext has been around for a long time, and in some places, the code could use some updating. Of course, the contributors to the project are probably not going to find a need to go through every line of code and modernize it, but it's hard not to see places where Linq can really make things more awesome. For instance, there are a handful of methods that try to determine what kind of request is being made, and specify a RequestLocation based on that. One of the methods that tests for a request to a static file looks like this:
private static bool IsStaticFileRequest(HttpRequestBase request)
{
string filePath = request.FilePath;
return filePath.EndsWith(".css", StringComparison.OrdinalIgnoreCase)
|| filePath.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase)
|| filePath.EndsWith(".js", StringComparison.OrdinalIgnoreCase)
|| filePath.EndsWith(".gif", StringComparison.OrdinalIgnoreCase)
|| filePath.EndsWith(".png", StringComparison.OrdinalIgnoreCase)
|| filePath.EndsWith(".xml", StringComparison.OrdinalIgnoreCase)
|| filePath.EndsWith(".txt", StringComparison.OrdinalIgnoreCase)
|| filePath.EndsWith(".html", StringComparison.OrdinalIgnoreCase)
|| filePath.EndsWith(".htm", StringComparison.OrdinalIgnoreCase)
|| filePath.Contains("/images/", StringComparison.OrdinalIgnoreCase);
}
However, this can be made much more awesome with Linq. Behold the More Awesome:
private static bool IsStaticFileRequest(HttpRequestBase request)
{
string filePath = request.FilePath;
var extensions = new[]
{ ".css", ".jpg", ".js", ".gif", ".png",
".xml", ".txt", ".html", ".htm", "/images/" };
return extensions.Any(e => filePath.EndsWith(e, StringComparison.OrdinalIgnoreCase));
}
I hope I never have to go back to .NET 2.0; I will most certainly weep heavily.