Bookmark and Share

Shortest String That Contains All Words

Mango12 created an interesting competition to kick off the New Year, and I decided to try it out. It’s a simple task along the lines of a code kata, and I recommend you try it yourself before looking over my solution.

Task: Compress a list of words into the shortest string that contains all words.

Test: “testing”, “ginger”, “german”, “minutes” should become “minutestingingerman”

Here is my approach: create a weighted graph connecting each term then recursively reduce the highest weighted edges.

The test to see if it works begins with four terms.

image

Two different edges can be reduced. The test won’t be affected by which one I choose. If I optimize this in the future, it would be prudent to check the nodes and merge the two with no other good matches.

image

The choice is clear this time.

image

After the final merge, we end up with minutestingingerman.

I started off with a test to prove that it works.

[Test]
public void ShouldCompressListOfWordsToShortestStringThatContainsAllWords()
{
    var terms = new[] {"testing", "ginger", "german", "minutes"};
    StringGraph graph = new StringGraph(terms);
    Assert.AreEqual("minutestingingerman", graph.Compress());
}

I then created a StringGraph class with weighted nodes. The Node class contain edges and a function to calculate weight when a node is added. In building the solution, I encountered a problem where I was mixing mutable and immutable methods. If I return to this in the future, I should decide which direction I want to go in. I decided to just make it work for the time being.

Since I have a myriad of classes for building the graph, I  will focus on a few important points. The solution is available here if you’re interested in trying it out for yourself.

Calculating Weight

I created a Node class that contains two generics: one for the value type and another for the weight type. To create edges, nodes are added and the weight must be calculated at that time. I decided to make the weight calculation a function that can be assigned by creator. The default function will set the weight to the default of the weight type.

public class Node<T, TWeight> : INode<T, TWeight>
{
    private List<IEdge<T, TWeight>> edges = new List<IEdge<T, TWeight>>();
    
    Func<INode<T, TWeight>, INode<T, TWeight>, TWeight> calculateWeight;

    public Func<INode<T, TWeight>, INode<T, TWeight>, TWeight> 
                                               CalculateWeight { get { if (calculateWeight == null) { calculateWeight = (n1, n2) => default(TWeight); } return calculateWeight; } set { calculateWeight = value; } } public IEnumerable<IEdge<T, TWeight>> Edges { get { return edges; } private set { edges = new List<IEdge<T, TWeight>>(value); } } public T Value { get; private set; } public Node(T value) { this.Value = value; } public void Add(INode<T, TWeight> node) { var edge = new Edge<T, TWeight>(this, node,
                                        CalculateWeight(this, node)); edges.Add(edge); } }

StringGraph assigns the function which checks for the overlap at the end of the parent node and the beginning of the child node.

calculateWeight = (node, newNode) =>
    {
        int maxLength = node.Value.Length < newNode.Value.Length ?
            node.Value.Length : newNode.Value.Length;
        for (int i = node.Value.Length - maxLength; i < maxLength; i++)
        {
            if (node.Value.EndsWith(
                  newNode.Value.Substring(0, maxLength - i))) { return maxLength - i; } } return 0; };

The values generated by this function correlate with the numbers in the graphs above.

Reducing the Graph

The Reduce method is meant to determine two nodes that should be merged and create a new, smaller graph. To make my test turn green, I wrote the following method.

 

public StringGraph Reduce()
{
    if (nodes.Count <= 1)
    {
        return this;
    }

    var edges = from n in nodes
                from e in n.Edges
                select e;
    var edge = edges.First(n => n.Weight == edges.Max(e => e.Weight));
    
    return new StringGraph(nodes.Select(n => n.Value)
                                .Where(str => str != edge.Parent.Value && 
                                                    str != edge.Node.Value) .With(edge.Combine().Value)); }

Although I didn’t put all the necessary error checking in this program, I felt it important that someone does not try to reduce a 1 or 0 node graph. I then pull up every edge, obtain the first one with the highest weight, then combine the nodes it connects.

This part of the code could use some optimization. I am creating an entirely new graph, which may be correct in a class library where immutable types are expected. However, StringGraph is far from immutable, and there is overhead from making it build up a new graph. If I wanted to generate a reduced graph without affecting the current instance, it may be better to implement cloning and a better node merging algorithm. The edges going to the parent and from the child will typically be the same.

More Improvements

