2011-11-17

Removing diacritics from a string

Recently I had to write a function that removes all diacritics from a string (e.g.: turning José into Jose). Searching the web, I quickly found the blog post “Stripping is an interesting job” by Michael Kaplan. His code is simple and good, but I saw some opportunities for optimizations (obvious stuff): because we know the approximate length (actually the maximum length) of the resulting string, we could give the StringBuilder-instance an initial capacity equal to the length of the original string. In some simple cases I actually like using a char-array instead of a StringBuilder, because it has even less overhead. Another obvious optimization is to check whether the original string is not empty. Here's my optimized version:
public static string RemoveDiacritics(this string value)
{
  if (value == null) throw new ArgumentNullException("value");

  if (value.Length > 0)
  {
    char[] chars = new char[value.Length];
    int charIndex = 0;

    value = value.Normalize(NormalizationForm.FormD);
    for (int i = 0; i < value.Length; i++)
    {
      char c = value[i];
      if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
        chars[charIndex++] = c;
    }

    return new string(chars, 0, charIndex).Normalize(NormalizationForm.FormC);
  }

  return value;
}

2010-03-04

Google Analytics in ASP.NET

Here's a little C# function I occasionally use to insert a Google Analytics-snippet in an ASP.NET website. It takes a Google Analytics-ID as an argument, which you can leave blank in debug-mode (to not render the snippet). It uses the recently introduced asynchronous loading method (which doesn't block the rendering of your website). It's not literally the same snippet you can find on the Google Analytics site, but it does the same thing (only with a slightly smaller dynamically built JavaScript-snippet).

