{"id":543,"date":"2003-12-04T17:37:45","date_gmt":"2003-12-04T21:37:45","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=543"},"modified":"2003-12-04T17:37:45","modified_gmt":"2003-12-04T21:37:45","slug":"optimal-number-of-arguments-to-a-method-or-function","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2003\/12\/04\/optimal-number-of-arguments-to-a-method-or-function\/","title":{"rendered":"Optimal number of arguments to a method or function"},"content":{"rendered":"<p><a href=\"http:\/\/www.cfm-applications.com\/blogman\/\">Bryan Hogan<\/a> <a href=\"http:\/\/www.cfm-applications.com\/blogman\/archives\/entries\/EA4EC885-3048-4181-CDD0DC135161CAE6.shtml\">posted<\/a> 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: &#8220;<i>If you have more than say two arguments that are needed to be passed into a function, use argumentCollection!<\/i>&#8221;  In his opinion this is better:<br \/>\n<code><br \/>\nemail = structnew();<br \/>\nemail[\"subject\"] = \"test subject\";<br \/>\nemail[\"to\"] = \"to@test.com\";<br \/>\nemail[\"from\"] = \"from@test.com\";<br \/>\nemail[\"content\"] = \"check this out!\";<br \/>\nemail[\"type\"] = \"text\";<br \/>\n\/\/ send the email using the SendEmail() function<br \/>\nSendEmail(email);<br \/>\n<\/code><br \/>\nthan writing this:<br \/>\n<code><br \/>\nSendEmail(\"test subject\", \"to@test.com\", \"from@test.com\", \"check this out\", \"text\");<br \/>\n<\/code><br \/>\nHis hypothesis didn&#8217;t sit right with me so I picked up my <a href=\"http:\/\/www.amazon.com\/exec\/obidos\/ASIN\/1556154844\/cephasnet-20\/\">Code Complete<\/a> book and went to see what Steve had to say about the subject.  Chapter 5, &#8220;Characteristics of High-Quality Routines&#8221;, 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&#8217;s code sample seemed to me to be an example of data-structure coupling, which is defined as &#8220;<i>.. two routines are data-structure-coupled if the data passed between them is structured and is passed through a parameter list.<\/i>&#8221; (it&#8217;s also called stamp coupling for anyone who cares).  The example that uses multiple parameters is an example of simple-data coupling: &#8220;<i>&#8230; two routines are simple-data-coupled if all the data passed between them is nonstructured and it&#8217;s all passed through a parameter list.  This is often called &#8216;normal coupling&#8217; and is the best kind.&#8217;<\/i>&#8221;  <\/p>\n<p>These definitions certainly don&#8217;t prove Bryan&#8217;s hypothesis, but they don&#8217;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 &#8220;How to Use Routine Parameters&#8221; has this to say: &#8220;<i>Limit the number of a routine&#8217;s parameters to about seven&#8230; 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.<\/i>&#8221; <\/p>\n<p>Stepping outside of academia for a second, I&#8217;d personally much rather use a method where either:<\/p>\n<p>a) I can read the parameter list and know exactly what is required. For example:<br \/>\n<code><br \/>\nsendEmail(string to, string from, string subject, string body, string type)<br \/>\n<\/code><br \/>\nis concise and explicit.  It leaves nothing for me to mess up.<\/p>\n<p>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:<br \/>\n<code><br \/>\nEmailPackage email = new EmailPackage(to, from, subject, body, type);<br \/>\n<\/code><br \/>\nand then send that package to the method:<br \/>\n<code><br \/>\nEmailDeliverer.send(email);<br \/>\n<\/code><br \/>\nConclusion? 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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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: &#8220;If you have more than say two arguments that are needed to be passed into a function, use argumentCollection!&#8221; In his &hellip; <a href=\"https:\/\/cephas.net\/blog\/2003\/12\/04\/optimal-number-of-arguments-to-a-method-or-function\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Optimal number of arguments to a method or function<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/543"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=543"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/543\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}