All programming languages I’m familiar with allow the programmer to pass a large number of parameters to a function call. While this makes it easy for the initial developer, it can become cumbersome for the maintenance developer. Just look at the following code:


var calculator = new Calculator(

IEnumerable<DateTime> existingValues,
DateTime today,
bool withTime,
bool withPremiumLicense );

A better way to construct the object is

var builder = new Calculator.Builder();
var calculator = builder
                   .WithExistingValues( values )
                   .WithToday( DateTime.Today)
                   .WithUseTime( false )
                   .WithPremiumLicense( true )
                   .Build();

The advantages:
– the code is more readable
– allows TDD
– it is easier to change the parameters and establish defaults.

For example:

var builder = new Calculator.Builder();
var calculator = builder
                    .WithDefaults()
                    .Build();

The conclusion is the next huge step for C# will be to empower developers
to use fluent API-s and immutable objects, just as in functional programming.
The Calculator object is actually similar to a C++ functoid.

Advertisements