Skip navigation

Recently I came across this little snippet of code which contains a serious gotcha!

public class Foo
{
    public Int32 Amount { get; private set; }

    public Foo(Int32 amount)
    {
        Amount = amount;
    }

    static public Foo operator +(Foo x, Foo y)
    {
        x.Amount += y.Amount;

        return x;
    }
}

This has two problems. Firstly the operator is mutating one of the operands, which is a side effect which probably wasn’t intended by the calling code. Secondly, the operator is returning a reference to one of the operands, which is likely to lead to serious and hard to track down bugs in the calling code. Take this simple usage example:

var x = new Foo(10);
var y = new Foo(20);
var sum = x + y;

Console.WriteLine(x.Amount);
Console.WriteLine(y.Amount);
Console.WriteLine(sum.Amount);

You might expect the output to be:
10
20
30

But in fact the output is:
30
20
30

The operator has mutated ‘x’ and returned a reference to it, such that ‘x’ and ‘sum’ are both referencing the same object.

The operator should be re-written like so:

static public Foo operator +(Foo x, Foo y)
{
    return new Foo(x.Amount + y.Amount);
}

To summarize, remember these two simple rules when overriding operators on reference types

  • Do not mutate any of the operands.
  • Do not return a reference to any of the operands.

Oftentimes thinking in terms of ‘design patterns’ can lead to anaylsis paralysis. If it isn’t immediately obvious that a particular pattern is a good solution to a problem, It’s easy become bogged down trying to find some other pattern that fits.

As the idea of design patterns proliferates on the internet, we’re getting a whole generation of coders who’s only real skill is picking a design pattern which most closely matches a problem scenario. Give them a curveball, a scenario which doesn’t precisely fit, and they’ll do two things. First of all they will spend a lot of time trying to figure out which pattern in their toolbox of patterns makes the problem disappear. Secondly, they will try to force their chosen pattern into a solution where it’s completely inappropriate, shoving a square peg into a round hole and wondering why the result is a mess.

The truth is that not all problems are easily solved by simply picking a design pattern. Design patterns are not lego bricks that you push together to build software.

Whenever I hear someone say “what design pattern should I use to xyz..”, I always think to myself, why limit yourself to design patterns? Why restrict yourself when you are in posession of a brain which is capable of creativity, critical thinking, reasoning and abstract thought?

Those are the real tools in your toolkit – not a bunch of ‘patterns’.

So it’s been a little while since my last blog post due to the fact that I have recently moved to London to start a new job. If you’re involved in the .NET community in London then look out for me at various conferences and user groups in the new year!

Following on from my previous post which explained the concept of an object who’s publicly exposed interface adapted based on its state, in this post I’ll explain how the technique can be used to create an elegant implementation of the Builder Pattern with a fluent interface.

For the example we’ll take a simple immutable object which represents a Car:

public class Car
{
    public EngineBase Engine { get; private set; }
    public SeatBase DriversSeat { get; private set; }
    public SeatBase PassengerSeat { get; private set; }
    public WheelType Wheels { get; private set; }
    public BreakeBase Brakes { get; private set; }
}

In order to construct a new Car object, we would have to introduce a constructor which includes all of these parameters. This could quickly lead to a huge constructor, potentially with a dozen or more parameters. Constructors like this are often very difficult to work with (I personally try to avoid any more than 3 parameters on any method/constructor). In these situations we may wish to abstract the complex construction logic into a separate ‘Factory’ class which tames the beastly constructor for us, but there is another way – The Fluent Build Context.

public interface ICarBuildContext
{
    ICarBuildContext WithEngine(EngineBase engine);
    ICarBuildContext WithDriversSeat(SeatBase seat);
    ICarBuildContext WithPassengerSeat(SeatBase seat);
    ICarBuildContext WithWheelType(WheelType wheelType);
    ICarBuildContext WithBrakes(BrakeBase breakes);

    Car Build();
}

This interface allows us to construct a Car using a very nice & readable method chain:

var myCar = carBuilder.WithEngine(new DieselEngine())
                      .WithDriversSeat(new LuxuryHeatedSeat())
                      .WithPassengerSeat(new BucketSeat())
                      .WithWheelType(WheelType.Alloy)
                      .WithBrakes(new CeramicBrakes())
                      .Build();

But how do we integrate this interface with our Car class? Through an often under-utilized feature of C# – Explicit Interface Implementation.

public class Car : ICarBuildContext
{
    public EngineBase Engine { get; private set; }
    public SeatBase DriversSeat { get; private set; }
    public SeatBase PassengerSeat { get; private set; }
    public WheelType Wheels { get; private set; }
    public BreakeBase Brakes { get; private set; }

    ICarBuildContext ICarBuildContext.WithEngine(EngineBase engine)
    {
        this.Engine = engine;
        return this;
    }

    ICarBuildContext ICarBuildContext.WithDriversSeat(SeatBase seat)
    {
        this.DriversSeat = seat;
        return this;
    }

    ICarBuildContext ICarBuildContext.WithPassengerSeat(SeatBase seat)
    {
        this.PassengerSeat = seat;
        return this;
    }

    ICarBuildContext ICarBuildContext.WithWheelType(WheelType wheelType)
    {
        this.Wheels = wheelType;
    }

    ICarBuildContext ICarBuildContext.WithBrakes(BrakeBase brakes)
    {
        this.Brakes = brakes;
        return this;
    }

    Car ICarBuildContext.Build()
    {
        return this;
    }
}

By having Car explicitly implement the build context interface itself, we are able to set the private properties on the class – effectively allowing consumers to tunnel through the immutability of the class, but only on our terms. Once the Car has been constructed it becomes immutable again – the interface adapts by context.

The final step in the chain is to suppress the default constructor for Car and introduce a static factory method:

public class Car
{
    private Car() { }

    static public ICarBuildContext BeginBuild()
    {
        return new Car();
    }

    // ...
}

Usage:

var myCar = Car.BeginBuild()
               .WithEngine(new V8Engine())
               .WithDriversSeat(new RacingSeat())
               // No passenger seat - this is a race car!
               .WithWheelType(WheelType.Alloy)
               .WithBrakes(new CeramicBrakes())
               .Build();

In a future blog post I’ll go into detail about how this approach also gives us the ability to build in detailed construction validation into the process. I’ll also go into detail about how we can nest build contexts to build up a complex object graph in one method chain.

Ask any programmer what ‘null’ means in C#, 99% of the time you will hear an answer that closely resembles this:

“Null – Amounting to nothing, non-existent, absent of value”.

Seems legit. But the really interesting thing about this definition is that it’s completely wrong. This is in fact, the English language definition of the word.

Now lets look at the actual C# definition of null:

“The null keyword is a literal that represents a null reference, one that does not refer to any object.”

And the Java definition of null:

“null is the reserved constant used in Java to represent a void reference i.e a pointer to nothing.”

What null means in C# & Java is simply that a pointer is invalid, eg. it does not reference a valid object in memory, nothing more. And yet almost every program I see abuses this simple concept by overloading the null keyword with additional semantics from the English language definition.

One very common example of this is in implementations of the repository pattern. The common convention is that when querying the repository, a return value of null indicates that no data was found for the given search criteria.

var item = myRepository.Get(id = 10);

if (item == null)
{
    return "no item found with Id: 10";
}

// Do something with item...

It is often said that using Exceptions to control program flow is a bad idea. Given our new found understanding of the null keyword in C#, can we honestly say that using invalid pointers is any better?

What are the alternatives?

Lets think for a second about what we’re actually trying to say he when we return null from our repository. We’re trying to express the fact that the value may or may not be present. There is a data type in all programming languages that is perfectly suited to express this dichotomy – boolean.

In functional languages we can use Maybe/Option monads. In object oriented languages we can use the Null Object Pattern. However I think the simplest and most concise approach is to use the tester-doer pattern as described in Framework Design Guidelines (sometimes called the try/can pattern).

if (myRepository.RecordExists(id = 10))
{
    var item = myRepository.Get(id = 10);
}
else
{
    return "no item found with Id: 10";
}

This can be optimised to avoid two queries to the database:


var item;
if (myRepository.TryGetItem(id = 10, out item))
{
    // Do something with item...
}
else
{
    return "no item found with Id: 10";
}

The resulting code is far clearer, and the null keyword is no longer being abused.

Many times you read about some design pattern in a book or online, and you realise that it’s a technique you’ve been using for years – you just didn’t have a  name for it. It can be a bit of a double edged sword; on the one hand it gives you a nice warm fuzzy feeling inside to know that you independently and quite naturally adopted a recognised best practice, but on the other hand you’re disappointed to learn that you haven’t actually innovated or invented something new and exciting.

What I refer to as the “Adaptive Interface Pattern” is I’m sure one of these cases. In this technique, you define a separate interface defining all of the available operations which are available for a given state of an object. In some ways it’s very similar to a state machine, but where the state transition changes the publicly exposed interface of an intersecting set of operations.

The most simple example of this would be a boolean switch. A traditional implementation might look something like this:

class BooleanSwitch
{
    private bool _isSwitchedOn = false;

    public void SwitchOn()
    {
        if (this._isSwitchedOn)
            throw new InvalidOperationException("Switch is already switched on");

        this._isSwitchedOn = true;
    }

    public void SwitchOff()
    {
        if (this._isSwitchedOn == false)
            throw new InvalidOperationException("Switch has not been switched on");

        this._isSwitchedOn = false;
    }
}

Clearly having both SwitchOn() and SwitchOff() methods available at the same time can easily lead to runtime exceptions. It would be better if the SwitchOn() method were only available when the object was switched off, and the SwitchOff() method only available when the object was switched on.

We can accomplish this by defining an interface for each of these states:

interface IOffSwitch
{
    IOnSwitch SwitchOn();
}

interface IOnSwitch
{
    IOffSwitch SwitchOff();
}

Our BooleanSwitch class can now be modified to implement both of these interfaces. And since calling the wrong method will now result in a compile time error, we can do away with the invalid operation exceptions…

class BooleanSwitch : IOnSwitch, IOffSwitch
{
    private bool _isSwitchedOn = false;

    public IOnSwitch SwitchOn()
    {
        this._isSwitchedOn = true;

        return this;
    }

    public IOffSwitch SwitchOff()
    {
        this._isSwitchedOn = false;

        return this;
    }

    //Finally we need to suppress the default constructor and write a factory method to create the object in its default state...
    static public IOffSwitch Create()
    {
        return new BooleanSwitch();
    }
}

Legal documents such as EULA’s are notoriously hard to follow, we’ve all seen (and most likely glossed over) several pages of legal mumbo jumbo after installing a new piece of software.. “Yes Microsoft, I definitely read the entirety of that 35 page EULA, tick”.

But there is a very valid reason that legal documents need to be so verbose. When dealing with legal matters it’s vitally important that there be as little amiguity as possible in the language used. Anything that’s left to interpretation can cause a legal grey area that can be exploited. Unfortunately the English language is not well equipped to meet these requirements, it’s much more suited to expressive, creative writing that’s ambiguous and open to interpretation. The result is a heavily modified English, so called “legal English” that bares very little resemblence to the language we write and speak with on a daily basis. Talk about forcing a square peg into a round hole!

Programming languages seem to be a far better tool for the job. They are completely unambiguous and their structure & syntax far more suited to representing complex conditional logic in a concise way. I would estimate that a complex legal document written in “legal English” could be equally well represented in psuedocode using 20x less characters.

An intetesting topic is what programming language would be best suited to represent legal terms. First things first, it would have to be an open language which is not tied to a specific vendor. It would be pretty bad if Microsoft or Oracle had such influence over legal documentation (it would be like Apple having a patent on the paper that bank notes are printed on). So that’s C#, VB, Objective C and Java discounted. We can also discount lower level languages such as C++. It would be important that the language be able to support multiple paradigms. Object oriented for representing legal entities, and functional for expressing complex rules. Within my own (admittedly limited) experience of programming languages, I would say Python is a good fit! Let me know if you disagree!

Just imagine running unit tests for a mobile phone contract, or writing functional specs for an employment contract using Cucumber! How much faster could the government pass new laws if they had a continuous delivery process? Truly the world would be a better place if all lawyers were software engineers…

Choosing appropriate names for data types and members is something every developer has to do on a daily basis, whether it be database columns, object properties, or XML schema types etc. In every case, choosing a good descriptive name is imperative to aid both our own understanding of the project, and any other developer who may be working with it, now or in the future.

As such we should make a point of having a certain level of expertise in natural language and linguistics, as well as machine code. I’m sure I’m not the only developer who keeps a thesaurus on his desk for quick reference whilst coding! In addition to having a firm grasp of linguistics, every developer ought to have an understanding of taxonomy, and how to apply that on a conceptual level – ideally to the point of borderline obsessive compulsive disorder, or beyond…

So! Lets say we’re designing a data schema which will represent a postal address value. A naive implementation might look something like this:

  • Building Name / Number
  • Street
  • Town
  • County
  • Postcode
  • Country

This has several problems (many of which will be strikingly obvious if you’re not British). First of all, it’s specific for UK addresses. The concept of a “County” makes no sense outside of the UK. Inversely, the concept of a “State” or “Prefecture” makes no sense to our data schema without some sort of mapping convention. The same is true of “Postcode” when contrasted with “Zip Code“. While “Postcode” might seem like enough of an abstraction, we actually need to look a lot deeper than this, and figure out exactly what it is we’re trying to represent here..

Finding the terminal hypernym

Hypernymy is a term used in linguistics to describe the semantic relation of class and sub-class. A “terminal” hypernym, a term sometimes used in the natural language processing community, is the most abstracted hypernym one can reach while still adequately describing the characteristics of the entity. For example, take the common noun “hatchback“, representing a specific type of car. In this case “car” is the hypernym of “hatchback“. We can follow this through several stages…

  • Hatchback
  • Is a type of “car
  • Is a type of “wheeled vehicle” (as opposed to a tracked or railed vehicle)
  • Is a type of “vehicle
  • Is a type of “machine

We could go on endlessly finding more and more abstract hypernyms, however I would personally draw the line at “vehicle”. “Machine” is so abstract that it’s no longer sufficient to adequately describe the entity, therefore “vehicle” is our terminal hypernym. Now where you draw that line often depends entirely on the domain being modeled, it’s up to you to decide how much abstraction is useful, and at what point it becomes cumbersome. We can follow the same procedure when describing the colour of our car…

  • Racing green
  • Is a type of “green
  • Is a type of “colour< terminal hypernym
  • Is a type of “light
  • Is a type of “electromagnetic radiation< way beyond being useful in our domain

Can you imagine a car salesman saying to you “and what shade of electromagnetic radiation would you like that in, Mr Smith?” – you would look at him like he just landed a UFO in your front garden. But like I said, it depends entirely on the domain being modeled, Henry Ford might well have said something like “and what shade of Black would you like that in?” – where you draw the line of abstraction is entirely domain driven.

So lets apply the same rules to our address schema, we might end up with something like the following:

  • Plot Identity
    • Plot” is abstract enough to represent anything ranging from a complex in a business park to a trailer in a trailer park.
    • Identity” can represent the identity of the plot whether it be a building name or a house number, or by some other means.
  • Thoroughfare
    • Can represent a “road“, “avenue“, “street“, or whatever local variations may exist.
  • Municipality
    • Is adequate to represent a “town“, “city“, or any urbanization.
  • Administrative division
    • Tricky one. While abstract enough to represent a “county“, “state“, “province” or “prefecture“, it could be deemed too abstract for the domain. Then again, the fact that it’s “administrative” could be too concrete!
  • Postal service identifier
    • As far as I’m aware, all postal /zip codes are specific identifiers defined by a domestic postal service.
  • Geographical region
    • Another tricky one! In most domains “country” would be plenty abstract enough, but it is not always the case (for example strictly speaking “Taiwan” is not a country, it’s a rogue state). “geographical region” is abstract enough to represent these kinds of edge cases, if your domain requires it.

It can often be a process of trial and error. On the one hand, you don’t want your data schema to be so concrete that it’s inflexible; on the other, you don’t want it to be so abstract that it’s difficult to understand what is being represented. Finding the sweet spot requires a sufficient understanding of the domain such that you can put your obsessive compulsive tendencies to work.

I saw an interesting question on StackOverflow yesterday, the poster asked the question “can making a method private improve performance?”. Of course the answer is a resounding no, but it got me thinking about how other access modifiers might affect performance. It’s no surprise that marking a method as “static” reduces call time significantly (in relation to an instance level method). Another access modifier that can help improve performance is “sealed”, since the CLR can now take a potentially big shortcut on the vtable when resolving the method call.

At the same time, the “virtual” access modifier can reduce performance, as the CLR has to query the vtable to resolve the method at runtime.

The big wildcards are “volatile” and the new “async” modifier in C#5. Both of those access modiers behave a little more like aspects, where the compiler generates extra code at compile time. These access modifiers also have a negative impact on performance, although not for the same reasons as “virtual”.

XML documentation comments in C# are a great feature, especially if you’ve used an automatic documentation generator such as Sandcastle. Being able to generate documentation directly from source code is nothing new of course, Doxygen has been doing a great job of it since 1997, and I’m sure it wasn’t an original concept then.

But generating documentation from source code introduces its own set of considerations. Code comments are no longer private messages or dialog between developers, or even messages to your future self. Making the decision to generate documentation from source code requires a subtle shift in your mentality as you write your XML comments.

  • You can no longer assume that people reading your comments have that particular source code file open, or even have access to the source code at all.
  • Since your comment readership may not have access to the source code, you need to adopt a different strategy when it comes to referencing other code elements.

There are a set of seldom used and often misunderstood elements within the XML documentation syntax that can solve these issues, and a standardised way of referencing code elements. Elements such as <see> and <seealso> allow you to reference other code elements, and are commonly converted into clickable hyperlinks in the generated documentation file.
Take this simple method as an example:

public void AddComponent(IComponent component)
{
    if (component == null)
        throw new ArgumentNullException("component");

    this.components.Add(component);
}

When it comes to commenting this method, the main point that needs to be documented is the fact that this method will reject a null reference passed to the component parameter, and in such cases will throw an ArgumentNullException.

Immediately we have an external element that we need to reference – ArgumentNullException, and we can accomplish this by adding an <exception> tag to the XML comments..

/// <summary>
/// Adds a component to the container.
/// </summary>
/// <exception cref="T:System.ArgumentNullException">
/// Thrown if the value passed to the <paramref="component"/> parameter is null.
/// </exception>
public void AddComponent(IComponent component)
{
    if (component == null)
        throw new ArgumentNullException("component");

    this.components.Add(component);
}

The tag has a cref attribute which lets us reference an external code element. As you can see, the name of the external code element must be fully qualified. In addition to the fully qualified name of the external code element, which can also hint at what the code element is… ‘T:’ in this case means we are referring to a Type, but there are several other options:

  • ‘N’ – Namespace – “N:System.Diagnostics”
  • ‘P’ – Property – “P:System.String.Length”
  • ‘M’ – Method – “M:System.Diagnostics.Debug.Assert”
  • ‘E’ – Event – “E:System.ComponentModel.INotifyPropertyChanged.PropertyChanged”

Now, expanding our simple example method to raise an event, we can see that fully utilizing the ability to reference external code elements in the XML documentation makes for clearer, more precise generated documentation which is far easier to navigate. And as a bonus, if you’re using Sandcastle to generate your documentation, anywhere you use a type reference to a class in the Framework Class Library, your documentation will automatically contain a link to the relevent page on MSDN!
Final example:

/// <summary>
/// Adds a <see cref="T:System.ComponentModel.IComponent"/> instance to the container.
/// </summary>
/// <exception cref="T:System.ArgumentNullException">
/// Thrown if the value passed to the <paramref="component"/> parameter is null.
/// </exception>
/// <remarks>
/// Calling this method will raise the <see cref="E:MyNamespace.MyContainer.ComponentAdded"/> event.
/// </remarks>
public void AddComponent(IComponent component)
{
    if (component == null)
        throw new ArgumentNullException("component");

    this.components.Add(component);

    // Raise a component added event...
    this.OnComponentAdded(component);
}

Follow

Get every new post delivered to your Inbox.