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:
public class OrderProcessor { public void Process(object[] args) { Order order = (Order) args[0]; Customer customer = (Customer) args[1]; // etc. } }
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:
public class OrderProcessor { public void Process(Order order, Customer customer) { // etc. } }
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:
public class OrderProcessor { private readonly ISessionContext _context; public OrderProcessor() : this(ObjectFactory.GetInstance<ISessionContext>()) { } public OrderProcessor(ISessionContext context) { _context = context; } }
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:
public interface ICommand { void Execute(object[] args); } public class TransferCommand : ICommand { public void Execute(object[] args) { Account source = (Account) args[0]; Account dest = (Account) args[1]; int amount = (int) args[2]; source.Balance -= amount; dest.Balance += amount; } }
The ICommand interface needs to be flexible in the arguments it can pass to specific implementations. We can use generics instead of blob parameters to accomplish the same effect:
public interface ICommand<T> { void Execute(T args); } public struct Transaction { public Account Source; public Account Destination; public int Amount; public Transaction(Account source, Account destination, int amount) { Source = source; Destination = destination; Amount = amount; } } public class TransferCommand : ICommand<Transaction> { public void Execute(Transaction args) { Account source = args.Source; Account dest = args.Destination; int amount = args.Amount; source.Balance -= amount; dest.Balance += amount; } }
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.