Ideal Binary

by Aidan Doolan 8/23/2009 2:40:27 AM 2 Comments

Well it’s been almost a year since I updated this blog! I’ve been pretty busy working at Pocket Kings here in Dublin.

Almost two years ago I decided to take a break from game/engine/porting development. I started researching web development technologies, in particular Rails, ASP.NET MVC, and Silverlight. After 10 months (and an MCTS) I started working for Pocket Kings as a Web Developer.

Essentially I wanted to ground the research I had done with some practical experience. And practical experience I got, in addition to a years experience using SCRUM in an agile development process. All together, the last two years have been an amazing learning experience.

On the 28th of this month I finish up at Pocket Kings and from September 1st I’ll be working with my brother at our own company, Ideal Binary. I intend to keep this blog alive, but not with technical posts. These will be published on my new Ideal Binary blog.

I hope you drop by and check out Ideal Binary.


My brother wrote a slick  lens panel control with Silverlight 2 recently. He modeled it on the look and behaviour of the Mac OSX Leopard dock. It was so slick, I had to write my own implementation. You can check it out here. After I got it up and running we decided to do a kind of distributed (by about 800 miles) pair programming session to see if we could come up with cleaner ways to structure our lens panel controls.

I'm not going to go into the low-level implementation details in this article. The behaviour of the control is pretty much the same as the Mac dock control. Kevin has described the high-level approach we ended up with on his own blog, but I will highlight some features of XAML we used to make the lens panel controls more easily extendable.

When we finished refactoring our code, both of our lens panel controls ended up making use of the ItemsControl, data templates, data binding and dependency injection though XAML, but neither of our controls started out that way.

Aidan's Mac OSX Leopard dock style Silverlight control

Dependency Injection

The first structural decision I made when I got far enough in to the code to start displaying items was to try to separate the source of the panel items from the lens panel control itself. The most basic way to do this is have a base class with the generic lens panel behaviour baked in, and then maybe derive versions for showing lens panel items from various different sources like Flickr, iStockPhoto, or custom menu items.

While this approach works, it tightly couples the base lens panel class with all other derived lens panels. What if you wanted to further customise the lens panel to display the individual items differently? You could derive some more classes with the new visuals baked in but again, this tightly couples even more classes together.

My first solution to this problem was to separate out each of the responsibilities into their own classes. The LensPanel class contained the logic to arrange and display the panel items. The LensPanel had a public property of type IPanelItemProvider. This allowed me to implement any number of concrete providers without directly coupling them to the lens panel control itself.

In addition to this, the providers contained a public property of type IPanelItemVisualFactory. This allowed me to further decouple the visual aspects of the panel items from the provider. All of these dependencies can be injected directly using XAML. Below is roughly how my first solution looked.

<LensPanel:LensPanel Width="1000" Height="300" NumberOfItems="12" >
    <LensPanel:LensPanel.PanelItemProvider>
        <LensPanel:iStockPanelItemProvider>
            <LensPanel:iStockPanelItemProvider.PanelItemVisualFactory>
                <LensPanel:ReflectionPanelItemVisualFactory />
            </LensPanel:iStockPanelItemProvider.PanelItemVisualFactory>
        </LensPanel:iStockPanelItemProvider>
    </LensPanel:LensPanel.PanelItemProvider>
</LensPanel:LensPanel>

The above solution has some nice benefits to offer. First, it makes use of the Single Responsibility Principle (SRP). Each of the objects has a single clear responsibility. The lens panel only lays out panel items. The panel item provider only provides panel items. The panel item visual factory only creates panel item visuals. Second, it also makes use of the Open/Closed Principle (OCP). This means that the solution is open for extension (by creating new concrete provider and factory objects) but closed for modification (providing new concrete providers and factories requires no modification of the base control). Third, this solution is very unit testable.

But it turns out there's a much simpler way to implement the same separation of concerns by exploiting several features already built-in to Silverlight 2.

Data Binding And Templates

