Intention-concealing interfaces: blob parameters

When someone is using your code, you want your code to be as explicit and easy to understand as possible. This is achieved through Intention-Revealing Interfaces. Evans describes the problems of opaque and misleading interfaces in Domain-Driven Design:

If a developer must consider the implementation of a component in order to use it, the value of encapsulation is lost. If someone other than the original developer must infer the purpose of an object or operation based on its implementation, that new developer may infer a purpose that the operation or class fulfills only by chance. If that was not the intent, the code may work for the moment, but the conceptual basis of the design will have been corrupted, and the two developers will be working at cross-purposes.

His recommendation is to create Intention-Revealing Interfaces:

Name classes and operations to describe their effect and purpose, without reference to the means by which they do what they promise. This relieves the client developer of the need to understand the internals. The name should conform to the Ubiquitous Language so that the team members can quickly infer their meaning. Write a test for a behavior before creating it, to force your thinking into client developer mode.

Several times now I’ve run into blob parameters (I’m sure there are other names for them). Blob parameters are amorphous, mystery parameters used in constructors and methods, usually of type ArrayList, HashTable, object[], or even params object[]. Here’s an example:

The intentions behind this code are good, but the results can lead to some frustrating results. Furthermore, this pattern leads to “Intention-Concealing Interfaces“, which is style completely opposite from the Intention-Revealing style Eric proposes. Clients need to know the intimate details of the internal implementation of the component in order to use the blob parameters.

Wrong kind of extensibility

Blob parameters lead to the worst kind of coupling, beyond just “one concrete class using another concrete class”. When using blob parameters, the client gets coupled to the internal implementation of unwinding the blob. Additionally, new client code must have intimate knowledge of the details, as the correct order of the parameters is not exposed by the blob method’s interface.

When a developer needs to call the “Process” method above, they are forced to look at the internal implementation. Hopefully they have access to the code, but otherwise they’ll need to pop open Reflector to determine the correct parameters to pass in. Instead of learning if the parameters are correct at compile-time, they have to wait until run-time.

Alternatives

Several alternatives exist before resorting to blob parameters:

Explicit parameters

Creation method

Factory patterns

IoC containers

Generics

Each of these alternatives can be used separately or combined together to achieve both Intention-Revealing Interfaces and simple code.

Explicit Parameters

Explicit parameters just means that members should ask explicitly what they need to operate through their signature. The Process method would be changed to:

The Process method takes exactly what components it needs to perform whatever operations it does, and clients of the OrderProcessor can deduce this simply through the method signature.

If this signature needs to change due to future requirements, you have a few choices to deal with this change:

Overload the method to preserve the existing signature

Just break the client code

People always assume it’s a big deal to break client code, but in many cases, it’s not. Unless you’re shipping public API libraries as part of your product, backwards compatibility is something that can be dealt with through Continuous Integration.

Creation Method/Factory Patterns/IoC Containers

When blob parameters start to invade constructors, it’s a smell that you need some encapsulation around object creation. Instead of dealing with changing requirements through blob parameters, encapsulate object creation:

Originally, the ISessionContext was a hodgepodge of different object that I needed to pass in to the OrderProcessor class. Since these dependencies became more complex, I encapsulated them into a parameter object, and introduced a factory method (the “ObjectFactory” class) to encapsulate creation. Client code no longer needs to pass in a group of complex objects to create the OrderProcessor.

Generics

Sometimes blob parameters surface because a family of objects needs to be created or used, but all have different needs. For example, the Command pattern that requires data to operate might look like this before generics:

Each ICommand implementation can describe its needs through the generic interface, negating the need for blob parameters.

No blobs

I’ve seen a lot of misleading, opaque code, but blob parameters take the prize for “Intention-Concealing Interfaces“. Instead of components being extensible, they’re inflexible, brittle, and incomprehensible. Before going down the path of creating brittle interfaces, exhaust all alternatives before doing so. Every blob parameter I’ve created I rolled back later as it quickly becomes difficult to deal with.

About Jimmy Bogard

I'm a technical architect with Headspring in Austin, TX. I focus on DDD, distributed systems, and any other acronym-centric design/architecture/methodology. I created AutoMapper and am a co-author of the ASP.NET MVC in Action books.

At the risk of exposing my own lack of coding ability (and it’s a big lack let me tell you), here is what I do:

I create a uber-class called Checkout that encapsulates all of the other objects that will need to be referenced when a ProcessOrder method is called.

By creating such an uber-class, I get type safety and let’s me do something like a UnitOfWork without having to worry about having a UnitOfWork class.

If that makes sense.

jdn

dave falkner

The Principle of Most Surprise.

Ads

About Me

I'm the chief architect at Headspring in Austin, TX. I focus on DDD, distributed systems, and any other acronym-centric design/architecture/methodology. I created AutoMapper and am a co-author of the ASP.NET MVC in Action books.