public static class SiteUtils
{
  public static string RenderGoogleAnalyticsScript(string googleAnalyticsID)
  {
    return string.IsNullOrEmpty(googleAnalyticsID)
      ? ""
      : "<script>"
      + "var _gaq=_gaq||[];"
      + "_gaq.push(['_setAccount','" + googleAnalyticsID + "']);"
      + "_gaq.push(['_trackPageview']);"
      + "(function(){"
      + "var d=document,s0=d.getElementsByTagName('script')[0],"
+ "s=d.createElement('script');" + "s.async=true;" + "s.type='text/javascript';" + "s.src='" + (HttpContext.Current.Request.IsSecureConnection ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js';" + "s0.parentNode.insertBefore(s,s0)}())</script>"; } }

I usually keep the Google Analytics-ID in my application settings (web.config), which allows me to use a different one for different deployments (or an empty one for debugging). So I have a second function (in the same SiteUtils-class) that doesn't take an argument, but takes the Google Analytics-ID from the settings:

public static string RenderGoogleAnalyticsScript()
{
  return RenderGoogleAnalyticsScript(Settings.Default.GoogleAnalyticsID);
}

In my master-page, I then use the following snippet to insert it into the body of the page (note that you have to use the fully qualified type name, including namespace):

<%= MyNamespace.SiteUtils.RenderGoogleAnalyticsScript() %>

Updated 2010-06-03: slightly modified the generated JavaScript-code to better match the current official Google Analytics-script.

2010-01-08

if-read-do-while-read part 2

I just received a comment on my previous post about the if-read-do-while-read construction. Tanton suggests that I could create a method that encapsulates this construction, using delegates for the before, during and after steps. Here's a quick version of an extension method on IDataReader that does this:

public static class DataReaderExtensions
{
  public static void ReadAll(
    this IDataReader reader,
    Action before,
    Action<IDataRecord> during,
    Action after)
  {
    if (reader == null) throw new ArgumentNullException("reader");

    if (reader.Read())
    {
      if (before != null)
        before();
      
      do
      {
        if (during != null)
          during(reader);
      }
      while (reader.Read());
      
      if (after != null)
        after();
    }
  }
}

And here's a rewrite of the HTML-example, using this extension method:

StringBuilder html = new StringBuilder();
using (IDataReader reader = command.ExecuteReader())
  reader.ReadAll(
    () => html.Append("<ul>"),
    record => html.AppendFormat("<li>{0}", record.GetValue(0)),
    () => html.Append("</ul>"));

It surely is shorter, but I'm not entirely convinced it's cleaner.

2009-12-29

if-read-do-while-read

Here’s a construction I occasionally need when reading items from a IDataReader: to execute some code before and after reading through the IDataReader, but only if there are rows in the IDataReader.

One option is to use a boolean variable that determines whether the collection has items, like this:

using (IDataReader reader = command.ExecuteReader())
{
  bool hasRows = false;
  while (reader.Read())
  {
    if (!hasRows)
    {
      hasRows = true;
      Before();
    }

    During();
  }

  if (hasRows)
    After();
}

But the following construction (which I call the if-read-do-while-read construction) does not need the boolean variable and is cleaner:

using (IDataReader reader = command.ExecuteReader())
{
  if (reader.Read())
  {
    Before();

    do
    {
      During();
    }
    while (reader.Read());

    After();
  }
}

Here's a practical example: creating HTML-code with a UL-list for the data, but only if there are items:

StringBuilder html = new StringBuilder();
using (IDataReader reader = command.ExecuteReader())
{
  if (reader.Read())
  {
    html.Append("<ul>");

    do
    {
      html.AppendFormat(
        "<li>{0}",
        reader.GetValue(0));
    }
    while (reader.Read());

    html.Append("</ul>");
  }
}

You could use the same technique on an IEnumerable<T>, but you wouldn't be able to use foreach anymore:

using (var enumerator = collection.GetEnumerator())
{
  if (enumerator.MoveNext())
  {
    Before();

    do
    {
      var item = enumerator.Current;
      During();
    }
    while (enumerator.MoveNext());

    After();
  }
}

2009-11-12

NDepend

A while ago, I received an e-mail from Patrick Smacchia, offering me a license for NDepend and asking me whether I could try it out, give some feedback and if I liked it, maybe I could blog about my experience with NDepend. Because I was already subscribed to the RSS-feed of Patrick’s blog, I knew about NDepend and how it could help developers write better code. I like trying out stuff like this, but somehow I hadn’t found time to install and properly test it. I was already using Visual Studio’s Code Analysis (FxCop) and StyleCop to analyze my code and improve its quality, so I never had the right incentives to use additional tools. But it would be rude to just say no to Patrick, so I decided to give it a try.

After installing it (which just means downloading: NDepend is a portable application that doesn’t really have to be “installed”), I started Visual NDepend (the GUI version of NDepend) and was greeted with a start page that looks like the Visual Studio start page. I decided not to just jump in and try stuff, but to first watch some of the screen casts and tutorials on the NDepend website. And I’m glad I did. After trying to analyze some of my projects, I discovered that NDepend produces a lot of data, reports and graphs that only make sense after you have learnt what it all means. This is a tool that you have to learn how to use and how to interpret its results. The user interface is very flexible, but also a bit overwhelming. But because I don’t really have an idea how I would design a UI for an application like this, I can’t really criticize it.

Comparing it to FxCop and StyleCop is not really fair. There is some overlapping functionality between the 3 tools, but NDepend offers a lot more. First of all, FxCop and StyleCop just analyze your code according to fixed rules and the result is a bunch of errors and warnings. NDepend can also be used like this, but the rules are not fixed and are defined in CQL, a query language that looks and feels like SQL, but allows you to query code constructs. The advantage of this approach is that you can write ad-hoc queries to gain insight in your code (or someone else’s assemblies). Here’s an example of a CQL query:

WARN IF Count > 0 IN SELECT TOP 10 TYPES WHERE NbMethods > 20 ORDER BY NbMethods DESC

This query will generate a warning if your code has classes (and structs) with more than 20 methods. All the built-in rules are written as CQL queries, which you can just modify (or you could just create additional rules).

Another area of analysis that NDepend offers that FxCop and StyleCop do not have is the visual views of your projects. There’s a metrics treemap that shows the proportions of different metrics of your project (methods, types, fields, …) and there are 2 ways to show dependencies: a dependency graph and a dependency matrix. While the dependency graph looks familiar and very graphical, it fails when there are too many elements and dependencies. This is not a failure of NDepend, but just a limitation of dependency graphs in general. Dependency matrices on the other hand are great to understand dependencies, even when there are a lot of elements, but they are less intuitive and you have to learn how to use them and interpret what you see.

My conclusion is that NDepend is a great tool to analyze your code and improve its quality, but it takes getting used to. You have to learn how to use it and how to interpret the results you get. I feel like I have just seen the tip of the iceberg. NDepend also contains a console-application, and can be automated and integrated in your build process (MSBuild, NAnt, …). It has add-ins for Visual Studio and Reflector, and can be used to compare different versions of a code base (for example: to see if compatibility is broken).

If you want to try it yourself, I suggest you first watch the screen casts, download NDepend and start analyzing some of your projects. Another suggestion I have is to start reading Patrick Smacchia’s blog, because he has written about some cool things you can do with NDepend. He also explains how he has analyzed some existing .NET applications (Paint.NET, NHibernate, SharpDevelop, …) and even how he uses NDepend to find what has changed between different versions of the .NET Framework BCL.

2009-10-03

Awesome videos on Channel 9 this week

Channel 9 has always produced nice videos with interviews, or weekly shows (This Week on Channel 9, Ping), but this week they have exceeded themselves in quality and awesomeness. I especially liked the following videos:

Besides being a fan of TWoC9 and Ping, I also loved the series “The History of Microsoft” (25 episodes) a while ago. I think Channel 9 is still revolutionary (related to the videos), despite the buggy forum.

2009-07-28

Calctor 2.1.2

This is a small update to Calctor. I added the option to use comma as a decimal separator.

You can download it here.

Technorati Tags: , , .