Rather than creating the provider interface, IPanelItemProvider, and then managing the connection of the items from the provider with the items in the lens panel, we decided to exploit Silverlight's data binding support. Both of our controls derive from the Panel class and it wasn't immediately obvious how we were going to bind directly to an items list within the panel. Kevin discovered the ItemsControl which makes this very simple.

The ItemsControl allows you to create a panel control where you can specify a panel template for the type of panel you want, in addition to a data template for the individual panel items. With only a small amount of refactoring, and without losing any of the benefits of our earlier approaches, we had reworked our solutions to use the ItemsControl. Below you can see I'm creating an ItemsControl and specifying my custom LensPanel class as the panel template and my custom LensPanelItem class for the item template.

<ItemsControl x:Name="_lensPanelControl" ItemsSource="{StaticResource iStockPhotoProvider}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <l:LensPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <l:LensPanelItem />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

In addition to specifying the templates for the panel and panel items, I'm also binding the ItemsControl to an instance of the iStockPhotoProvider declared in the local resources. I chose to derive the iStockPhotoProvider directly from ObservableCollection so that once it is bound to the ItemsControl, any modifications to it result in an update of the ItemsControl. Again, this provides the same benefits as before. The provider is loosely coupled to the lens panel control. Below is the declaration for the concrete photo provider.

<UserControl.Resources>
    <l:iStockPhotoProvider x:Key="iStockPhotoProvider" />
</UserControl.Resources>

This makes it possible for a designer to swap out the provider with different providers by simply modifying the XAML. No code changes are required.

Conclusion

So far, I've been very impressed with WPF and Silverlight 2. It's very obvious that the features of these frameworks are based on solid practical foundations. I found Chris Anderson's book Essential Windows Presentation Foundation (WPF) (Microsoft .NET Development Series) to be an excellent guide to exploring how to use these features in practical ways. Also, he describes at a low level (and in very simple terms) the motivation behind some of these features and exposes details about their implementation. This was a big help in transitioning from a Windows Forms way of thinking to the more modern approach suggested by WPF and Silverlight 2.


I had to review some physics code for a client recently. Although the physics model being used was very simple point physics, the code was quite long and the algorithms were not immediately obvious. This tends to happen when code isn't refactored as it is modified and extended.

I made a few suggestions about the code, one of which was to try and wrap the underlying math routines in unit tests. This is a really simple way to catch bugs that can otherwise result in strange behavior, like the ball disappearing or missing collisions. It also lays the foundation for performing code refactoring that can dramatically improve the quality of the code.

As a little test, I decided to write a small physics project to illustrate this in action.

Scope Of The Project

I didn't want to write a full physics engine for this test. I've written one before and it's not a small undertaking. Then I remembered an old Amiga demo I saw a million years ago that had a soft-body jelly vector object contained within a spinning cube. No, I can't for the life of me remember the name of the demo. I decided to try to recreate it in C# and then use unit tests and code refactoring to clean up the code.

softbody1

Cleaning Up The Code

The idea was simple. Write the most flat unstructured code and get it up an running. This code base represents the 'mess' that has to be cleaned up and refactored. Even basic code refactoring can have a dramatic effect on this code. Have a look below.

// Apply spring force
for (int l = 0; l < _jellyLines.Length / 2; l += 1)
{
    int line = l * 2;
    int vertex1 = _jellyLines[line] * 3;
    int vertex2 = _jellyLines[line + 1] * 3;
 
    // Get Spring length 
 
    float x1 = _jelly_os[vertex1];
    float y1 = _jelly_os[vertex1 + 1];
    float z1 = _jelly_os[vertex1 + 2];
 
    float x2 = _jelly_os[vertex2];
    float y2 = _jelly_os[vertex2 + 1];
    float z2 = _jelly_os[vertex2 + 2];
 
    float lineRestLength = _jellyLineLengths[l];
    float lineLength = (float)Math.Sqrt((z2 - z1) * (z2 - z1) + (y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1));
 
    // Get Relative Velocity of both end points
 
    float rvx = _jelly_velocity[vertex2] - _jelly_velocity[vertex1];
    float rvy = _jelly_velocity[vertex2 + 1] - _jelly_velocity[vertex1 + 1];
    float rvz = _jelly_velocity[vertex2 + 2] - _jelly_velocity[vertex1 + 2];
 
    float relativeVelocity = (float)Math.Sqrt(rvz * rvz + rvy * rvy + rvx * rvx);
 
    // Calculate Spring Force
 
    float force = _springK * (lineLength - lineRestLength) - _springB * relativeVelocity;
 
    // Get Spring Vector
 
    float vx = _jelly_os[vertex2] - _jelly_os[vertex1];
    float vy = _jelly_os[vertex2 + 1] - _jelly_os[vertex1 + 1];
    float vz = _jelly_os[vertex2 + 2] - _jelly_os[vertex1 + 2];
 
    // Normalise
 
    float vl = (float)Math.Sqrt(vz * vz + vy * vy + vx * vx);
    vx = vl != 0.0f ? vx / vl : 0.0f;
    vy = vl != 0.0f ? vy / vl : 0.0f;
    vz = vl != 0.0f ? vz / vl : 0.0f;
 
    // Apply force to both ends of the Spring
 
    _jelly_force[vertex1] += vx * -force;
    _jelly_force[vertex1 + 1] += vy * -force;
    _jelly_force[vertex1 + 2] += vz * -force;
 
    _jelly_force[vertex2] += vx * force;
    _jelly_force[vertex2 + 1] += vy * force;
    _jelly_force[vertex2 + 2] += vz * force;
}

I've made no attempt to use any OO design at all in the above code. It iterates over the lines/springs making up our soft-body jelly object and applies the spring forces to both ends of the spring. It stinks of many of the classic bad code smells. After a afternoon of refactoring the code for the entire project, the above code ended up as follows.

public void ApplyForceToEndPoints(PointMass[] points)
{
    Vector3 springVector = GetSpringVector(points);
    Vector3 relativeVelocityVector = GetEndPointsRelativeVelocityVector(points);
    Vector3 springForceVector = GetSpringForceVector(springVector, relativeVelocityVector);
 
    points[Point1].Force -= springForceVector;
    points[Point2].Force += springForceVector;
}

For a start the refactored code above is a lot smaller. This is because duplicate functionality has been extracted along with operators and methods and classes. Notice also that this method is a class method and operates on only one spring and not an array of springs. Finally, the need for comments is gone thanks to the new refactored method names.

Another example is shown below.

// Integrate
for (int v = 0; v < _jelly_os.Length / 3; v += 1)
{
    int vertex = v * 3;
 
    // Assume mass is 1.0, apply acceleration
    _jelly_velocity[vertex] += _jelly_force[vertex] * interval / 1000;
    _jelly_velocity[vertex + 1] += _jelly_force[vertex + 1] * interval / 1000;
    _jelly_velocity[vertex + 2] += _jelly_force[vertex + 2] * interval / 1000;
 
    // Apply velocity
    _jelly_os[vertex] += _jelly_velocity[vertex];
    _jelly_os[vertex + 1] += _jelly_velocity[vertex + 1];
    _jelly_os[vertex + 2] += _jelly_velocity[vertex + 2];
}
 
// Clip vertices
for (int v = 0; v < _jelly_os.Length / 3; v += 1)
{
    int vertex = v * 3;
 
    float x = _jelly_os[vertex];
    float y = _jelly_os[vertex + 1];
    float z = _jelly_os[vertex + 2];
 
    for (int p = 0; p < _cubePlanes_ws.Length / 4; p++)
    {
        int plane = p * 4;
 
        float px = _cubePlanes_ws[plane];
        float py = _cubePlanes_ws[plane + 1];
        float pz = _cubePlanes_ws[plane + 2];
        float pd = _cubePlanes_ws[plane + 3];
 
        float clipD = px * x + py * y + pz * z;
 
        if (clipD < pd)
        {
            x += px * -(clipD - pd);
            y += py * -(clipD - pd);
            z += pz * -(clipD - pd);
 
            // Remove this vector component from the velocity vector
 
            float velocityD = px * _jelly_velocity[vertex] + py * _jelly_velocity[vertex + 1] + pz * _jelly_velocity[vertex + 2];
 
            _jelly_velocity[vertex] -= px * velocityD;
            _jelly_velocity[vertex + 1] -= py * velocityD;
            _jelly_velocity[vertex + 2] -= pz * velocityD;
        }
    }
 
    _jelly_os[vertex] = x;
    _jelly_os[vertex + 1] = y;
    _jelly_os[vertex + 2] = z;
}