There are other improvements that can be made. I’m not currently detecting and removing nodes that represent inner strings, and there are likely better algorithms for NP problems. This four string example is rather simplistic, and there is much more ground that can be covered. Give it a shot and create your own solution, or improve upon the one I’ve presented. Be sure to tell me your approach in the comments!

Bookmark and Share

Static Constructors Should Be Private

Static constructors should be private: a pretty simple rule to follow and one most .NET developers are forced to do. So why do I bring it up? During a webinar earlier today, I demonstrated the new static constructor mocking capabilities in JustMock. I decided to offer that piece of advice because non-private static constructors are a security flaw. However, I was in a C# project, and it’s impossible to use a non-private static constructor anyway.

It’s rare that I need to use static constructors, and I was lacking an example of one with an external dependency. In fact, that’s something I would try to avoid. So, I needed to find a real world example to show why this feature is useful. I did a quick search and found this code analysis entry on MSDN: If a static constructor is not private, it can be called by other code. This is a breaking security issue. I filed this info away and continued my search for a dependency in a static constructor.

I should have read a little further. The article also states: “This rule is enforced by the C# and Visual Basic .NET compilers.” For the vast majority of .NET developers, this rule is irrelevant. Having never had an inclination to make a public static constructor, I failed to realize that I was giving inconsequent advice.

If you’re coding in a language that does support non-private static constructors, please realize the security risk involved. If you are instantiating dependencies in your static constructor, consider a different approach. If one isn’t feasible, JustMock has your back for testing the class.

Bookmark and Share

Partial Application

Functions have a given number of arguments which define the arity of the function. The process of producing another function by setting any number of arguments to a function is known as partial application. C# developers are most familiar with this concept by calling one method from a more specific method, thereby adhering to the DRY principle.

For an example, I have decided to name the div operator with a Divide method.

public static double Divide(double x, double y)
{
    return x / y;
}

I could then use partial application to create an Inverse method, fixing the first argument to the Divide method as the value 1.

public static double Inverse(double y)
{
    return Divide(1, y);
}

This adheres to the DRY principle since the details to the Divide method may change. Perhaps I will choose to use a math library in the future, and the Inverse method is a more specific version of the Divide method, so changes should affect both places.

The context most people hear about partial application is with functional programming. Methods are procedures or functions associated with a class, and that’s why many functional techniques are applicable in the object-oriented world. In C#, most people associate functional programming with anonymous functions, so let’s see how we can use partial application with it.

Func<double, double, double> div = (x, y) => x / y;
Func<double, double> inv = y => div(1, y);

Using partial application with lambda expressions was straightforward. First, I defined a div function. Then, I defined an inv function, calling the div function with 1 as its first argument. There is something to be desired here… it feels like partial application should be built into the language somehow. If you call div(1), it should apply the argument and return a reduced function. Since C# doesn’t support this, we will have to use what tools we have available to build something similar.

public static class FuncExtensions
{
    public static Func<T2, TResult> Apply<T, T2, TResult>(this Func<T, T2, TResult> func, T arg1)
    {
        return arg2 => func(arg1, arg2);
    }
}

I wrote an extension method to apply the first argument to a binary function (you can fill in the rest). This cleans up the original code.

Func<double, double, double> div = (x, y) => x / y;
var inv = div.Apply(1.0);

The compiler cannot expand the type to match the extension method, so it is necessary to specify a double when passing the argument. However, it is now possible to use an implicit type declaration since the method returns a Func with one less generic parameter.

I believe I explained clearly why you should care about this when it comes to methods, but it may be less clear why you need this when it comes to anonymous functions. You may be defining functions in the same manner you are doing methods, and you are attempting to not repeat yourself. What I think may be a more compelling case is that sometimes you need to pass functions with a specific signature, and the best way to achieve that signature is to apply known information. This is powerful in systems that dynamically create and consume functions (business rules), and it simplifies the passing of data as functions carry requisite values.

Partial application is a useful tool, and you’ve likely used it without realizing it.

Bookmark and Share

Arity

The term ‘arity’ evokes images of complex differential diagrams that requires a graduate degree to understand, but the concept itself is rather simple. Arity is a value that represents the number of arguments a function takes. Does your method accept a single argument? Then it’s arity is one. Does it accept two arguments? Then it’s arity is two. Simple.

