<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:clearspace="http://www.jivesoftware.com/xmlns/jive/rss" version="2.0">
  <channel>
    <title>Adobe Community: Message List - Disable cffunction accepting undeclared arguments?</title>
    <link>https://forums.adobe.com/community/coldfusion/coldfusion_advanced?view=discussions</link>
    <description>Most recent forum messages</description>
    <language>en</language>
    <pubDate>Sat, 26 Jul 2014 15:23:03 GMT</pubDate>
    <generator>Jive Engage 7.0.0.1  (http://jivesoftware.com/products/)</generator>
    <dc:date>2014-07-26T15:23:03Z</dc:date>
    <dc:language>en</dc:language>
    <item>
      <title>Re: Disable cffunction accepting undeclared arguments?</title>
      <link>https://forums.adobe.com/message/6585991?tstart=0#6585991</link>
      <description>&lt;!-- [DocumentBodyStart:739b1fa6-5ef9-45b8-b430-ea9b38cb2ede] --&gt;&lt;div class="jive-rendered-content"&gt;&lt;p&gt;The issue arises because Coldfusion is weakly typed. As such, it cannot support method overloading. So I understand what you are aiming at. You could nevertheless improve the design.&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;At the moment, you give responsibility for checking the arguments to the function itself. That design reduces cohesion. Ideally functions should be defined such as to perform one service. Making them to validate arguments, on top of the task they each have to perform, introduces unnecessary complexity, hence reduced cohesion.&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;The 'Design By Contract' (DbC) paradigm suggests a solution. The validation of arguments - as you have done - is an example of a &lt;em&gt;precondition&lt;/em&gt; in DbC. A precondition is carried out as follows:&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;1)&amp;nbsp; by the caller of a function;&lt;/p&gt;&lt;p&gt;2)&amp;nbsp; before the function runs.&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Delegating the task to the caller is inevitable. In software design, you delegate tasks and responsibilities ideally to the party that has the most information necessary to perform them. (This is sometimes called the G.R.A.S.P. pattern). In this case, that party is the caller of each function. For the caller is the specialist who, better than anyone else, is aware of the parameters that are used to call a function.&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;This improved design leaves your above validation code basically unchanged. Above all, it has the tremendous advantage of leaving your functions intact. Imagine how much refactoring, debugging and hair-tearing this would save you if you had, say, 2000 functions.&lt;/p&gt;&lt;/div&gt;&lt;!-- [DocumentBodyEnd:739b1fa6-5ef9-45b8-b430-ea9b38cb2ede] --&gt;&lt;img src='/beacon?t=1415919118850' /&gt;</description>
      <pubDate>Sat, 26 Jul 2014 15:10:41 GMT</pubDate>
      <author>forums_noreply@adobe.com</author>
      <guid>https://forums.adobe.com/message/6585991?tstart=0#6585991</guid>
      <dc:date>2014-07-26T15:10:41Z</dc:date>
      <clearspace:dateToText>3 months 3 weeks ago</clearspace:dateToText>
      <clearspace:objectType>0</clearspace:objectType>
    </item>
    <item>
      <title>Re: Disable cffunction accepting undeclared arguments?</title>
      <link>https://forums.adobe.com/message/6570284?tstart=0#6570284</link>
      <description>&lt;!-- [DocumentBodyStart:2a33a2f3-d902-434f-9979-9fa6eeb69be1] --&gt;&lt;div class="jive-rendered-content"&gt;&lt;p&gt;Your understanding was correct. I was hoping to find a coldfusion setting to enable some sort of "strict mode" on function calls, but using this code did the job and only required a paste into each function. Note: works only on CF 9+&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=";"&gt;&amp;lt;!--- Include this invoke in any function to check the arguments ---&amp;gt;&lt;/td&gt;&lt;td style=";"&gt;&lt;/td&gt;&lt;td style=";"&gt;&lt;/td&gt;&lt;td style=";"&gt;&lt;/td&gt;&lt;td style=";"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;&amp;lt;cfinvoke component="validation" method="verify_arguments"&amp;gt;&lt;/p&gt;&lt;p&gt;&amp;lt;cfinvokeargument name="accepted_arguments" value="#ARGUMENTS#"&amp;gt;&lt;/p&gt;&lt;p&gt;&amp;lt;cfinvokeargument name="declared_arguments" value="#GetMetaData(Evaluate(GetFunctionCalledName())).parameters#"&amp;gt;&lt;/p&gt;&lt;p&gt;&amp;lt;/cfinvoke&amp;gt;&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;lt;!--- Function it calls for reference ---&amp;gt;&lt;/p&gt;&lt;p&gt;&amp;lt;cffunction name="verify_arguments" access="public" output="yes"&amp;gt;&lt;/p&gt;&lt;p&gt;&amp;lt;cfargument name="accepted_arguments" required="yes"&amp;gt;&lt;/p&gt;&lt;p&gt;&amp;lt;cfargument name="declared_arguments" required="yes"&amp;gt;&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;lt;cfif ArrayLen(declared_arguments) IS NOT ArrayLen(accepted_arguments)&amp;gt;&lt;/p&gt;&lt;p&gt;&amp;lt;h2 class="warning"&amp;gt;Function has accepted more arguments than were declared.&amp;lt;/h2&amp;gt;&lt;/p&gt;&lt;p&gt;&amp;lt;h3&amp;gt;Declared Arguments&amp;lt;/h3&amp;gt;&lt;/p&gt;&lt;p&gt;&amp;lt;cfdump var="#declared_arguments#"&amp;gt;&lt;/p&gt;&lt;p&gt;&amp;lt;h3&amp;gt;Accepted Arguments&amp;lt;/h3&amp;gt;&lt;/p&gt;&lt;p&gt;&amp;lt;cfdump var="#accepted_arguments#"&amp;gt;&lt;/p&gt;&lt;p&gt;&amp;lt;/cfif&amp;gt;&lt;/p&gt;&lt;p&gt;&amp;lt;/cffunction&amp;gt;&lt;/p&gt;&lt;/div&gt;&lt;!-- [DocumentBodyEnd:2a33a2f3-d902-434f-9979-9fa6eeb69be1] --&gt;</description>
      <pubDate>Mon, 21 Jul 2014 20:57:13 GMT</pubDate>
      <author>forums_noreply@adobe.com</author>
      <guid>https://forums.adobe.com/message/6570284?tstart=0#6570284</guid>
      <dc:date>2014-07-21T20:57:13Z</dc:date>
      <clearspace:dateToText>3 months 3 weeks ago</clearspace:dateToText>
      <clearspace:replyCount>1</clearspace:replyCount>
      <clearspace:objectType>0</clearspace:objectType>
    </item>
    <item>
      <title>Re: Disable cffunction accepting undeclared arguments?</title>
      <link>https://forums.adobe.com/message/6568844?tstart=0#6568844</link>
      <description>&lt;!-- [DocumentBodyStart:0bc958f0-a095-4394-9795-f7d304cecf07] --&gt;&lt;div class="jive-rendered-content"&gt;&lt;p&gt;I think I get it now. The argument name is status_code, but what gets into the function is &lt;em&gt;arguments.status&lt;/em&gt; instead of &lt;em&gt;arguments.status_code&lt;/em&gt;. Right?&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;If so, you should check where the function is called with the argument status_code. There might be a space between &lt;em&gt;status&lt;/em&gt; and &lt;em&gt;_code.&lt;/em&gt; In any case, at the point where the function is called, retype the name &lt;em&gt;status_code,&lt;/em&gt; just to be sure.&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;blockquote class="jive-quote"&gt;
