Bryan Hogan posted a thought provoking blurb a couple weeks ago where he postulated that passing a data structure as the parameter to a method is better than passing numerous parameters. Specifically, he said: “If you have more than say two arguments that are needed to be passed into a function, use argumentCollection!” In his opinion this is better:
email = structnew();
email["subject"] = "test subject";
email["to"] = "to@test.com";
email["from"] = "from@test.com";
email["content"] = "check this out!";
email["type"] = "text";
// send the email using the SendEmail() function
SendEmail(email);
than writing this:
SendEmail("test subject", "to@test.com", "from@test.com", "check this out", "text");
His hypothesis didn’t sit right with me so I picked up my Code Complete book and went to see what Steve had to say about the subject. Chapter 5, “Characteristics of High-Quality Routines”, mentions that there are 5 levels of coupling between routines: simple-data coupling, data-structure coupling, control coupling, global-data coupling, and pathological coupling. Bryan’s code sample seemed to me to be an example of data-structure coupling, which is defined as “.. two routines are data-structure-coupled if the data passed between them is structured and is passed through a parameter list.” (it’s also called stamp coupling for anyone who cares). The example that uses multiple parameters is an example of simple-data coupling: “… two routines are simple-data-coupled if all the data passed between them is nonstructured and it’s all passed through a parameter list. This is often called ‘normal coupling’ and is the best kind.’”
These definitions certainly don’t prove Bryan’s hypothesis, but they don’t disprove him either. It seems then that the real juice of his argument is that one or two parameters is better than four or five. Chapter 5, section 7 of Code Complete, titled “How to Use Routine Parameters” has this to say: “Limit the number of a routine’s parameters to about seven… If you find yourself passing more than a few arguments, the coupling amoung your routines is too tight. Design the routine or group of routines to reduce the coupling.”
Stepping outside of academia for a second, I’d personally much rather use a method where either:
a) I can read the parameter list and know exactly what is required. For example:
sendEmail(string to, string from, string subject, string body, string type)
is concise and explicit. It leaves nothing for me to mess up.
or b) I can pass an object (which presumably has either defined getters or publicly accessible instance variables). For example, I might define an email package:
EmailPackage email = new EmailPackage(to, from, subject, body, type);
and then send that package to the method:
EmailDeliverer.send(email);
Conclusion? Using more than 2 parameters is definitely not a bad practice, using a data structure instead of a number of parameters is sometimes a good practice, but not the best. Thinking about you should best structure your code? Priceless. Keep up the great thoughts Bryan.