So it’s simple: a method’s arity is identical to its number of parameters? Not so fast! Arity refers specifically to arguments. Arguments are the values you pass into a method or function. A parameter typically expects you to pass a value to it when you call a method, but it’s possible that the value has been defaulted.

Bob Martin’s Clean Code Tip of the Week #10 was “Avoid Too Many Arguments.” That seems fair enough, but why is it that you hear things like “functions rarely have an arity greater than one.” There are two primary issues at play here.

1. Function inputs can be represented as composite types so that every argument is reduced to one.

2. Functions can be curried so that functions have one argument and returns another function (which has one argument and continues until the full chain is unraveled).

When closures occur, number one is sure to follow to capture the variables used within the lambda expression. The important thing to note is that although the compiler is taking care of the closure, human readers of the code are still experiencing many arguments. Uncle Bob’s tip should still be followed.

Number two is a powerful language feature, but it suffers from the same issue as above.

Lower arity also occurs when side effects from other external sources (such as system variables) are resulting in affected out with fewer arguments. This is undesirable, but sometimes necessary.

As always, keep your code readable and maintainable. Strive for lower arity, but don’t let it break you. Besides, I’d rather you call it “binary” than “arity 2.”

Bookmark and Share

Avoid Parallel Hierarchies

In Implementation Patterns by Kent Beck, he mentions a use of inheritance that can be particularly troublesome: parallel hierarchies. He uses an example of this from an insurance system.

image

As shown in the image above, each subclass from one hierarchy requires one from the other hierarchy. This leads to class explosion! As you can imagine, it’s even worse when there are more than two hierarchies interacting in this manner.

Here’s how the skeleton C# code for the example might look:

public abstract class Contract
{
    public Product Product { get; protected set; }
}

public class InsuranceContract : Contract
{
    public InsuranceContract(InsuranceProduct product)
    {
        this.Product = product;
    }
}

public class PensionContract : Contract
{
    public PensionContract(PensionProduct product)
    {
        this.Product = product;
    }
}

public abstract class Product
{
}

public class InsuranceProduct : Product
{
}

public class PensionProduct : Product
{
}

Kent’s team was able to get around this by restructuring Contract to only use Product without worrying about specific versions.

Generics can alleviate some of this issue.

image

 

public class Contract<T> where T : Product
{
    public T Product { get; protected set; }

    public Contract(T product)
    {
        this.Product = product;
    }
}

public abstract class Product
{
}

public class InsuranceProduct : Product
{
}

public class PensionProduct : Product
{
}

There may be need for variations in the types of Contract, in which case a subclass can be created. However, there may be times which functionality needs to be created on a case by case basis or calculations are being determined based upon formulas in an external source. This can be solved a number of ways. Here’s an example I created using delegates and IronPython.

public class ContractFactory
{
    public Contract<T> CreateContract<T>(T product) where T : Product
    {
        ScriptRuntime py = Python.CreateRuntime();
        dynamic rules = py.UseFile("rules.py");            
        var contract = new Contract<T>(product);
        contract.CalculatePremium = p => rules.CalculatePremium(p);
        return contract;
    }
}

public class Contract<T> where T : Product
{
    public T Product { get; protected set; }
    public Func<T, double> CalculatePremium { get; set; }

    public Contract(T product)
    {
        this.Product = product;
    }
}

You will sometimes end up with parallel inheritance hierarchies as inheritance can be a good representation of the relationship between objects. These relationships can naturally fall into this pattern. However, you should nip it in the bud if the classes start to grow as they can easily become cumbersome. Always consider what changes between the classes and consider moving the responsibility elsewhere.

Bookmark and Share

Empowering Enums

When reading Steve Smith’s articles on enums on ASP.NET Alliance and his blog, I couldn’t help but think of a different approach to the issue. I’ve seen, used, and coded the enum class pattern before (and it works well), but we have new language features today to encourage reuse and empower true enums. I am going to show you techniques that won’t require significant changes to your code if you have an existing base of enums.

First, let’s start off with the Role enum Steven introduced:

public enum Role
{
    Author = 1,
    Editor = 2,
    SalesRepresentative = 3,    
    Secret = 4,
    AnotherSecret = 5,
    Manager = 6
}

It’s a straightforward enum with its ordinal values defined. The original code was retrieving a list of names. I contend that this shouldn’t be limited to one particular enum. I created an EnumList class to turn any enum into an IEnumerable<T>.

