Saturday, October 31, 2009

Design focus on simplicity

Recently I did design review for a simple scenario that roughly corresponds to Command pattern extensible with inspection or modification of information prior to and subsequent to command execution. Simple design for a simple scenario. Though it wasn’t the case.

interface ICommand
{
    object Do(object arg);
}

interface ICommandInspector
{
    object BeforeCall(object arg);

    object AfterCall(object ret);
}

abstract class Command<T> : ICommand
    where T : ICommandInspector, new()
{
    public object Do(object arg)
    {
        var insp = new T();
        arg = insp.BeforeCall(arg);

        var ret = DoCore(arg);

        return insp.AfterCall(ret);
    }

    protected abstract object DoCore(object arg);
}

Besides Command pattern itself one more concept was introduced – command inspector that handles pre and post processing requirement. By the way does this concept sound familiar to you?

For example, the following inspector measures command execution time.

class PerformanceCounterInspector : ICommandInspector
{
    private readonly Stopwatch m_sw = new Stopwatch();

    public object BeforeCall(object arg)
    {
        m_sw.Start();
        return arg;
    }

    public object AfterCall(object ret)
    {
        m_sw.Stop();
        Console.WriteLine(m_sw.Elapsed);
        return ret;
    }
}

The solution addresses the requirements. So, where is the problem? Complexity! Let us not forget that managing complexity is primary topic in software development. Simpler solution that does the job should be preferable.

Proposed solution makes developers to divide tasks into command and inspector aspects although the only consumer of inspector concept is command itself and seems it can be a part of it. On the other hand it is not that easy to understand what inspector does unless you have good understanding of how it is consumed by the command. A complex solution to a simple problem.

We can try to keep solution as simple as possible from employed concepts perspective. How to employ only Command concept but still address requirements? This is where Decorator pattern is handy.

abstract class CommandDecorator : ICommand
{
    private readonly ICommand m_cmd;

    protected CommandDecorator(ICommand cmd)
    {
        m_cmd = cmd;
    }

    public virtual object Do(object arg)
    {
        return m_cmd.Do(arg);
    }
}

It is easier to understand the intent of added functionality as it is not separated from command concept.

class PerformanceCounterCommandDecorator : CommandDecorator
{
    public PerformanceCounterCommandDecorator(ICommand cmd)
        : base(cmd)
    {
    }

    public override object Do(object arg)
    {
        var sw = new Stopwatch();
        sw.Start();

        var ret = base.Do(arg);

        sw.Stop();
        Console.WriteLine(sw.Elapsed);
        return ret;
    }
}

The Decorator pattern naturally promotes dynamic functionality composition which is especially important when there are several ways to compose functionality. On the other hand decorator pattern promotes composition over inheritance (good thing as inheritance violates encapsulation and may lead to fragile designs). 

Summary:

  • DO NOT introduce new concepts into your design unless you have strong reasons to do so
  • DO always assess complexity of your design
  • DO strive for concepts that support composition

Tuesday, September 29, 2009

False promises

I do not like ads because the way it is made and presented makes me think that the advertisement tells me what product cannot do instead of what it can do (too many promises sound like no promises at all).

As a developer you advertise your component’s capabilities through its API. Unfortunately it so happens that we (developers) sometimes are no better than those “can do everything” magic product advertisers.

Consider the following public API (it is abstract but enough to illustrate the point):

class Container
{
    public void Put<T>(T item, string name);
    public T Get<T>(string name);
}

Let’s look at it assuming that the author had “Self-Documenting Object Models” principle in mind. What are your expectations? I would made assumptions like:

  • it is a container for named of items of arbitrary type
  • it won’t cause unnecessary boxing/unboxing for items of value types as both methods are generic
  • two items with the same name but of different type won’t overlap or otherwise want kind of behavior should I expect in cases like (I used explicit type parameter specification for illustration purposes)
    Container c = ...; // initialized elsewhere
    c.Put<int>(1, "one");
    var d = c.Get<string>("one");
    

And now let’s look at the implementation (I’ve seen similar things several times):

class Container
{
    Dictionary<string, object> items = new Dictionary<string, object>();

    public void Put<T>(T item, string name)
    {
        items[name] = item;
    }

    public T Get<T>(string name)
    {
        return (T)items[name];
    }
}

How many of our expectations are met? Only one – it is a container after all. It seems that public API advertised nonexistent capabilities. It turns out the author had different set of assumptions:

  • name is the only identifier of an item
  • generic methods allow to avoid additional casts

Wow! That is absolutely different story. Instead of advertised capabilities I got questionable advantage of not having to explicitly cast (is it really an advantage in this case?):

Container c = ...; // initialized elsewhere
var one = c.Get<int>("one");
// compared to
var one = (int)c.Get("one");

