One argument in favour of boilerplate code is that if you change it in one place, it only affects one flow of the code. This has to be balanced against the fact that more-often-than-not, you actually want a change to affect every piece of code that uses it. But I have seen rare examples that support the argument.
Let's say you have a piece of code that says
public ForTheBar(Foo foo)
{
Bar bar = foo.bar();
return bar.BeFooed();
}
This is used in about 2 places in your code.
One day someone comes along and says "ok, in this path only, we want you to Grommit the bar before Fooing it."
And you think "well this is simple."
public ForTheBar(Foo foo, bool shouldIGrommit)
{
Bar bar = foo.bar();
if (shouldIGrommit)
{
bar.BeGrommitted();
}
return bar.BeFooed();
}
Then your user adds some new functionality and you reckon it fits in well with FooTheBar. And you dutifully ask them if you should Grommit that bar before you Foo it and they say "no, not this time".
So you just call the method above.
But then your user says "ok, wait, in the third case, we want you to Doodle the Bar before calling BeFooed."
No problem, you think, I can do that.
public ForTheBar(Foo foo, bool shouldIGrommit, bool shouldIDoodle)
{
Bar bar = foo.bar();
if (shouldIGrommit)
{
bar.BeGrommitted();
}
if (shouldIDoodle)
{
bar.BeDoodled();
}
return bar.BeFooed();
}
Suddenly your code is becoming less boilerplate. Perhaps you should have accepted the repeated two lines of code. By now you would have three pieces of code, each 2-3 lines long and not looking very repeated any more.
All this said, I would counter that with "this is not a common case, and when it happens, you can refactor."
Another argument I have recently heard is that boilerplate code can sometimes help you navigate code. The example we were discussing was where we'd removed tons of boilerplate mapping code and replaced it with AutoMapper. Now, it was argued, because everything is convention-based, you cannot say "Where is this property set," to the IDE and expect it to know.
I've seen people argue similar things about IoC containers.
Not to say that I agree with them, but it's a fair argument nonetheless.