The above code uses Euler integration to move the soft-body jelly object forward in time. It then clips the vertices or points of the soft-body against the planes of the cube in which it is contained. Both operations occur within for loops. The same bad code smells from earlier are also evident here.

Both of the above for loops iterate over the points of the soft-body. That's where I started when I began to refactor the code. When I was finished, I ended up with a PointMass class that housed most of the above functionality.

public class PointMass
{
    public Vector3 Point { get; set; }
    public Vector3 Velocity { get; set; }
    public Vector3 Force { get; set; }
    public float Mass { get; set; }
 
    public PointMass()
    {
        Point = new Vector3();
        Velocity = new Vector3();
        Force = new Vector3();
        Mass = 1;
    }
 
    public void Integrate(int intervalInMilliseconds)
    {
        Velocity += Force * (((float)intervalInMilliseconds / 1000) / Mass);
        Point += Velocity;
    }
 
    public void ClipToPlanes(PipelinePlane[] planes)
    {
        foreach(PipelinePlane plane in planes)
            ClipPointAndVelocityToPlane(plane.WorldPlane);
    }
 
    private void ClipPointAndVelocityToPlane(Plane plane)
    {
        float distance;
        if (!plane.IsPointInfront( Point, out distance))
        {
            Point = plane.Clip(Point);
            RemoveVectorComponentFromVelocity(plane.normal);
        }
    }
 
    private void RemoveVectorComponentFromVelocity(Vector3 vector)
    {
        float velocityDistanceAlongVector = vector.DotProduct(Velocity);
        Velocity -= vector * velocityDistanceAlongVector;
    }
}

The above code is much easier to read and is easy to debug and maintain. Again, the need for comments is largely gone.

Unit Tests

The unit test project contains 21 tests covering most of the maths routines. These tests are extremely simple and are by no means complete. But if the project is developed further, more tests can be added easily. Likewise, if bugs are found, new tests can be added to confirm the fix and ensure it doesn't pop up again.

Another benefit of having unit tests in place appears when optimizations are required. Take the Length method of the 3D Vector object as shown below.

public float Length()
{
    return (float)System.Math.Sqrt(z * z + y * y + x * x);
}

The Length method above has been implemented using the System.Math.Sqrt method. While the square root operation is accurate, it is also usually a costly one, so developers often replace the square root operation with something more efficient but less accurate. The degree to which we are willing to sacrifice accuracy for an increase in efficiency can be encoded in our unit tests by specifying a tolerance value as shown below.

[Test]
public void VectorLengthTest()
{
    Vector3 vector = new Vector3(221, 132, 40);
    
    float vectorTolerance = 0.00001f;
    float expectedLength = (float)System.Math.Sqrt(result.x*result.x + result.y*result.y + result.z*result.z);
 
    Assert.AreEqual(expectedLength, vector.Length(), vectorTolerance, "Vector Length is not within acceptable tolerance");
}

This allows us to set the boundaries within which to optimize. I used this approach before when writing a custom software version of the Mascot Capsule 3D API while at Upstart Games. I recorded the output of the math routines from Mascot Capsule and set up a series of tests with a specified tolerance. I used these tests to ensure my own pipeline produced the same values within the specified tolerance. This allowed me to emulate and optimize at the same time without breaking anything.

Conclusion

Simple code refactoring and unit testing can be powerful tools to help developers take control of their code no matter what type of software they develop. You can download the full source (including unit tests) for the project and the binaries below. Again, these are not perfect examples but they do demonstrate some of the benefits of using refactoring and unit tests.

JellyVector.zip (54.08 kb) 

JellyVector.bin.zip (11.76 kb)

[Update: The Amiga demo I referred to earlier was called Krest Mass Leftovers by Anarchy. It was released in 1992.]


About 6 months ago, I briefly tried out JetBrains IntelliJ. As far as Java development goes, there doesn't appear to be a more impressive IDE. I had heard of JetBrains ReSharper for Visual Studio at that point but I hadn't tried it out. Needless to say, once you've given ReSharper a go there's just no going back.

When the ReSharper 4.0 EAP was announced I started testing it out. I did my bit for JetBrains and submitted bug reports when issues popped up. When it was finally released, I ordered my copy.

ReSharper 4.0 is packed with useful features. One of the new features that caught my eye is the Annotated Framework. On its own, the Annotated Framework doesn't appear as significant as the many other features provided. It's the last feature documented right down at the bottom of the Code Analysis feature page on the JetBrains site. But in terms of software development practices, it's an interesting indication of what direction JetBrains see developers taking to improve the quality of their code.

What is the Annotated Framework?

The Annotated Framework is contained in the JetBrains.Annotations namespace and consists of the following custom attributes (as described on the JetBrains web site).

  • StringFormatMethodAttribute (for methods that take format strings as parameters)
  • InvokerParameterNameAttribute (for methods with string literal arguments that should match one of caller parameters)
  • AssertionMethodAttribute (for assertion methods)
  • AssertionConditionAttribute (for condition parameters of assertion methods)
  • TerminatesProgramAttribute (for methods that terminate control flow)
  • CanBeNullAttribute (for values that can be null)
  • NotNullAttribute (for values that can not be null)

The idea is that you use these attributes to decorate your methods and parameters so that ReSharper can provide you with better live code analysis. For example, the code below shows a custom method called MyFormat that has the same signature as String.Format. This method uses composite formatting which means it takes a string that contains any number of format items (like {0}, {1}, etc.) in addition to that many parameters, each of which corresponds to a format item.

[StringFormatMethod("text")]
public void MyFormat( [NotNull] string text, params object[] args )
{
    // Output the text specified.
}

The method signature above is decorated with some attributes from the JetBrains.Annotation namespace. The first is a method attribute, StringFormatMethod and it takes a single parameter. The parameter is used to specify the name of the parameter in the method call that contains the composite format string. In this case, the string is called "text". The second is a parameter attribute, NotNull. This attribute is used to indicate that the parameter it decorates should never be null.

When you make calls to the MyFormat method, ReSharper will inform you of any instances where your code is not complying with the requirements of these attributes. Below is a usage example.

public void TestMethod()
{
    int i = 0;
    int j = 0;
 
    // The call to MyFormat below is flagged by ReSharper as containing problems
    // because it has two format items, but only one additional paramter.
    MyFormat( "{0}, {1}", i );
 
    // The call to MyFormat below is flagged by ReSharper as containing problems
    // because the composite format string parameter is null.
    MyFormat( null, i, j );
    
    // The call to MyFormat below is not flagged because the composite format string 
    // is not null and both format items are accounted for in the parameters i and j.
    MyFormat( "{0}, {1}", i, j );
}

In addition to being able to use these attributes in your own code, JetBrains have annotated most of the .NET Framework so you get a nice benefit there also.

To use the code annotation attributes in your own code, you can either add a reference to the JetBrains.Annotations.dll (found in the ReSharper install directory) or, copy the default implementation straight into your project.

The key point to keep in mind about the Annotated Framework is that it is designed to give you better code analysis. There are no runtime implications here, in other words no exceptions will be thrown if a parameter you've annotated with the NotNullAttribute ends up containing a null value.

But wouldn't it be nice if the Annotated Framework did allow you to enforce runtime constraints? Spec# (from Microsoft Research) has this facility and more. Check Scott Hanselman's podcast on Spec# for more info.

As it is the Annotated Framework looks like a step towards Design by Contract for C# developers.

Design by Contract

According to Wikipedia, Design by Contract is an approach to software design whereby 'software designers should define precise verifiable interface specifications for software components based upon the theory of abstract data types and the conceptual metaphor of a business contract.'