An API must be minimal and sufficient to fulfill its scenarios and clearly communicate type’s capabilities. Container for named items treated as objects may look like this:

class Container
{
    Dictionary<string, object> items = new Dictionary<string, object>();

    public void Put(object item, string name)
    {
        items[name] = item;
    }

    public object Get(string name)
    {
        return items[name];
    }
}

It doesn’t meet my original expectations but at least it makes no false promises which may lead to confusion.

Summary:

  • DO carefully consider usage scenarios
  • DO provide minimal and sufficient API that clearly communicates type’s capabilities

Sunday, August 30, 2009

Single Responsibility Principle – discovering violations

Single Responsibility Principle states:

A class should have only one reason to change. Responsibilities are reasons to change.

Violations of this principle leave you face to face with fragile design and all the maintenance nightmares it implies. Unfortunately there is no hundred percent way to prevent it from happening – it is just the nature of design.

Common sense often helps to spot multiple responsibilities mixed within a single class. However some cases are tricky and require careful attention. This is where design assessment comes in. An object can be looked at from two perspectives:

  • Data
    • What it knows?
    • What connections between objects it maintains?
  • Behavior
    • What it decides?
    • What services it performs?

Uncontrolled growth in any of the groups can lead to god class (data or behavior forms respectively).

What it knows?

Keeping related data and behavior in one place is essential to building consistent abstractions. Failing to do so may lead to:

  • unnecessary details disclosure (by exposing all the data necessary to implement behavior)
  • duplicated or inconsistent behavior (due to poor discoverability)
  • uncontrolled growth (as it is harder to see the big picture due to scattered behavior)

Following Information Expert pattern promotes the idea:

Assign a responsibility to the class that has the information needed to fulfill it.

Consider an example below where publicly visible person name could be incorrectly used to distinguish people (as names are not unique). Thus it makes sense to let Person type to define what equality means as it has all the necessary information (social security number). This will prevent introduction of several inconsistent equality behaviors across the code base.

class Person
{
    private string name;
    // Keep social security number in private
    private string ssn;
    
    // Names are not unique
    public string Name { get { return name; } }

    public override bool Equals(object obj)
    {
        if (obj == null || obj.GetType() != typeof(Person))
            return false;
        // Names cannot be used to identify people unlike 
        // social security number
        return ssn == ((Person) obj).ssn;
    }
}

Keeping related data and behavior in one place is as important as not letting not related sets of data/behavior to be put into the same class. Most of the methods defined on a class should be using most of the data members most of the time.

Many of you started to work still being a student. Employer needs your abilities to work and most likely he isn’t interested in that you still studying.

// Smart student - works and studies
class Student
{
    private string name;
    private int knowledge;
    private Func<Course, bool> preferences;
    private int experience;

    public string Name
    {
        get { return name;}
    }

    public void Study()
    {
        knowledge++;
    }

    public void Enlist(IEnumerable<Course> courses)
    {
        // Select appropriate courses and enlist
        foreach (var course in courses.Where(preferences))
            course.Enlist(name);
    }

    public void Work()
    {
        experience++;
    }

    public void Sign(Contract contract)
    {
        // sign job contract with your name
        contract.Agree(name);
    }
}

This class clearly has more than one responsibility and the fact that study related methods and work related methods both operate on a subset of data shows that. However separation can solve the problem.

class Student
{
    private Person person;
    private int knowledge;
    private Func<Course, bool> preferences;

    public Student(Person newStudent)
    {
        person = newStudent;
    }

    public void Study()
    {
        knowledge++;
    }

    public void Enlist(IEnumerable<Course> courses)
    {
        // Select appropriate courses and enlist
        foreach(var course in courses.Where(preferences))
            course.Enlist(person.Name);
    }
}

class Employee
{
    private Person person;
    private int experience;

    public Employee(Person newEmployee)
    {
        person = newEmployee;
    }

    public void Work()
    {
        experience++;
    }

    public void Sign(Contract contract)
    {
        // sign job contract with person's name
        contract.Agree(person.Name);
    }
}

What connections between objects it maintains?

What is connection between objects anyway? For example, a document can be seen a set of glyphs (text, graphics, structural elements like columns and rows). The connection between them is the fact they belong to the same document. So basically the document maintains connection between glyphs. So what must attract our attention in this area to spot Single Responsibility Principle violations?

If the object maintains connections between objects of different abstraction levels it is highly likely that it does someone’s job.