&lt;p&gt;Bradleythall wrote:&lt;/p&gt;
&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I would like coldfusion to throw an error any time there is something in the arguments scope that is not an expected argument.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Use code similar to that in bold.&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;lt;cffunction name="testFunc"&amp;gt;&lt;/p&gt;&lt;p&gt;&amp;lt;cfargument name="arg"&amp;gt;&lt;/p&gt;&lt;p&gt;&amp;lt;cfargument name="myArg"&amp;gt;&lt;/p&gt;&lt;p&gt;&amp;lt;cfargument name="myOtherArg"&amp;gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&amp;lt;!--- List arguments in alphabetical order to enable comparison ---&amp;gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&amp;lt;cfif listSort(StructKeyList(arguments),"textnocase","asc") is not "arg,myArg,myOtherArg"&amp;gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;cfthrow message="An unexpected argument was passed to the function"&amp;gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&amp;lt;/cfif&amp;gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&amp;lt;cfreturn "Success!"&amp;gt;&lt;/p&gt;&lt;p&gt;&amp;lt;/cffunction&amp;gt;&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;lt;cfoutput&amp;gt;#testFunc(someOtherArg=1)#&amp;lt;/cfoutput&amp;gt;&lt;/p&gt;&lt;/div&gt;&lt;!-- [DocumentBodyEnd:0bc958f0-a095-4394-9795-f7d304cecf07] --&gt;</description>
      <pubDate>Mon, 21 Jul 2014 15:08:34 GMT</pubDate>
      <author>forums_noreply@adobe.com</author>
      <guid>https://forums.adobe.com/message/6568844?tstart=0#6568844</guid>
      <dc:date>2014-07-21T15:08:34Z</dc:date>
      <clearspace:dateToText>3 months 3 weeks ago</clearspace:dateToText>
      <clearspace:replyCount>2</clearspace:replyCount>
      <clearspace:objectType>0</clearspace:objectType>
    </item>
    <item>
      <title>Re: Disable cffunction accepting undeclared arguments?</title>
      <link>https://forums.adobe.com/message/6568643?tstart=0#6568643</link>
      <description>&lt;!-- [DocumentBodyStart:7ff6a93a-cc61-4e6e-ade6-bfa825429057] --&gt;&lt;div class="jive-rendered-content"&gt;&lt;p&gt;Please bear with me: I still don't get it. What do you mean by, &lt;em&gt;"when I invoke a function expecting "status_code" as an argument with an argument of "status" instead. The function executes with status_code at its default value and status just sitting in the arguments scope unused"&lt;/em&gt; ?&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Could you just paste a little code here to illustrate? Thanks.&lt;/p&gt;&lt;/div&gt;&lt;!-- [DocumentBodyEnd:7ff6a93a-cc61-4e6e-ade6-bfa825429057] --&gt;</description>
      <pubDate>Mon, 21 Jul 2014 14:58:56 GMT</pubDate>
      <author>forums_noreply@adobe.com</author>
      <guid>https://forums.adobe.com/message/6568643?tstart=0#6568643</guid>
      <dc:date>2014-07-21T14:58:56Z</dc:date>
      <clearspace:dateToText>3 months 3 weeks ago</clearspace:dateToText>
      <clearspace:objectType>0</clearspace:objectType>
    </item>
    <item>
      <title>Re: Disable cffunction accepting undeclared arguments?</title>
      <link>https://forums.adobe.com/message/6568527?tstart=0#6568527</link>
      <description>&lt;!-- [DocumentBodyStart:adfa56f3-3f91-46aa-9fea-5413f946e9ca] --&gt;&lt;div class="jive-rendered-content"&gt;&lt;p&gt;The nature of this project requires the long lists of arguments, and I do use names with optional arguments. The issue I'm having is when I invoke a function expecting "status_code" as an argument with an argument of "status" instead. The function executes with status_code at its default value and status just sitting in the arguments scope unused. This is never intentional, but happens on occasion. I would like coldfusion to throw an error any time there is something in the arguments scope that is not an expected argument.&lt;/p&gt;&lt;/div&gt;&lt;!-- [DocumentBodyEnd:adfa56f3-3f91-46aa-9fea-5413f946e9ca] --&gt;</description>
      <pubDate>Mon, 21 Jul 2014 14:40:21 GMT</pubDate>
      <author>forums_noreply@adobe.com</author>
      <guid>https://forums.adobe.com/message/6568527?tstart=0#6568527</guid>
      <dc:date>2014-07-21T14:40:21Z</dc:date>
      <clearspace:dateToText>3 months 3 weeks ago</clearspace:dateToText>
      <clearspace:replyCount>4</clearspace:replyCount>
      <clearspace:objectType>0</clearspace:objectType>
    </item>
    <item>
      <title>Re: Disable cffunction accepting undeclared arguments?</title>
      <link>https://forums.adobe.com/message/6564585?tstart=0#6564585</link>
      <description>&lt;!-- [DocumentBodyStart:e6508201-d51b-48c6-824e-5e102af11f22] --&gt;&lt;div class="jive-rendered-content"&gt;&lt;p&gt;First of all, if a function has a long list of arguments, say, more than 5 or 6 arguments, then it is complex. It is likely to be doing too many things. As a rule, a function should have just one responsibility. You should split such complex functions up into 2 or more separate functions.&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;To avoid ambiguity, use the name of the arguments when you call the function. For example, suppose that myFunc has arguments arg1, arg2,..., arg6, all of which are optional. Then you could call it as follows: myFunc(arg3="Journal",arg5="2014").&lt;/p&gt;&lt;/div&gt;&lt;!-- [DocumentBodyEnd:e6508201-d51b-48c6-824e-5e102af11f22] --&gt;</description>
      <pubDate>Sat, 19 Jul 2014 18:22:35 GMT</pubDate>
      <author>forums_noreply@adobe.com</author>
      <guid>https://forums.adobe.com/message/6564585?tstart=0#6564585</guid>
      <dc:date>2014-07-19T18:22:35Z</dc:date>
      <clearspace:dateToText>3 months 4 weeks ago</clearspace:dateToText>
      <clearspace:replyCount>5</clearspace:replyCount>
      <clearspace:objectType>0</clearspace:objectType>
    </item>
    <item>
      <title>Disable cffunction accepting undeclared arguments?</title>
      <link>https://forums.adobe.com/message/6558753?tstart=0#6558753</link>
      <description>&lt;!-- [DocumentBodyStart:a5d6f75d-576e-48e0-acee-40830f7caf7d] --&gt;&lt;div class="jive-rendered-content"&gt;&lt;p&gt;I have a lot of function calls with long lists of arguments and sometimes they can get mismatched (invoke with invokeargument status, function argument status_code). Coldfusion allows this to happen and makes the undeclared argument available in the arguments scope. Is there any way to disable this behavior so it only allows declared arguments and throws and error when it gets passed an argument that it doesn't expect?&lt;/p&gt;&lt;/div&gt;&lt;!-- [DocumentBodyEnd:a5d6f75d-576e-48e0-acee-40830f7caf7d] --&gt;</description>
      <pubDate>Thu, 17 Jul 2014 15:05:12 GMT</pubDate>
      <author>forums_noreply@adobe.com</author>
      <guid>https://forums.adobe.com/message/6558753?tstart=0#6558753</guid>
      <dc:date>2014-07-17T15:05:12Z</dc:date>
      <clearspace:dateToText>3 months 1 month ago</clearspace:dateToText>
      <clearspace:replyCount>6</clearspace:replyCount>
      <clearspace:objectType>0</clearspace:objectType>
    </item>
  </channel>
</rss>