For a method on a class object, this boils down to the following.

  1. Specifying a precondition for the method. The caller of the method is obliged to meet this precondition and this frees the method from having to handle the precondition.
  2. Specifying a postcondition for the method. The method will guarantee this postcondition on exit. This frees the caller from having to handle the postcondition.
  3. Specifying an invariant. The method assumes this value on entry and guarantees it on exit.

The extent to which C# currently supports Design By Contract natively is type checking. For example, the code below shows a method that allows the caller to set an email address.

public void SetEmail( string email )
{
    // Store the email address.    
}

The contract being enforced by C# natively for this method guarantees that the email parameter must be a string object. But an email address is more than just a string. It's a string that contains characters in a specific pattern. In the code below I've made the contract less vague with a fictitious Regex attribute.

public void SetEmail( [Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")] string email )
{
    // Store the email address.
}

In the code above I've made the contract explicit. It implies that the method should only accept strings that contain valid email addresses that match the specified regular expression for an email address. This requirement is not buried inside the method body.

I've read some interesting blog posts recently describing different approaches to enforcing contracts as described above. Fredrik Normén and Roger Alsing have both written about this topic.

Extending the Annotated Framework

As a test, I decided to try and extend the annotated framework so that it also provides run-time checking. There are a number of difficulties with the idea of enforcing contracts with attributes at run-time using the current version of C#. Although you can use reflection to get details about the parameters of a method, it is not possible to use this approach to retrieve the value of these parameters at run-time. If you could get at these values easily it would mean you could decorate your methods to describe a contract and then assert the contract with a simple call to an contract method as shown below.

public void SetEmail([Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")] string email)
{
    Contract.Demand();
    
    // Continue with method body
}
 
public static class Contract
{
    public static void Demand()
    {
        var stack = new StackTrace(false);
        var frame = stack.GetFrame(1);
        var method = frame.GetMethod();
 
        foreach (var parameter in method.GetParameters())
        {
            foreach (var attribute in parameter.GetCustomAttributes(false))
            {
                var validator = attribute as ValidatorAttribute;
                if( validator != null)
                {
                    validator.Validate(parameter.Name, parameter.Value); // NOTE: parameter.Value is a fictitious property.
                }
            }
        }
    }
}

The code above assumes that all Custom Validation Attributes derive from a ValidatorAttrbiute class whose interface contains a method called Validate. It also uses a fictitious property of the ParameterInfo object parameter.

To get the above code to compile and run, I had to pass in the parameter values and remove the reference to the fictitious ParameterInfo Value property as shown below.

public void SetEmail([Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")] string email)
{
    Contract.Demand( email ); // Pass in the method parameters.
 
    // Continue with method body        
}
 
public static class Contract
{
    public static void Demand(params object[] parameters)
    {
        var stack = new StackTrace(false);
        var frame = stack.GetFrame(1);
        var method = frame.GetMethod();
 
        var parameterIndex = 0;
        foreach (var parameter in method.GetParameters())
        {
            foreach (var attribute in parameter.GetCustomAttributes(false))
            {
                var validator = attribute as ValidatorAttribute;
                if( validator != null)
                {
                    validator.Validate(parameter.Name, parameters[parameterIndex]);
                }
            }
            parameterIndex++;
        }
    }
}

This requires that all the parameters appear in the call to Contract.Demand, and they must occur in the same order as the containing method. Apart from the performance issues that come with using reflection, the approach I've tested above is just not as clean as other simpler approaches to contract validation. And it doesn't come close to what can be done with Spec#.

Conclusion

The new ReSharper Annotated Framework does exactly what it's supposed to. It improves code analysis. It also encourages a Design by Contract approach to software development. In effect, it offers a first line of defense against vague interfaces and focuses the developer on establishing expectations before and after method calls. It will be interesting to see if, or how this influences the C# community in general and also how this in turn influences Microsoft when advancing the C# language. Let's hope that Microsoft Research get the chance to have Spec# improvements integrated into C#.

You can find out more about ReSharper 4.0 at the JetBrains web site.