mardi 23 juillet 2019

Is it best practice to use the "Move Accumulation To Collecting Parameter" pattern?

I'm currently reading Joshua Kerivsky's 'Refactoring To Patterns'. One of the refactoring techniques referred to in the book is based upon Kent Beck's 'Collecting Parameter' pattern.

This pattern is described as something you may need when you are needing to accumulate data, (such as with Java's StringBuffer) compared to lots of calls and returns. In the book, he describes a Collecting Parameter as an object that you pass to methods in order to collect info from those methods.

I cannot find many examples of people talking about this online and it seems to be rarely discussed. I'm wondering how/when is best practice to use a pattern like this, as I don't know if it's the 'done thing' these days. I haven't seen it used in production and I generally don't know if it's an 'old world' programming technique.

I see that we are mutating the object every time we want to accumulate data which, if you look at functional programming principles is generally bad practice. But is it okay in an OOP sense?

Simple example of usage (Java):

FROM:

result += "<" + tagName + " " + attributes + ">";

TO:

private void writeOpenTagTo(StringBuffer result) {
    result.append("<");
    result.append(name);
    result.append(" ");
    result.append(attributes.toString());
    result.append(">");
}

Aucun commentaire:

Enregistrer un commentaire