public class EnumList<T> : IEnumerable<T> where T : struct
{
    public EnumList()
    {
        if (!typeof(T).IsEnum)
        {
            throw new ArgumentException("Generic parameter must be enum");
        }
    }

    public virtual IEnumerator<T> GetEnumerator()
    {
        return Enum.GetValues(typeof(T)).Cast<T>().GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return ((IEnumerable<T>)this).GetEnumerator();
    }
} 

**UPDATED** Paulo Morgado reminded me that I could just call GetEnumerator().

This is merely hiding the necessity of calling Enum.GetValue(typeof(T)).Cast<T>(), but it makes for friendlier tests.

[TestMethod]
public void ShouldCreateValidListAndReturnFirstValue()
{
    var roles= new EnumList<Role>();
    Assert.AreEqual(Role.Author, roles.First());
}

When displaying enum values, it may be requisite to flag a value to not display, or to show a better description than the value itself. We can use attributes to achieve this. A description attribute is provided in System.ComponentModel. I created the Visible attribute.

{
    public class VisibleAttribute : Attribute
    {
        private readonly bool visible;

        public bool Visible
        {
            get { return this.visible; }
        } 

        public VisibleAttribute()
        {
            this.visible = true;
        }

        public VisibleAttribute(bool visible)
        {
            this.visible = visible;
        }

    }

I then set the appropriate attributes on the enum values.

public enum Role
{
    Author = 1,
    Editor = 2,
    [Description("Sales Representative")]
    SalesRepresentative = 3,
    [Visible(false)]       
    Secret = 4,
    [Visible(false)]
    AnotherSecret = 5,
    Manager = 6
}

Here’s where we can begin empowering enums. All enums can be given extension method to retrieve the value of the attribute.

public static class EnumExtensions
{
    public static bool IsVisible(this Enum value)
    {
        var visible = value.GetType()
                           .GetField(value.ToString())
                           .GetCustomAttributes(typeof(VisibleAttribute), false)
                           .Cast<VisibleAttribute>()
                           .FirstOrDefault() ?? new VisibleAttribute(true);

        return visible.Visible;                  
    }

    public static string Description(this Enum value)
    {
        var description = value.GetType()
                               .GetField(value.ToString())
                               .GetCustomAttributes(typeof(DescriptionAttribute), false)
                               .Cast<DescriptionAttribute>()
                               .FirstOrDefault() ?? new DescriptionAttribute(value.ToString());

        return description.Description;
    }
}

The great thing is that these methods work for every attribute. EnumExtensions is a helper class, but we treat these methods as belonging to the enum value.

The original post was interested in displaying friendly names which consists of only those values that are visible. In addition, we want to use the description instead of the value if it is available. This can be achieved by writing an extension method for IEnumerable<Role>.

public static class RoleListExtensions
{
    public static IEnumerable<string> DisplayFriendlyNames(this EnumList<Role> list)
    {
        return list.Where(role => role.IsVisible())
                   .Select(role => role.Description());
    }
}

This allows the test to pass, which proves that only visible descriptions pulled from the list of roles.

[TestMethod]
public void DisplayFriendlyNamesShouldBeVisibleAndFullyDescriptive()
{
    var roles = new EnumList<Role>().DisplayFriendlyNames().ToArray();
    Assert.AreEqual(4, roles.Length);
    Assert.AreEqual("Author", roles[0]);
    Assert.AreEqual("Editor", roles[1]);
    Assert.AreEqual("Sales Representative", roles[2]);
    Assert.AreEqual("Manager", roles[3]);
}

There are strengths to creating classes based on an enum. The approach I demonstrated should only be used when it is desirable to add a little functionality to enums across the board. Create classes for anything more complex. It is possible to go overboard with the extension methods and completely mimic a class from an enum… if you find yourself doing this, perhaps a class would be more appropriate.

Bookmark and Share

Making It More Functional

Ted Neward goes through a code kata with several programming paradigms in the latest Working Programmer column. Being a coding polyglot is all the rage nowadays, and understanding multiple paradigms is essential to being a well-rounded developer. Ted goes through procedural style, modifies it to be object-oriented, demonstrates how to use meta and then dynamic programming. Finally, he makes it more functional. The proposed solution adds an aspect of functional programming, but it can be taken further.

First, let me post Ted’s functional solution to the Data Munging kata.

TextParser<WeatherData> parser = new TextParser<WeatherData>();
parser.LineParseVerifier =
    (line) =>
    {
        if ( (line.Trim().Length == 0) ||                                         
            (line.Contains("MMU")) ||    
            (line.Contains("Dy")) ||     
            (line.Contains("mo")))
            return false;
        else
            return true;
    };
parser.ColumnExtracter =
    (line) =>
    {
        WeatherData wd = new WeatherData();
        wd.Day = line.Substring(0, 4);
        wd.MxT = line.Substring(5, 6);
        wd.MnT = line.Substring(12, 6);
        return wd;
    };
List<WeatherData> results = parser.Parse("weather.dat");

This is actually a hybrid object-oriented/functional solution. TextParser<T> is a functional version of the template method pattern. It replaces the abstract methods with delegates, likely using the standardized Func classes. LineParseVerifier would be Func<string, bool>, and ColumnExtracter would be Func<string, T>. Instead of creating a subclass, you only need to define the delegates that are called. The power of this approach is that you have a reusable class without class explosion.

This solution has been available since C# 2.0. It’s made cleaner in C# 3.0 with the introduction of the Func and Action classes and lambda expressions. The 2.0 version would have used defined delegates and anonymous methods.

C# is an object-oriented language, but C# 3.0 introduced other functional aspects than the ones used by this solution. These are extension methods and list comprehension via LINQ. With these, you can approach the problem in an entirely different manner.

var weatherInfo = from line in File.ReadLines("weather.data")
                  where line.Trim().Length > 0
                        && !line.Contains("MMU")
                        && !line.Contains("Dy")
                        && !line.Contains("mo")
                  select new
                  {
                    Day = Int32.Parse(line.Substring(0, 4).Replace("*", " ")),
                    MxT = Single.Parse(line.Substring(5, 6).Replace("*", " ")),
                    MnT = Single.Parse(line.Substring(12, 6).Replace("*", " "))
                  };

Console.WriteLine("Max spread: " + weatherInfo.Max(wd => wd.MxT - wd.MnT));

With this declarative approach, there’s no need to create the TextParser or WeatherData classes. The functions are defined in the where and select statements. You can use the extension methods of LINQ directly, and extract them to make the code even more readable and declarative. The downside is that classes must be created, but they are reusable.

The classes:

public class WeatherData
{
    public int Day { get; set; }
    public float MxT { get; set; }
    public float MnT { get; set; }
}

public static class EnumerableStringExtensions
{
    public static IEnumerable<string> WhereWeatherData(this IEnumerable<string> strings)
    {
        return strings.Where(line => line.Trim().Length > 0
                                && !line.Contains("MMU")
                                && !line.Contains("Dy")
                                && !line.Contains("mo"));
    }

    public static IEnumerable<WeatherData> MapWeatherData(this IEnumerable<string> strings)
    {
        return strings.Select(line => new WeatherData
        {
            Day = Int32.Parse(line.Substring(0, 4).Replace("*", " ")),
            MxT = Single.Parse(line.Substring(5, 6).Replace("*", " ")),
            MnT = Single.Parse(line.Substring(12, 6).Replace("*", " "))
        });
    }
}

And the call:

var weatherInfo = File.ReadLines("weather.data")
                      .WhereWeatherData()
                      .MapWeatherData();

You can approach this a several different ways in a functional language. The above would read something like: let weatherInfo = mapWeatherData whereWeatherData File.ReadAllLines(“weather.data”);

There are many different approaches to a problem, but some are better than others depending on what you’re trying to accomplish. It is perfectly acceptable to use the procedural approach if it’s a one shot program. Use classes when you need to encapsulate functionality for reusability. I tend to use extension methods when the functionality belongs to another object, or to improve the readability of my code. However, the approach I demonstrated may not be ideal for the application you’re developing. Therefore, it is important to know the options available to give yourself more flexibility when coding.

**EDIT** Jonathan Allen pointed out that ReadLines is the correct method when iterating the lines in the deferred manner used in the examples. The original was using ReadAllLines which does not use deferred execution.

Bookmark and Share

Decompiling .NET

If you’re like me, you like peeking at code to see how things work. I find myself poking around the internals of the .NET Framework quite often, to see what’s going on inside Microsoft’s collective head. I do this for fun, but I want to share the things I come across with everyone else. So, I’m starting a new weekly series on decompiling the .NET Framework.

My tool of choice is Telerik JustDecompile. I work for Telerik, and this gives me an opportunity to provide feedback to the team. Of course, you can follow along with the .NET decompiler you prefer. I will be primarily showing things in C#, and every now and then I will discuss the IL. In rare cases, I will show things in VB to discuss the differences in how code is represented.

This post will serve as hub for all the articles. Some people subscribe to my blog and will read the articles when they are released. However, it will be useful to return to this post if you’re looking for a specific entry.

Bookmark and Share

Going Asynchronous

In A Brief Introduction to Fundamental OOP, I built a small twitter application. It does the job well, but it has a very undesirable quality. When you click the search button, the application momentarily freezes as it waits for the Twitter service to respond. The application would feel a lot smoother if it allowed the user to continue using the application while the request is processed.

Here is the button click handler.

private void SearchButton_Click(object sender, RoutedEventArgs e)
{
    SocialMediaReader reader = new Twitter(SearchText.Text);
    listBox.ItemsSource = reader.GetMessages();
}

I’m calling GetMessages() to assign the ItemSource property of listBox. This is the call that is blocking the UI thread.

public abstract class SocialMediaReader
{
    protected abstract Uri GetUri();
    protected abstract IEnumerable<Message> ParseData(string data);

    public IEnumerable<Message> GetMessages()
    {
        WebClient client = new WebClient();
        string data = client.DownloadString(GetUri());
        return ParseData(data);
    }
}

As you can see the GetMessages() method simply returns an IEnumerable<Message>. The blocking call is to to WebClient.DownloadString().

Callbacks

When you “fire and forget” a method, it is essential to define what comes next.  The asynchronous version of GetMessages will accept a delegate that will be called when the messages are received. This is similar to your typical continuation, except it only affects control flow when data is available.

public void GetMessages(Action<IEnumerable<Message>> callback)
{
    WebClient client = new WebClient();
    client.DownloadStringCompleted += (s, e) =>
        callback(e.Error == null ? ParseData(e.Result) : Enumerable.Empty<Message>());
    client.DownloadStringAsync(GetUri());
}

Instead of calling DownloadString, I call DownloadStringAsync, which will call client.DownloadStringCompleted when it is finished. The event handler for client.DownloadStringCompleted is a lambda expression that executes the callback.

private void SearchButton_Click(object sender, RoutedEventArgs e)
{
    SocialMediaReader reader = new Twitter(SearchText.Text);
    reader.GetMessages(messages => listBox.ItemsSource = messages);
}

The click event now calls the asynchronous version of GetMessages by passing a lambda expression as an argument. This expression simply assigns listBox.ItemsSource with the data that was parsed from Twitter.

C# 5

The Microsoft Visual Studio Async CTP adds language constructs to make asynchronous programming more expressive. After you install the CTP, you need to reference AsyncCtpLibrary.dll in your project. This is typically available at %HOMEPATH%\Documents\Microsoft Visual Studio Async CTP\Samples.

Telerik JustCode Q2 SP1 supports the new language constructs, and I’ve written an article describing async support in Telerik JustCode. If you need a Visual Studio productivity tool, you can count on JustCode to be up-to-date.

Two new keywords are required to enable an asynchronous method. The first is the async keyword, which is used to mark the method signature as asynchronous. The second is the await keyword. The line that uses the await keyword is called immediately, and the lines after the await keyword are part of the continuation. If a value is returned from the method, the assignment because part of the continuation as well.

public async Task<IEnumerable<Message>> GetMessagesAsync()
{
    WebClient client = new WebClient();
    string data = await client.DownloadStringTaskAsync(GetUri());
    return ParseData(data);
}
private async void SearchButton_Click(object sender, RoutedEventArgs e)
{
    SocialMediaReader reader = new Twitter(SearchText.Text);
    listBox.ItemsSource = await reader.GetMessagesAsync();
}

To maintain the non-asynchronous version of the method, I had to change the name of the name of the async version to GetMessagesAsync. The parameter list was identical (no parameters), but they have different return types. You cannot overload on return type. Mentioning return type, you may have noticed the GetMessageAsync method returns Task<IEnumerable<Messsage>> rather than IEnumerable<Message>. Methods marked async must be void, return Task (equivalent to using void), or return Task<T>. When the return value is Task<T>, you return from the method the type defined as T. For void and Task, you do not return a result.

As you can see, this is somewhat cleaner than the callback version of the code. It eliminates the requirement to pass around lambda expressions or delegates.

Conclusion

In the introduction, I talked about the issue of the UI thread being blocked. Using asynchronous methods solves the problem and creates a more responsive environment.

Bookmark and Share

A Brief Introduction of Fundamental OOP

As .NET developers, most of us are accustomed to object-oriented programming. Many of my articles and talks go far beyond the fundamental principles, but they are worth visiting for developers who need to brush up on OOP. In this article, I am going to avoid theory and focus on practical steps when building a small application. This is building toward an asynchronous programming post I will publish tomorrow.

Why should you care about OOP

When designing software, we will start out with requirements for what we are trying to build. It may be unnecessary to go the object-oriented route if you have simple requirements. I tend to start out separating parts into appropriate subsystem layers and deciding what belongs in separate, physical tiers. But let’s say you only need to create a console application that writes out a user’s latest tweets. If that’s the only requirement, there is no need to write anything other than a simple, procedural application. KISS: Keep It Simple Silly.

public static class Program
{
    const string timelineUrl = 
        "http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=";

    private static void Main(string[] args)
    {
        if (args.Length == 0)
            return;

        string userName = args[0];
        WebClient client = new WebClient();
        string xml = client.DownloadString(timelineUrl + userName);
        XElement xmlTweets = XElement.Parse(xml);

        var messages = from tweet in xmlTweets.Descendants("status")
                       select tweet.Element("text").Value;

        foreach (var msg in messages)
            Console.WriteLine(msg);
    }
}

This simple program fits the requirement. If you call UserTweets.exe kodefuguru, my tweets will be printed to the screen. There are some out there who believe everything must be perfectly architected and designed, but you should allow requirements to drive your development. I could add error checking and logging routines, but if the only thing I need to do is create a console application that writes a user’s tweets to the screen, this program is fine the way it is. I’m done, move on to the next project.

Create classes

There are perfectly valid reasons to complicate this program with OOP. The primary reason is reuse, and I may anticipate using this functionality for another project. The first thing I notice is that other data is available when parsing the tweets. I’m going to group useful data into a class called Message. I am abstracting the actual twitter data. If I write programs that pull from other social media sources, this is useful for representing what I will use in my program. I can map data returned from the source to the class the program uses.

public class Message
{
    public string Text { get; set; }
    public string ImageSource { get; set; }
    public string UserName { get; set; }
}

I probably want a class that handles the retrieval of these messages. If I needed to do more than one at a time, reusing a class is preferable to copying and pasting code. The former encapsulates the functionality so that if the details change, I only need to change the implementation in one place.

public class Twitter
{
    const string timelineUrl = 
        "http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=";

    private string userName;

    public string UserName
    {
        get { return userName; }
        set 
        {
            if (String.IsNullOrWhiteSpace(value))
                throw new ArgumentNullException();

            userName = value;
        }
    }

    public Twitter(string userName)
    {
        this.UserName = userName;
    }

    public Message[] GetMessages()
    {
        WebClient client = new WebClient();
        string xml = client.DownloadString(timelineUrl + this.UserName);
        XElement xmlTweets = XElement.Parse(xml);

        var messages = from tweet in xmlTweets.Descendants("status")
                       select new Message
                       {
                           ImageSource = tweet.Element("user").Element("profile_image_url")
                                              .Value,
                           Text = tweet.Element("text").Value,
                           UserName = tweet.Element("user").Element("screen_name").Value
                       };

        return messages.ToArray();
    }
}

The twitter class has the functionality for calling twitter, and I only need to instantiate one to make the calls from another class. You may also notice that I required UserName to be passed in the constructor, and it throws an exception if it is null or whitespace. The username is required, and encapsulation elegantly wraps up that requirement.

The alternative to encapsulation is to publicly expose the field. If you design classes in this manner, you will find that there is no easy way to control changes to an object’s memory. It is better to encapsulate a field into a property.

The program now looks like the following.

private static void Main(string[] args)
{
    if (args.Length == 0)
        return;

    string userName = args[0];
    Twitter twitter = new Twitter(userName);
    var messages = twitter.GetMessages();

    foreach (var msg in messages)
        Console.WriteLine(msg.Text);
}

Inheritance and Polymorphism

This handles the capabilities of twitter, but I may want to access other social media sources such as Facebook or Google+. We can solve this problem by using inheritance. If you look at the GetMessages method, it’s not too difficult to determine what will be different between services. There are two major pieces: the URL and the mapping.

Interfaces could be used, but there is shared implementation. I will create a SocialMediaReader abstract class to encapsulate the things that stay the same. I changed Message[] to IEnumerable<Message> since client classes should only read the messages.

public abstract class SocialMediaReader
{
    protected abstract Uri GetUri();
    protected abstract IEnumerable<Message> ParseData(string data);

    public IEnumerable<Message> GetMessages()
    {
        WebClient client = new WebClient();
        string data = client.DownloadString(GetUri());
        return ParseData(data);
    }
}

I moved the Twitter specific functionality to the Twitter class, which inherits from SocialMediaReader.

public class Twitter : SocialMediaReader
{
    private string userName;

    public string UserName
    {
        get
        {
            return userName;
        }
        set
        {
            if (value == null)
                throw new ArgumentNullException();

            userName = value;
        }
    }

    public Twitter(string userName)
    {
        this.UserName = userName;
    }

    protected override Uri GetUri()
    {
        return new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=" 
            + this.UserName);
    }

    protected override IEnumerable<Message> ParseData(string data)
    {
        XElement xmlTweets = XElement.Parse(data);

        var messages = from tweet in xmlTweets.Descendants("status")
                       select new Message
                       {
                           ImageSource = tweet.Element("user").Element("profile_image_url")
                                              .Value,
                           Text = tweet.Element("text").Value,
                           UserName = tweet.Element("user").Element("screen_name").Value
                       };

        return messages.ToArray();
    }
}

