Second-class citizens above the law

It’s not too late. Almost eleven months have passed since I favored a Long Parameter List over perceived Feature Envy on the grounds of coupling with the order of the parameters:

Long Parameter Lists are also about coupling, to a lesser extent. You are coupled to the order of the parameters.

I even said in that post that Feature Envy isn’t a clear-cut issue:

Let me share something about Tell Don’t Ask that I’ve realized: the most harmful instances that require attention are those where you get some values out of an object, do something with them, and set some others back into the object.

Yes. And then I defended the readability of a Long Parameter List:

exporter.add(name, id, salary, boss, dept, title);

also sounds like “Dear Exporter, add name, id, salary, boss, dept, and title to yourself.”

But that is assuming you studiously declare all local variables first, assign to them, and then use them. Often, we aren’t that disciplined, and even more often, arguments in the list share the same type, so it is even more likely that you can confuse the order of such parameters, even if you declare the locals first.

void foo(String a, int f, double c, String e, String d, int b)

// caller

String a = ...;
int f = ...;
double c = ...;
String e = ...;
String d = ...;
int b = ...;

foo(a, f, c, e, d, b);

So what is so bad about this instead?

void foo(ParameterObject parameter)

// caller

ParameterObject parameter = new ParameterObject();



I don’t think anything is wrong with it, and I am emboldened by Uncle Bob‘s approval in Clean Code:

Reducing the number of arguments by creating objects out of them may seem like cheating but it’s not.

I am advocating Parameter Objects now, but not for the readability. I think the readability is pretty much equivalent. Setting into a Parameter Object is just as much typing as declaring all local variables and using them in a Long Parameter List.

I think the true advantage is freeing yourself from the order of the parameters, and this is especially beneficial when you have parameters of the same type. Even with local variables, you run the risk of transposing those arguments of the same type.

OK, OK, you can still confuse them with a parameter object. In the example, nothing stops you from setting the “A” String into the “E” string, but now the error is a lot easier to pick out. No longer is there a need to count parameters in the method to find out what’s getting set where.

And what about Feature Envy here? Feature Envy? We’re talking about a data structure. It’s a second-class citizen and the laws of objects don’t apply. It is a second-class citizen who is above the law.

Announcer: You’re reading the EIP web-ring.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: