Thought Cloud TDD #6: Get your thoughts in order

I found it backwards that you have to create a thought before you create a cloud.

public ICommand AddCloud
{
    get
    {
        return MakeCommand
            .Do(delegate
            {
                Thought thought = _identity.NewThought();
                _identity.NewCloud(thought);
            });
    }
}

A thought is part of a cloud, so the cloud should be a predecessor. But since we made a thought part of the cloud’s key, the thought is a predecessor.

Rule #3: Facts that are part of the key (a.k.a predecessors) must be created first.

It makes more sense if we create a cloud first, and then add thoughts to it. A cloud should be a predecessor of a thought. Let’s make that change.

fact Thought {
key:
    unique;
    Cloud cloud;

...
}

Now we need to create a thought in a Cloud. Let’s move the NewThought method from Identity to Cloud where it belongs.

public partial class Cloud
{
    public Thought NewThought()
    {
        return Community.AddFact(new Thought(this));
    }
}

Now we can create the cloud first.  But hold on! The cloud needs a central thought. The central thought is in the cloud. That’s a circular reference. The predecessor rule makes it impossible to create circular references out of keys. This is an important feature of Correspondence.

But a circular reference can be constructed if one of the fields is mutable. So let’s change the central thought.

fact Cloud {
key:
    unique;
    Identity creator;

mutable:
    Thought centralThought;
}

Now that the central thought is mutable, a cloud can be constructed with no central thought. Then the thought is created, and finally it is assigned.

public ICommand AddCloud
{
    get
    {
        return MakeCommand
            .Do(delegate
            {
                Cloud cloud = _identity.NewCloud();
                Thought thought = cloud.NewThought();
                cloud.CentralThought = thought;
            });
    }
}

This makes more sense. Now thoughts belong to a cloud, not the other way around. After following the ripples out to the edges of the pond, we can compile and pass the unit tests. But we’ve introduced a bug. We just need to write a test to prove it.

Leave a Reply

You must be logged in to post a comment.