Now the program can use any class that inherits from SocialMediaReader. This makes it easy to switch out social media providers in the future.

static void Main(string[] args)
{
    if (args.Length == 0)
        return;

    string userName = args[0];
    SocialMediaReader reader = new Twitter(userName);
    var messages = reader.GetMessages();

    foreach (var msg in messages)
        Console.WriteLine(msg.Text);
}

This works because the Twitter class is a SocialMediaReader, and that’s all we need. Use inheritance to represent an *IS* relationship. Notice that the Twitter class still contains a UserName property. Use composition to describe a *HAS* relationship. The Twitter class is a SocialMediaReader and it has a UserName property.

Reuse

I can easily take these classes and use them in another program. I’ve created a simple WPF application that does just that.

The view is nothing fancy, just a TextBox, Button, and ListBox.

<Window x:Class="Eventing.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <TextBox Name="SearchText"/>
        <Button Name="SearchButton" Grid.Column="1" Content="Search" 
                Click="SearchButton_Click"/>
        <ListBox Margin="0,10,0,0" Name="listBox" Grid.ColumnSpan="2" Grid.Row="1" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Height="132">
                        <Image Source="{Binding ImageSource}" Height="73" Width="73" 
                               VerticalAlignment="Top" Margin="0,10,8,0"/>
                        <StackPanel Width="370">
                            <TextBlock Text="{Binding UserName}" Foreground="#FFC8AB14" 
                                       FontSize="28" />
                            <TextBlock Text="{Binding Text}" TextWrapping="Wrap" 
                                       FontSize="24" />
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

I decided not to dive into presentation patterns in this article, so I am using code behind to represent the SearchButton’s click functionality.

private void SearchButton_Click(object sender, RoutedEventArgs e)
{
    SocialMediaReader reader = new Twitter(SearchText.Text);
    listBox.ItemsSource = reader.GetMessages();
}

Since I had already created the classes necessary for this application, it was a simple matter of calling them. I did not need to copy and paste code, and if I enhance my class library other apps that reference the library will benefit directly from my efforts.

There is one difference between the console application and the WPF application that will require me to make changes. When you click the search button, the application is unresponsive until it returns. Tomorrow, I will go about fixing this problem in C# 4, and then I will show how C# 5 makes it even easier to accomplish.

KodefuGuru.GetInfo()

Chris Eargle
LinkedIn Twitter Technorati Facebook

Chris Eargle
Telerik Developer Evangelist, C# MVP

JustCode

Telerik .NET Ninja

 

INETA Community Speakers Program

 

MVP - Visual C#

 

Friend of RedGate

World Map

Tag cloud

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.