Consider an object model where a man wants to build a house. Man class maintains connection between tasks (got from an architect) and workers to build a house.  And this where different abstraction levels (from domain model perspective) meet – a man hired foreman who is supposed to manage construction but it so happens that a man wants to control everything and treats hired foreman as a simple worker giving him tasks to do in not necessarily correct order. Basically a man got foreman’s responsibility (or in other words foreman abstraction isn't consistent).

abstract class Task
{
    public bool Completed { get; private set; }

    public virtual void Do()
    {
        DoCore();
        Completed = true;
    }

    protected abstract void DoCore();
}

class Worker
{
    public virtual bool CanDo(Task task)
    {
        // Can do everything worker =)
        return true;
    }

    public virtual void Accept(Task task)
    {
        task.Do();
    }
}

class Foreman : Worker
{
    IEnumerable<Worker> workers;
    Func<Task, bool> next;

    public override bool CanDo(Task task)
    {
        // Can do things only on time or otherwise the house 
        // won't stand long
        return next(task);
    }  

    public override void Accept(Task task)
    {
        // Foremen looks for a worker who can do the task
        workers.Where(w => w.CanDo(task)).First().Accept(task);
    }
}

// A man who tries to manage his new house construction
class Man
{
    IEnumerable<Task> construction;
    Foreman foremen;

    public void BuildHouse()
    {
        IEnumerable<Task> tasks;
        // As far as we don't know what to do next we'll ask foreman
        while ((tasks = construction.Where(t => foremen.CanDo(t) && !t.Completed)).Any())
        {
            foreach (var task in tasks)
            {
                foremen.Accept(task);
            }
        }
    }
}

A man must let foreman to manage tasks or become a foreman himself (we’ll take first approach or otherwise the house won't stand long).

class Foreman
{
    IEnumerable<Worker> workers;
    Func<Task, bool> next;
    
    public void Manage(IEnumerable<Task> construction)
    {
        IEnumerable<Task> tasks;
        // Foreman selects tasks that are not completed and must be done next
        while((tasks = construction.Where(t => !t.Completed && next(t))).Any())
        {
            foreach (var task in tasks)
            {
                workers.Where(w => w.CanDo(task)).First().Accept(task);
            }
        }
    }
}

class Man
{
    IEnumerable<Task> construction;
    Foreman foremen;

    public void BuildHouse()
    {
        // Foreman takes the whole construction to manage
        foremen.Manage(construction);
        // Just check that everything is done
        if (construction.Where(t => !t.Completed).Any())
        {
            throw new InvalidOperationException("Something isn't done!");
        }
    }
}

Now abstractions are leveled - man allows construction to be managed by foreman (foreman abstraction is now consistent).

What it decides?

It is quite uncomfortable when someone asks you for your opinion and then makes his own mind and tells you what and how to do. Why don’t tell what to do in the first place and let me decide how to do it. Isn’t that looking like you are doing my job? This is what Tell Don’t Ask Principle is about:

As the caller, you should not make decisions based on the called object’s state and then change the object’s state.

Here is couple of teenager definitions – which do you think makes more sense?

class Teenager
{
    private int age;
    private List<string> clothes;

    // Is it age that drives your clothes preferences?
    public int Age { get { return age; } }

    // Do you really wan't your parents to dress you 
    // just based on your age?
    public void TakeNew(string clothing)
    {
        clothes.Add(clothing);
    }
}

// ... or

class Teenager
{
    private List<string> clothes;
    // Preference is something personal
    private Func<string, bool> preference;

    // And you want to able to select clothes based on your 
    // preferences?
    public bool SelectNew(IEnumerable<string> shop)
    {
        var clothing = shop.Where(preference).FirstOrDefault();
        if (clothing != null)
        {
            // Got something you like
            clothes.Add(clothing);
        }
        // Tell your parents whether you need to go to other shop =)
        return clothing != null;
    }
}

By violating Tell Don’t Ask principle caller gains responsibility on make decision instead of the called one. Busted!

What services it performs?

Service provider can do the job by itself or ask collaborators for help. Keeping dependencies on collaborators explicit facilitate controlled class growth. Classes should not contain more objects than a developer can fit in his or her short-term memory (5-7). Otherwise it is possible to introduce responsibilities into class which are either duplicated or not related. Whenever number of collaborators growth more than 5-7 you should consider whether all collaborators still help you to form single abstraction or a new one was introduced.

On the other hand it makes sense to look at consumers and in particular on how they consume supplied services. If consumers use different subsets of provided services it may be a sign that class captures more than one responsibility.

Recall the student example. Original class has two consumers: employer and university (for example). Both consumers were interested in a subset of provided methods (working and studying related respectively). And as we discovered the class captured two abstractions: employee and student.

Summary:

  • DO pay careful attention to responsibilities assignment
  • CONSIDER decomposing class into areas (what it knows, maintains, does and decides) and performing assessment in each area

Thursday, August 13, 2009

Inject or locate dependencies?

Inversion of Control pattern allows to decouple components (consumers) from their dependencies and takes care of dependencies location and lifetime management through delegation of these responsibilities to external (with respect to dependent type) component. This pattern actively used in composite application development.

Inversion of Control comes in two flavors (Unity provides both capabilities):

Service locator holds references to services and knows how to locate them. It is further used by dependent component to obtain necessary services. In other words consumers play active role.

interface IService
{
    void Do();
}

class ActiveConsumer
{
    private readonly IUnityContainer locator;

    // Reference to service locator comes from outside
    public ActiveConsumer(IUnityContainer serviceLocator)
    {
        locator = serviceLocator;
    }

    public void Do()
    {
        // In order to fulfill its task active consumer relies 
        // on service implementation that is obtained on demand 
        // from service locator
        var service = locator.Resolve<IService>();
        service.Do();
    }
}

Dependency injection makes dependent components passive (little or no work is done to get its dependencies). The only responsibility consumers still care about is to express their dependencies somehow (the way dependencies are expressed depends on pattern implementation, but for this example we will use single constructor automatic injection supported by Unity).

class PassiveConsumer
{
    private readonly IService service;

    // This constructor is used to inject service dependency
    public PassiveConsumer(IService svc)
    {
        service = svc;
    }

    public void Do()
    {
        // We got this dependency from outside and done nothing 
        // to let it happen - so just use it
        service.Do();
    }
}

...

// At this point container resolves consumer's dependency 
// and injects it during construction 
var passiveConsumer = container.Resolve<PassiveConsumer>();
passiveConsumer.Do();

So what is the difference?

First, is dependency from service locator appropriate? If the component in question is supposed to be reused by others you may end up with putting unnecessary constraints (for example you are using some open source service locator but developers that could reuse your component are not allowed to use any open source stuff due to customer’s demand and thus won’t be able to reuse the component).

Second, dependencies visibility. Service locator makes consumer’s “real” dependencies hidden and dependency from service locator itself visible. When dependencies are explicit it is much easier to understand dependent class. Explicit dependencies allows you to assess and control the growth of the component. For example, if your component accepts 10 services in its constructor it may be a sign that it does, or knows or decides too much and it is time to split it. Consider the same thing when using service locator. In order for you to spot number of dependencies you need to look for all unique usage occurrences of service locator. It is not that hard with modern IDE but still it is not that easy as looking at component’s contract.

On the other hand, it makes sense to consider the audience of the component. If it will be reused by others and dependencies are hidden it may require deep knowledge of component’s inner workings in order to use it.

Third, consumer’s relation with dependency. Dependency injection promotes constant relations (from lifetime perspective). Consumer obtains its dependency at construction time and lives with it. On the other hand service locator compasses to temporary relations – get service instance when it is time, call its methods, discard it. Why discard? Because if the component has a constant relation why not use dependency injection otherwise which gives you explicit dependencies?

But anyway, what particular case forces locator usage? When consumer has longer lifetime than its dependency. For example, you are writing smart client application. You organized presentation layer using Model-View-Presenter pattern. Presenter calls remote service in response to user interaction. View controlled by a presenter can be opened for a long time. If presenter gets remote service proxy dependency only once it may happen that proxy will go into faulted state (for example, due to network connectivity problems) and any subsequent calls to it will result in exception. So it is better to dispose proxy every time a particular task accomplished and create new one when new task is on the way or cache it and in response to proxy going faulted create a new one (which is of course harder as long as you need to handle all cases where it used and maintain cache). Thus it seems that service locator is more appropriate in this case.

However we can make short lived dependencies explicit. Let’s assume that IService implementation instance must be disposed every time it is used.

interface IService : IDisposable
{
    void Do();
}

// This is still active consumer as it uses service locator to get service instance
class Consumer
{
    private readonly IUnityContainer locator;

    public ActiveConsumer(IUnityContainer serviceLocator)
    {
        locator = serviceLocator;
    }

    public void Do()
    {
        using (var service = locator.Resolve<IService>())
        {
            service.Do();
        }
    }
}

Service locator has wide surface (it terms of services it can provide) and this makes consumer’s contract opaque. What we need to do is narrow the surface but still provide ability to create service instances (as long as we need to dispose them every time). Abstract factory will do the thing. Factory provides clear connection with service it creates. On the other hand we need to make consumer’s dependency from factory explicit. We will use dependency injection.

interface IServiceFactory
{
    IService CreateService();
}

// Consumer is no longer active as it gets its dependencies from outside
class Consumer
{
    private readonly IServiceFactory factory;

    // The dependency is now explicit
    public PassiveConsumer(IServiceFactory serviceFactory)
    {
        factory = serviceFactory;
    }

    public void Do()
    {
        // We still can create service instances on demand
        using (var service = factory.CreateService())
        {
            service.Do();
        }
    }
}

How about this? That is not all. Factory clearly and explicitly states the relation between dependent component and dependency (service that is created by factory) – it is a temporary relation (as long as it provides ability to create new instances).

Summary:

  • DO make dependencies explicit
  • DO use dependencies to assess and control growth of the dependent component
  • CONSIDER component audience when choosing between dependency injection and service locator
  • CONSIDER using abstract factory pattern and dependency injection to make short lived dependencies (in comparison with dependent component lifetime) explicit

Wednesday, August 5, 2009

Type parameter leveling

At some point developers have to decide whether to use generic class or generic method (or in other words whether to put type parameter on type level or method level).

Type parameter at type level (generic type type parameter) allows to enforce constraints for multiple members that use this type parameter. For example, IEnumerable<T> fixes what kind of enumerators it can produce to IEnumerator<T>.

public interface IEnumerable<T> : IEnumerable
{
    IEnumerator<T> GetEnumerator();
}

Generic type may be used when it:

  • implements generic interface
  • has internal state that depends on type parameter
  • type parameter is implicitly used as an internal state

You can also look at the alternatives from granularity point of view. Type parameter of a generic type has wider influence than type parameter of generic method. If type parameter makes sense only for a single member it doesn’t makes sense to put type parameter at a higher level than member that uses it. Otherwise you will force consumers of members to depend on type parameter and its constrains they may not use.

static class Util<T>
    where T: IComparable<T>
{
    public static T Min(T left, T right)
    {
        return left.CompareTo(right) < 0 ? left : right;
    }
    
    public static void Swap(ref T left, ref T right)
    {
        var tmp = left;
        left = right;
        right = tmp;
    }
}

...

int i1 = 10, i2 = 15;
var min = Util<int>.Min(i1, i2);

object o1 = i1; object o2 = i2;
// Not allowed and results in compilation error
Util<object>.Swap(ref o1, ref o2);

In the case above type parameter introduces constrain which makes sense only for Min method and thus limits capabilities of Swap method (Swap can operate on any argument and Min can only operate on comparable arguments). On the other hand generic type argument must be specified explicitly and it doesn’t use advantages of type inference which simplifies usability of the API.

static class Util
{
    public static T Min<T>(T left, T right)
        where T: IComparable<T>
    {
        return left.CompareTo(right) < 0 ? left : right;
    }

    public static void Swap<T>(ref T left, ref T right)
    {
        var tmp = left;
        left = right;
        right = tmp;
    }
}

...

int i1 = 10, i2 = 15;
var min = Util.Min(i1, i2);

object o1 = i1; object o2 = i2;
// Now it is perfectly legal
Util.Swap(ref o1, ref o2);

Changing type parameter level with respect to members that depend on it within inheritance hierarchy may lead to Design by Contract and Liskov Substitution Principle violation.

I came across an API which do the trick recently. In the abstracted example below ISomething states that its DoSomethingWith method can do something with enumerable of any T.

interface ISomething
{
    void DoSomethingWith<T>(IEnumerable<T> target);
}

But the implementation provides specialization on a fixed type parameter. It changed type parameter level from method to type level – DoSomethingWith method implementation depends on generic type type parameter whereas interface states method’s dependency from method type parameter.

class FixedSomething<U> : ISomething
{
    private Func<U, bool> predicate;
    private IEnumerable<U> slice = Enumerable.Empty<U>();

    public void DoSomethingWith<T>(IEnumerable<T> target)
    {
        if (typeof (U) != typeof (T))
            // Cannot do something on argument of type T. U type is expected.
            throw new ArgumentException();

        slice = target.Cast<U>().Where(predicate).Concat(slice);
    }

    //... other members elided for clarity 
}

It violates ISomething contract as it strengthens input parameter type constrain and introduces new exception that may be thrown. On the other hand it performs runtime type check which violates LSP.

Summary:

  • DO use generic type when it implements generic interface or has internal state that depends on type parameter.
  • CONSIDER type parameter granularity.
  • DO NOT change type parameter level within inheritance hierarchy.

Monday, July 27, 2009

Generic method type parameter as Type parameter

Generics is another mechanism offered by the common language runtime (CLR) and programming languages that provides one more form of code re-use: algorithm re-use.
Jeffrey Richter, CLR via C# 2nd

However algorithm abstraction from data types it operates on is not the only usage scenario. Type parameter of a generic method can be treated as a parameter of Type. Consider the following example

// T IUnityContainer.Resolve<T>()
IService service = unityContainer.Resolve<IService>();

Unity container implementation uses supplied type parameter to resolve an instance of requested type from the container. It is beneficial as it allows us to read code naturally and it avoids unnecessary type casts.

It is quite often used when building fluent interfaces (in particular Method Chaining). Consider autofac container configuration example:

var builder = new ContainerBuilder();
builder.Register<Straight6TwinTurbo>().As<IEngine>().FactoryScoped();

The configuration can be read as a natural language sentence.

However it is only half of the truth. There are several scenarios where this approach doesn’t work so well.

Not all languages support generics, others may have syntax that makes them hard to read. For example the same thing written in VB.NET doesn’t have the same readability as in C#:

Builder.Register(Of Straight6TwinTurbo)().As(Of IEngine)().FactoryScoped();

On the other hand it is beneficial when type parameter is known at compile time and it may complicate more dynamic scenarios when it is deferred until runtime.

For example, Unity container has an Extension called StaticFactoryExtension. The purpose of this extension is to add the ability to register types within the container while deferring the instantiation of the type to a factory method. The extension is configured through IStaticFactoryConfiguration:

public interface IStaticFactoryConfiguration : IUnityContainerExtensionConfigurator
{
    IStaticFactoryConfiguration RegisterFactory<TTypeToBuild>(FactoryDelegate factoryMethod);
    IStaticFactoryConfiguration RegisterFactory<TTypeToBuild>(string name, FactoryDelegate factoryMethod);
}

It is used like this:

container.AddNewExtension<StaticFactoryExtension>();
IStaticFactoryConfiguration config = container.Configure<IStaticFactoryConfiguration>();
config.RegisterFactory<IProductService>(ProductServiceFactory.Create);

Note however that the factory registration API includes only generic methods. It makes harder to implement scenarios were calling code wants to enumerate types at runtime and register particular factory for them.

In order to solve this problem the API should provide non generic version in addition to generic one. For example, Unity container provides both (generic and non-generic) versions for Resolve method:

public interface IUnityContainer
{
    T Resolve<T>();
    object Resolve(Type t);

    //... other members elided for clarity
}

Summary:

  • CONSIDER audience of your API
  • CONSIDER providing non generic version of your API taking into account your audience and usage scenarios

Thursday, July 16, 2009

Scenario in terms of abstraction levels

Framework Design Guidelines promotes several principles but one of them is worth special attention:

Frameworks must be designed starting from a set of usage scenarios and code samples implementing these scenarios.

It allows framework designers to see their API from consumer’s perspective and the value the sight produces should not be underestimated. The framework designers are experts in subject area and thus they are cursed by this knowledge. That is why it is highly important to express mainline usage scenarios in terms that consumers are familiar with or can get easily to know (of course advanced scenarios may require much deeper knowledge in the subject area).

Unfortunately designers quite often rely on standard design methodologies (including object-oriented design methodologies) that are oriented more on maintainability than on usability of the API.

Recently I came across an API (or basically an approach promoted over the application) that is responsible for data persistence. Basically in common scenarios data persistence is used in both directions (read/write). From consumer’s perspective it looks like a single scenario. Suggested approach assumes that data is loaded through loaders and data changes are propagated back to data source using Unit of Work pattern:

// Represents some company information
internal class Company
{
    public int Id { get; set; }
    public int TrustLevel { get; set; }
}

// Loaders are responsible for data reading.
internal interface ICompanyLoader
{
    // Loads limitted set of data about companies that has highest 
    // profits.
    IEnumerable<Company> LoadTopPerformingCompanies(int count);
}

// Here goes Unit of Work pattern.
internal interface IUnitOfWork
{
    // Methods that are responsible for state changes tracking and writing out changes 
    // coordination.

    void RegisterNew(object obj);
    void RegisterDirty(object obj);
    void RegisterClean(object obj);
    void RegisterDeleted(object obj);
    void Commit();
    void Rollback();
}

Used like this:

// Loading data which is expressed in terms of business specific
var companies = companyLoader.LoadTopPerformingCompanies(10);

// Updating data which is expressed in terms of a selected 
// persistence layer implementation
foreach (var company in companies)
{
    company.TrustLevel++;
    unitOfWork.RegisterDirty(company);
}
unitOfWork.Commit();

So where is the problem? It seems working…

As mentioned from consumer’s perspective this is a single scenario. However the API expresses single scenario in terms of different abstraction levels:

  • Data loading is expressed in business specific terms (load top performing companies – companies that has profits for the last year more than the others)
  • Data updating is expressed in terms of state management (register object as dirty - updated)

This is the catch. In order for consumer to implement the scenario one needs to switch between abstractions which dramatically reduces productivity and on the other hand readability of the code.

In order to avoid mentioned problems a single level of abstraction must be used:

// It may be Table Data Gateway pattern 
interface ICompanyGateway
{
    // Loads limitted set of data about companies that has highest 
    // profits.
    IEnumerable<Company> LoadTopPerformingCompanies(int count);

    // Updates companies trust
    void IncreaseCompaniesTrust(IEnumerable<Company> companies);
}

...

// Loading and updating data which is expressed in terms of business specific
var companies = companyGateway.LoadTopPerformingCompanies(10);
companyGateway.IncreaseCompaniesTrust(companies);

Summary:

  • DO NOT mix different abstraction levels within single scenario
  • DO provide expected by the consumer terms required to implement the scenario

Friday, June 19, 2009

Commonality that is worth abstractions

Unfortunately desire to achieve reusability not always leads to expected results. It can bring you development effort savings through reusing existent components or it can lead to negative consequences (such as proliferation of unnecessary classes, decreased discoverability and readability).

Reusability is often based on abstractions. Abstraction captures some commonality between its concrete implementations. But abstractions being very powerful are dangerous as well.

Consider situation where a piece of work is generalized and represented through Command pattern. Basically task (commonality) resulted in command (abstraction).

interface ICommand
{
  // Performs command's task
  void Do(object parameter);
}

Is this commonality worth the abstraction? It depends on the rationale behind.

It may result from desire to factor out functionality that belongs to a particular category (for example, business rules) and through implementing common interface outline membership in a category (and of course it may be expected that through abstraction they will be reusable in some other scenarios). However with this rationale at hand it is most likely that code that creates command also executes it (because it is the only scenario for now – more other scenarios are coming :-)).

// Holds arguments for SomeCommand
class Arguments
{
  public string Arg1 { get; set; }
  public int Arg2 { get; set; }
}

class SomeCommand : ICommand
{
  public void Do(object parameter)
  {
      var args = (Arguments) parameter;
      // Do something with arguments
  }
}
var command = new SomeCommand();
var args = new Arguments {Arg1 = "value", Arg2 = 10};
command.Do(args);

Unfortunately this rationale doesn’t justify introduction of command abstraction. It forces calling code to consume general purpose opaque contract despite the fact that calling code knows exactly what must be done. It decreases readability in comparison with direct call to a method with comprehensive name and may result in proliferation of unnecessary classes which are basically arguments containers. Thus the abstraction introduced drawbacks with no benefits.

It is as important to resist premature abstractions as the abstraction itself. Every abstraction even not used once is not free - it has maintenance costs attached since abstractions are considered to be a contract between the author and consumers that must be fulfiled.

Anytime you introduce a new abstraction a concrete implementation of the abstraction and consuming API must be provided as well. It is hard to see whether abstraction designed correctly unless there is a use case implemented that uses the abstraction.

Consider when calling code cannot execute commands itself but rather commands must executed in order of appearance (through a queue).

// Executes commands in order
interface CommandQueue
{
  // Enqueues command with parameter that is known
  // at the time of enqueue
  void Enqueue(ICommand command, object parameter);

  // Enqueues command with function that will provide
  // parameter on demand
  void Enqueue(ICommand command, Func<object> parameter);
}

In this case abstraction introduction is justified. The command queue conforms to Open/Closed Principle - it is easily extensible with new types of commands because it relies on Command abstraction.

If classes share only a common interface (not behavior), then they should inherit from a common base class or interface only if they will be used polymorphically.

Summary:

  • AVOID premature abstractions
  • DO carefully justify introduction of new abstractions
  • DO provide concrete implementation and consuming API for every abstraction introduced

Thursday, June 11, 2009

Events and Callbacks vs. Explicit Calls

From API design perspective interaction between components can be expressed in request/reply (explicit calls) or in event driven manner (events and callbacks). Consider BackgroundWorker usage example:

// BackgroundWorker executes an operation on a separate thread.
// The operation is basically an event handler of DoWork event
var worker = new BackgroundWorker();
// Once worker will be run this event will be raised in separate thread
worker.DoWork += (sender, e) =>
                {
                   // Event handler does the actual work
                   // The event is raised in a separate thread
                };
worker.RunWorkerCompleted += (sender, e) =>
                            {
                               // Performs post processing step
                               // The event is raised through sycnhronization
                               // context
                            };
// Runs worker
worker.RunWorkerAsync();

Why .NET Framework designers selected event based interaction model instead of request/reply (see hypothetical example below)?

// Represents unit of background work
internal interface IBackgroundWork
{
    // Performs actual work and will be called
    // in a separate thread
    object Do();
    // Performs post processing step and will be
    // called through sycnhronization context
    void Complete(object result);
}

internal class SomeBackgroundWork : IBackgroundWork
{
    public object Do()
    {
        // Do the actual work and return result
        return ...;
    }

    public void Complete(object result)
    {
        // Do post processing
    }
}

internal class BackgroundWorker
{
    // Runs work unit asynchronously
    public void RunWorkerAsync(IBackgroundWork work)
    {
        // elided
    }
}
var worker = new BackgroundWorker();
worker.RunWorkerAsync(new SomeBackgroundWork());

The short answer is that it is about the relationship between participants (worker and provider) and in particular the direction of the dependency. Dependency direction is identified by the domain that holds participants.

Consider IComparable<T> interface. This interface is implemented by types whose instances can be ordered. Implementing this interface on a type states that it supports comparison. On the other hand GenericComparer<T> uses IComparable<T> to compare instances.

[Serializable]
internal class GenericComparer<T> : Comparer<T>
    where T : IComparable<T>
{
    public override int Compare(T x, T y)
    {
        if (x != null)
        {
            if (y != null)
            {
                return x.CompareTo(y);
            }
            return 1;
        }
        if (y != null)
        {
            return -1;
        }
        return 0;
    }

    // othe members elided
}

Explicit calls clearly express the relationship and the consequences of the action but it limits extensibility to compile time and puts more work on API consumers because it requires them to create new types. In the "explicit" background worker example consumer will need to create new type for its unit of work. And it is highly likely that it won't be used in other places.

Consider ThreadPool class. It provides ability to execute arbitrary code on a separate thread. ThreadPool is not interested to know its consumers but it is the consumers who depend on ThreadPool to perform a task. It uses callbacks mechanism to execute arbitrary code within thread pool.

ThreadPool.QueueUserWorkItem(state =>
                            {
                             // Does actual work
                            });

Events and callbacks do not create explicit dependency and thus allows for runtime customization. Events has great tool support. It puts less work on consumers because it doesn’t require to create new types. But these benefits come at a price. It is hard to understand what happens just from looking at the code - in order to understand it all subscriptions to events must be seen. The event triggering may result in an events cascade where handling code raises next event thus creating a chain of events. From performance perspective events and callbacks are more heavyweight than explicit calls.

Summary:

  • CONSIDER using explicit calls to express the relationship (consuming several events or callbacks to perform single task may be a sign that there is role to discover)
  • CONSIDER using explicit calls if there is a potential to reuse called code in other use cases
  • CONSIDER using explicit calls to increase testability
  • AVOID expressing complex interaction using events or callbacks which will be really hard to understand
  • CONSIDER using events and callbacks to allow runtime customization and avoid undesired dependency

Friday, June 5, 2009

When member overload backfires

I do believe that quality of software design isn’t based solely on appropriate high level decisions. It is detailed quality decisions made on a smaller scale that make it possible.

The API influences consuming code. Making it obvious and simple helps to avoid creating hard to read and write error-prone code.

Recently I caught myslef writing the API presented below (I fixed it, honestly :-)):

