Extension Methods vs. Prototype

I had my first exposure recently to extension methods in C# (which I am still relatively new to):

Extension methods enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C#…there is no apparent difference between calling an extension method and the methods that are actually defined in a type.

This makes it easy to add functionality to existing types — this comes in pretty handy if you find yourself doing the same thing a number of times in your code. A good example of extension methods in action can be found here.

I use a similar approach to format telephone numbers for readback in TTS when I need to render VoiceXML using C#/ASP.NET. So, if I have a string representing a telephone number, it may be formatted as 555-111-3333, or (555) 111-3333, or 555.111.3333, etc. When I render a phone number in VoiceXML (using the SSML <say-as> tag) I like to use a string of numbers only, no special delimiters or other characters, to ensure it is read out properly by the TTS engine. Extension methods can help with this.

C# example:

public static class StringExtensionsClass
    {
        public static string GetOnlyNumbers(this string s)
        {
            MatchCollection col = Regex.Matches(s, "[0-9]");
            StringBuilder sb = new StringBuilder();
            foreach (Match m in col) 
            {
                sb.Append(m.Value); 
            }
            return sb.ToString();
        }
    }

In ASP.NET, you would invoke this extension method as if it were a built in method of String:


string telephoneString = "(555) 111-3333";
...
Response.Write("<say -as interpret-as="telephone">" + telephoneString.GetOnlyNumbers() + "</say-as>");

Extension methods remind me a lot of the prototype-based approach of extending classes in JavaScript.

JavaScript example:

String.prototype.getNumbersOnly = function()
{
var mySplitString = this.split("");
var myMatch = new RegExp("[0-9]");
var myNumberString = "";
  for(var i=0; i<mysplitstring.length; i++) {
    if(myMatch.test(mySplitString[i])) {
      myNumberString += mySplitString[i];
    }
  }
return myNumberString;
};

In your VoiceXML code, you would invoke this custom method of the JavaScript String Object like this:


<var name="telephoneString" expr="'(555) 111-3333'"/>
...
<say -as interpret-as="telephone"><value expr="telephoneString.getNumbersOnly()"/></say-as>

So, whether you need to modify your phone number string in your server-side code, or in your client-side code this approach can make the job easy and consistent.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s