May 28, 2009

Attention to Detail

Sometimes you just have to wonder how the things that show a lack of attention to detail can make it to production.  Especially when there are sooo many eyeballs that would have looked over something before it’s release.

Notice anything suspect about the latest Zune HD marketing shots?  Hint: It’s inside the circled area :-)

image

A font clipping problem? Seriously?! Why wouldn’t you just reduce the font size by a tiny fraction so that the default text actually fits cleanly.  It’s not that hard a problem surely?  And now that I’ve seen that I’m wondering if the device is actually too narrow and whether other software will also experience the same clipping problem.

And then I start wondering, why on earth would you use an image with clipping problems as a promotional picture?  I can’t imaging Apple letting something like that slip through.

May 27, 2009

My Visual Studio Environment (R# and more)

At the most recent Oz Virtual Alt.Net meeting I gave a run through of the various things I have installed on my box for making Visual Studio work the way I want it to, and making myself much more productive in the process.  The recording of that session can be seen either down below or over on the OzAlt.Net blog.

For reference here are the links to the various things I was showing off:

There’s actually more stuff I have installed, but since I didn’t show them off I won’t list them now.  Maybe in another blog post I’ll go through what those things do.

 

May 5, 2009

Basics: Adding Lambda Support To Your Classes

I talked recently about Linq and Extension methods and showed how you could refactor For Each loops into simple linq statements.  When using Linq one of the things you will often come across are Lambda functions (anonymous methods declared inline).

If you’re a curious type, then you might ask yourself “How can I make it so my classes can use Lambda functions?”.  If that’s you then read on :-)

In this scenario I’m going to try and do a few things.  I want to create a simple class that has a collection of strings in it and I want to have a method in that class to print those strings.  But here’s the catch, I don’t want to actually code up that printing method itself in my class.  It doesn’t really belong there and instead  I want someone else to handle the printing – I just want to send them the strings in the class and let them worry about it.

Now I could do it using Dependency Injection techniques, but that’s a different discussion.  Here and now, I want to have someone supply me a function and I’ll just call it.

Test First

Let’s start by creating a test for it first (forgive me for not putting any asserts in the code).  Here’s what it looks like

[TestMethod]
public void TestMethod()
{
StringHolder stringHolder = new StringHolder();
stringHolder.Strings.Add("string 1");
stringHolder.Strings.Add("string number 2");
stringHolder.PrintUsing(Console.WriteLine);
stringHolder.PrintUsing(p => Console.WriteLine(p.ToUpper()));
}

Nothing too complex.  Add some strings and call the method named PrintUsing.  But have a look at those two PrintUsing statements.  What do you see?


The first one says we want to do the print using Console.Writeline.  That’s easy enough to read, but what about the second one?  The second one has one of those funny => operators – it’s a lambda function.  A method defined in line.


What this method is saying is that for each string to be printed, pass it in as a parameter named “p” to a method that has no name (it’s declared inline so needs no name).  That method will call Console.WriteLine, passing to it the string once it has been converted to Uppercase.  Pretty cool.


The StringHolder Class and Action<T>


So let’s implement the class

public class StringHolder
{
public StringHolder()
{
Strings = new List<string>();
}

public List<string> Strings { get; private set; }

public void PrintUsing(Action<string> action)
{
foreach (var part in Strings)
{
action(part);
}
}
}

Here we see something interesting.  Have a look at the parameter for the PrintUsing method – it is looking for an Action<string>.  An Action<T> is a .NET type for a lambda function that returns a void.  The <T> type is the parameter to pass to the method.  If we used Action<string,int> we would be wanting two parameters; a string and an integer.


What we also see is that inside the method we simply call the action passed in as if it were a normal method, including the passing of the parameters.  Quite elegant and simple.


Functions vs Actions


So how about we now expand things a little and add a few extra lines to the end of our test as so

var averageLength = stringHolder.CustomFunction(l => l.Sum(p => p.Length)/l.Count);
Console.WriteLine("Average length: " + averageLength);

We now want to allow people to supply a custom function so that they can apply if to our entire list.  So instead of giving them one string at a time, we’re going to send them the whole string list and they can manipulate it as they choose.


In this case we’re going to work out the average length of the strings in the list.  To do it we add up the length of all the strings using Linq’s Sum method, and then divide it by the number of strings in the list.


Now what do we have to add to our class in order to support this?  As it turns out, we have to add just 3 lines:

public T CustomFunction<T>(Func<List<string>,T> function)
{
return function(Strings);
}

Notice the Func<List<string>,T> type on the parameter. It’s just like the Action<T> we looked at earlier, but with a slight difference. We now have two types being listed.  The parameter types and the return type.  The return type is always the last type in the type list.


Now that it’s declared as a parameter we can call function() just like any other method and then do something with it’s return value.  In our case, we’ll just return it.  I should also point out that we’re using generics here – the type <T> after the CustomFunction method name says that T is a generic type.  In other words the function we pass in can actually return any type it likes, and we’ll pass that back to the calling code.


Default Actions and Overrides


How can any of this be useful? Well, let’s consider our string class and think about showing a total of the number of strings in the class.  I might want a default behaviour, but also allow people to override that behaviour by supplying a different method to use into one of my classes, and I want to be able to do it at run time.


Here’s how it might work.  Let’s start with a test as follows:

[TestMethod]
public void CallAnAction()
{
StringHolder stringHolder = new StringHolder();
stringHolder.MyAction();
}

I simply call the MyAction method on the stringHolder object and it does something.  In this example we’ll get it to write the number of strings out to the console as follows (I know, incredibly complex stuff)

public void MyAction()
{
Console.WriteLine("Number of strings: " + Strings.Count);
}

But I can’t override this behaviour.  I could potentially sublcass it and do an overload on the method (assuming I made it virtual), but that’s not really what I want to do.  As a different approach I can refactor my class slightly and make the MyAction method a property as shown:

public class StringHolder
{
public StringHolder()
{
Strings = new List<string>();
MyAction = () => Console.WriteLine("Number of strings: " + Strings.Count);
}

public Action MyAction { get; set; }

We’ve now made MyAction an Action property and we supply the default method signature in the constructor.  The () at the start of the lambda declaration indicates that there are no parameters for the method.  The good thing is that our test remains unchanged and everything works.


Now we can take this a step further and from our user code supply an alternate behaviour, overriding that defined by the StringHolder class as shown here:

[TestMethod]
public void CallAnAction()
{
StringHolder stringHolder = new StringHolder();
stringHolder.MyAction = () => Console.WriteLine("Alternate " + stringHolder.Strings.Count);
stringHolder.MyAction();
}

We set the property value to be the new lambda function we’ve defined and we’re all set.  This time when we call the MyAction() method the program will use the newly supplied lambda method we’ve supplied and show the alternate output.


Conclusion


What we’ve done is overridden the behaviour of our StringHolder class at runtime avoiding the hassle of subclassing, dependency injection or any other compile time techniques we might use.  It’s an easy and simple way to provide hooks into your classes where you want to have a standard behaviour that can be overridden.

May 3, 2009

Windows 7 RC: Blank “All Programs” After Upgrade

I performed an upgrade on my Vista SP1 based laptop today to the Win 7 Release Candidate and after the upgrade everything looked OK, except for the All Programs folder under the Start Menu being blank, empty, vacant, non-existent.

What was strange though is that I could search for programs via the Start Menu and it would still find them and I could run them.  A quick bit of hunting around on Google led me to Greg Duncan’s post where he explained the problem and a workaround.

So, if you’re having the same problem, head over there and try it out.  The workaround/fix resolved the issue for me.  Hopefully it’ll do the same for you.