// Represents basic document template - named placeholders can be mapped to data and
// resulting document is generated
public class DocumentTemplate
{
       // Maps named placeholder to data item
       public void Map<T>(string placeholderName, T data)
       {
              // Placeholder will be replaced with textual representation of the data
       }

       // Maps named placeholder to collection of data items - this is a repeatable placeholder
       public void Map<T>(string placeholderName, IEnumerable<T> data)
       {
              // Placeholder is treated as a repeatable piece
              // Data collection will be enumerated and for each item placeholder body will
              // duplicated and replaced with textual representation of the item
       }

       // ... Other non-relevant members are omitted
}

Consider code snippet below – it uses type inference. Can you tell which overload will be called?

TemplateMapping templateMapping = ...
var list = new List<int> {1, 2};
templateMapping.Map("v1", list); // #1
templateMapping.Map("v2", list.AsEnumerable()); // #2

It does require you to know overload resolution rules to figure out which overload will be called. In #1 case Map<List<int>>(…) will be called and Map<int>(…) in #2.

But worse it can lead to hidden bugs. Suppose we have a method like this

static IEnumerable<int> GetCollection()
{
       return new List<int> { 1, 2 };
}

And it is used with buggy-prone templating API

TemplateMapping templateMapping = ...
templateMapping.Map("v", GetCollection());

In this case Map<int>(…) is called. But suppose that the author of the GetCollection() method decided to change return type to IList<int> - other overload will be called!

It should be clear from looking at the calling code which overloaded member will be called.

In order to solve this you can for example specify manually which overload must be used

templateMapping.Map<int>("v", GetCollection());

But wait. This API has a more fundamental problem. Overloaded members are meant to be semantically equivalent. But in this particular case mapping semantic changes based on parameters types! Latter example shows case where your application may behave differently.

This problem can be solved through giving each semantically different member unique descriptive name. For example,

// Maps named placeholder to data item
public void MapSingle<T>(string placeholderName, T data)

// Maps named placeholder to collection of data items - this is a repeatable placeholder
public void MapEnumerable<T>(string placeholderName, IEnumerable<T> data)

This will also allow to avoid overload resolution problems mentioned above.

Summary:

  • AVOID making overloads that are semantically different
  • DO NOT require from developers deep knowledge of overload resolution rules in order to use your API