<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7268570014893367744</id><updated>2024-09-19T13:23:12.083-06:00</updated><category term="coldfusion"/><category term="best practices"/><category term="CFC"/><category term="caching"/><category term="component"/><category term="QoQ"/><category term="application.cfc"/><category term="architecture"/><category term="best practicies"/><category term="cache"/><category term="cacheduntil"/><category term="cachedwithin"/><category term="cfinclude"/><category term="cflock"/><category term="cfml"/><category term="closing tags"/><category term="dependency injection"/><category term="empty-element"/><category term="error handling"/><category term="frameworks"/><category term="generalist"/><category term="html"/><category term="inheritance"/><category term="named arguments"/><category term="object-oriented"/><category term="onMissingMethod"/><category term="performance"/><category term="proxy"/><category term="proxy class"/><category term="query"/><category term="query of queries"/><category term="readability"/><category term="scope"/><category term="self-closed"/><category term="self-closing"/><category term="shared memory"/><category term="thread safe"/><category term="threads"/><category term="timeout"/><category term="variables"/><category term="web development"/><category term="webservice"/><category term="xml"/><title type='text'>&amp;lt; CFBestPractices &amp;gt;</title><subtitle type='html'>Just because you can...doesn&#39;t mean you &lt;i&gt;should&lt;/i&gt;.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://cfbestpractices.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7268570014893367744/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://cfbestpractices.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Shawn Holmes</name><uri>http://www.blogger.com/profile/05110605868875966328</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>12</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7268570014893367744.post-2349158479227994054</id><published>2012-12-06T08:44:00.000-07:00</published><updated>2012-12-06T08:49:40.210-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="best practices"/><category scheme="http://www.blogger.com/atom/ns#" term="CFC"/><category scheme="http://www.blogger.com/atom/ns#" term="coldfusion"/><category scheme="http://www.blogger.com/atom/ns#" term="component"/><category scheme="http://www.blogger.com/atom/ns#" term="error handling"/><category scheme="http://www.blogger.com/atom/ns#" term="named arguments"/><category scheme="http://www.blogger.com/atom/ns#" term="onMissingMethod"/><category scheme="http://www.blogger.com/atom/ns#" term="proxy class"/><title type='text'>Prefer Passing Named Arguments Into CFCs</title><content type='html'>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlQejP-GfUnJlQvkNPeF2gSp8IfuZtf96OQEKBjX9wGdm540SwcpOhiK6RC2-q6cfoa82dcqoXo8m6779is-Aj_1sZrEv2hWlHR1t7VCxC4q_pQ8N9YFlO5CiKio20VLr9u_35ddrnBMOp/s1600/named-params.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;68&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlQejP-GfUnJlQvkNPeF2gSp8IfuZtf96OQEKBjX9wGdm540SwcpOhiK6RC2-q6cfoa82dcqoXo8m6779is-Aj_1sZrEv2hWlHR1t7VCxC4q_pQ8N9YFlO5CiKio20VLr9u_35ddrnBMOp/s320/named-params.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
Today&#39;s post is on the topic of how arguments are passed to methods, also known as the method&#39;s &lt;i&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Calling_convention&quot; target=&quot;_blank&quot;&gt;calling conventions&lt;/a&gt;&lt;/i&gt;.&amp;nbsp;When you call a method in a ColdFusion Component (or any user-defined function, for that matter), you do so by passing the parameters in the order they are declared in said method. This is typically the most used, path-of-least-resistance style of method calling.&lt;br /&gt;
&lt;br /&gt;
So, for example, if you have a method:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;cffunction name=&quot;doWork&quot; returntype=&quot;void&quot;&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;lt;cfargument name=&quot;foo&quot; type=&quot;string&quot; /&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;lt;cfargument name=&quot;bar&quot; type=&quot;string&quot; /&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;lt;cfreturn arguments.foo &amp;amp; &quot; - &quot; &amp;amp; arguments.bar /&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;/cffunction&amp;gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
You may call this method like so:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;cfoutput&amp;gt;#doWork( &#39;word1&#39;, &#39;word2&#39; )#&amp;lt;/cfoutput&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
In this example, the &lt;b&gt;order is important&lt;/b&gt;; they are passed into the method in the order of their declaration inside the method. In this case, word1, being parameter numero uno, is assigned as the value to the internal variable &#39;foo&#39;, while &#39;word2&#39; is likewise assigned to the internal variable bar.&lt;br /&gt;
&lt;br /&gt;
If, for some bizarre reason, you &lt;b&gt;absolutely had&lt;/b&gt; to specify the parameters in &lt;i&gt;different&lt;/i&gt; order than they are declared in the method, you could do so by taking advantage of a feature known as &lt;a href=&quot;http://en.wikipedia.org/wiki/Named_parameters&quot; target=&quot;_blank&quot;&gt;named parameters&lt;/a&gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;cfoutput&amp;gt;#doWork( bar=&#39;word2&#39;, foo=&#39;word1&#39; )#&amp;lt;/cfoutput&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
In this example, we&#39;ve swapped the order of our values, but the results this method call produces is the same as our first example; foo still gets &#39;word1&#39; and bar still gets &#39;word2&#39;, even though we supplied them in an entirely different order. The reason this works is because we explicitly pass in the name of the internal variable that we wish to have each appropriate value assigned to. Once this metadata exists, ColdFusion may apply the variables to their appropriate internal arguments, regardless of order. It&#39;s a convenient mechanism that grants a CF developer additional flexibility.&lt;br /&gt;
&lt;h2&gt;
Conventional Wisdom&lt;/h2&gt;
&lt;i&gt;Let me be clear:&lt;/i&gt; I &lt;b&gt;am&lt;/b&gt; &lt;b&gt;not&lt;/b&gt; advocating arbitrarily re-ordering your parameters. This was merely an example to demonstrate its use. Read on...&lt;br /&gt;
&lt;br /&gt;
The practices I preach here will always come back to the three main fundamentals: your code must be&amp;nbsp;&lt;b&gt;readable&lt;/b&gt;, &lt;b&gt;maintainable&lt;/b&gt;, and &lt;b&gt;consistent&lt;/b&gt;. Passing arguments in the order you declare them plays to all my core values, and will better serve the generation that follows you.&amp;nbsp;The intent of this particular best practice is rooted elsewhere. Rather than advocating arbitrarily re-arranging your variables, what I&#39;m recommending is simply &lt;i&gt;using named arguments across the board when calling methods in CFCs&lt;/i&gt;. At a high level, the reason is for &lt;b&gt;consistency&lt;/b&gt;--again, to improve one&#39;s craft, one must first pick a plan, and &lt;b&gt;stick to it&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
So, for those of you CF veterans who already thinking, &quot;Oh &lt;i&gt;God&lt;/i&gt;, though...it&#39;s incredibly verbose to do that. Why waste the extra time when nothing is gained? If you want to be consistent, why not err on the side of &amp;nbsp;&lt;b&gt;never&lt;/b&gt; passing variables as named arguments?&quot;&lt;br /&gt;
&lt;br /&gt;
Because...there is one particular case when you &lt;i&gt;must&lt;/i&gt; use named arguments.&lt;br /&gt;
&lt;h2&gt;
The Case of the Missing Method&lt;/h2&gt;
When ColdFusion 8 was released, a number of enhancements to ColdFusion Components were introduced--ones that had been sought after by the community since their introduction in CFMX. One such enhancement was the addition of a built-in method that all CFCs could implement: &lt;b&gt;onMissingMethod&lt;/b&gt;. CF developers expressed a need to build more robust frameworks and APIs around CFCs, but lacked the necessary tools to properly handle errors thrown by incorrect or missing method names. This functionality was vital not only in handling errors, but to facilitate the design of &lt;a href=&quot;http://en.wikipedia.org/wiki/Proxy_pattern&quot; target=&quot;_blank&quot;&gt;proxy&lt;/a&gt;-style classes. &lt;b&gt;onMissingMethod&lt;/b&gt; filled this gap in functionality and, when used, would correctly trap both &lt;i&gt;the name&lt;/i&gt;&amp;nbsp;(&lt;b&gt;MissingMethodName&lt;/b&gt;) of the method that was attempted, and &lt;i&gt;the arguments&lt;/i&gt;&amp;nbsp;(&lt;b&gt;MissingMethodArguments&lt;/b&gt;) that were passed to that method (if any). For the purposes of this post, we care about &lt;b&gt;MissingMethodArguments&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;MissingMethodArguments&lt;/b&gt; is the name of the variable inside &lt;b&gt;onMissingMethod&lt;/b&gt; that gains the arguments which were attempted, and they are poured into a struct. There is a very &lt;i&gt;specific, subtle difference&lt;/i&gt; between a failed method that has had its parameters passed via the named argument convention, as opposed to not. In the former case, you receive a wonderful struct of keys (names of the arguments) and their appropriate values:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;foo:word1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;bar:word2&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
This struct can be broken apart by the programmer into appropriate key/value pairs, and passed along to additional methods as needed.&lt;br /&gt;
&lt;br /&gt;
However, if they are passed as unnamed arguments, this is what the struct looks like when &lt;b&gt;onMissingMethod&lt;/b&gt; &amp;nbsp;fires:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;1:word1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;2:word2&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Notice that the names of the variables are suspiciously missing, replaced by numbers (which further confuses the issue, as the untrained eye might assume it is an array, but in fact, it is &lt;i&gt;still&lt;/i&gt; a struct). They&#39;re missing because &lt;b&gt;they were never provided&lt;/b&gt;. Remember how we arrived here? You called a method. It didn&#39;t exist. &lt;b&gt;onMissingMethod&lt;/b&gt; caught this, and took control, capturing the metadata in the process. And in your original method call, all you specified were values. No actual variable names were present. So, I ask you, how &lt;i&gt;could&lt;/i&gt;&amp;nbsp;ColdFusion know the names of the variables?&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;It couldn&#39;t.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
You&#39;re now in a pickle, because if you expect to set up any kind of component architecture to pass these nameless variables to, you won&#39;t be able to, because you&#39;ll be unable to programmatically map those values to the variable names they originally represented.&lt;br /&gt;
&lt;br /&gt;
So, err on the side of safety. Develop a standard within your CF code in which you call methods on your CFCs by using named arguments. That way, when you want to perform more robust error handling and build proxy classes by taking advantage of &lt;b&gt;onMissingMethod&lt;/b&gt;, the argument names won&#39;t be missing.&lt;br /&gt;
&lt;br /&gt;
Unless, of course, you enjoy going back and changing every...single...method...call.&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;iframe bordercolor=&quot;#000000&quot; frameborder=&quot;0&quot; height=&quot;150&quot; hspace=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;http://ad.doubleclick.net/adi/N963.148119.BLOGGEREN/B6527721.7;dcadv=3632184;sz=180x150;lid=41000613802463762;pid=UBM9780201616224;usg=AFHzDLuo3YRag-DD8ZmMjJsyBBbRN5tkIA;adurl=http%253A%252F%252Fwww.cdsbooksdvds.com%252Fproduct.jhtm%253Fsku%253DUBM9780201616224;pubid=603632;price=%2451.59;title=The+Pragmatic+Programm...;merc=CDS+Books+and+DVDS;imgsrc=http%3A%2F%2Fc380038.r38.cf1.rackcdn.com%2F9780201616224.jpg;width=67;height=85&quot; vspace=&quot;0&quot; width=&quot;180&quot;&gt;&lt;/iframe&gt;&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cfbestpractices.blogspot.com/feeds/2349158479227994054/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cfbestpractices.blogspot.com/2012/12/prefer-passing-named-arguments-into-cfcs.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7268570014893367744/posts/default/2349158479227994054'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7268570014893367744/posts/default/2349158479227994054'/><link rel='alternate' type='text/html' href='http://cfbestpractices.blogspot.com/2012/12/prefer-passing-named-arguments-into-cfcs.html' title='Prefer Passing Named Arguments Into CFCs'/><author><name>Shawn Holmes</name><uri>http://www.blogger.com/profile/05110605868875966328</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlQejP-GfUnJlQvkNPeF2gSp8IfuZtf96OQEKBjX9wGdm540SwcpOhiK6RC2-q6cfoa82dcqoXo8m6779is-Aj_1sZrEv2hWlHR1t7VCxC4q_pQ8N9YFlO5CiKio20VLr9u_35ddrnBMOp/s72-c/named-params.png" height="72" width="72"/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7268570014893367744.post-8476191792532982949</id><published>2012-11-12T15:31:00.000-07:00</published><updated>2012-11-13T10:01:34.568-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="best practices"/><category scheme="http://www.blogger.com/atom/ns#" term="cfml"/><category scheme="http://www.blogger.com/atom/ns#" term="closing tags"/><category scheme="http://www.blogger.com/atom/ns#" term="coldfusion"/><category scheme="http://www.blogger.com/atom/ns#" term="empty-element"/><category scheme="http://www.blogger.com/atom/ns#" term="html"/><category scheme="http://www.blogger.com/atom/ns#" term="readability"/><category scheme="http://www.blogger.com/atom/ns#" term="self-closed"/><category scheme="http://www.blogger.com/atom/ns#" term="self-closing"/><category scheme="http://www.blogger.com/atom/ns#" term="xml"/><title type='text'>Know the Ramifications of Closing Empty-Element Tags</title><content type='html'>There are two ways to write a CFML tag that has no opener and closer, aka an &quot;empty-element&quot; tag.&lt;br /&gt;
&lt;br /&gt;
Way #1:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;cfset var foo = &quot;bar&quot;&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Way #2:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;cfset var foo = &quot;bar&quot; /&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Note that the only difference here is the /&amp;gt; on the end of the 2nd style.&lt;br /&gt;
&lt;br /&gt;
Now, let&#39;s not fool ourselves into thinking there is a right or wrong way to do this: &lt;b&gt;it is a personal preference&lt;/b&gt;, plain and simple. There is no right or wrong way to do this. There is no &quot;compliance&quot; engine that you can pass your CFML through to give it a green or red light. There are no XHTML-style standards that dictate CFML must be written the first or the second way. In many cases (both for and against), programmers ultimately fall on the side of aesthetics, feeling strongly that one way is far more pleasing to the eye as the other. Additionally, IDEs struggle with correctly parsing one style over the other. And although the battle rages on as to which way is correct (there isn&#39;t one), people often miss the point of the real issue at hand...&lt;br /&gt;
&lt;br /&gt;
...when you close an empty-element tag in CFML, you have the potential to &lt;i&gt;change behavior&lt;/i&gt;.&lt;br /&gt;
&lt;h3&gt;
Logic Behind the Close&lt;/h3&gt;
So, why all the holy wars about closing vs. non-closing tags? Where did this all come from?&amp;nbsp;CFML is a language that is based upon/been influenced by other tag-based languages like HTML and XML. HTML has always been very loosey-goosey with how it has allowed developers to code the markup. For example, this is fine:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;br&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
but so is this:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;br /&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
XML, on the other hand, isn&#39;t nearly as forgiving. In many cases, having a single element in XML like this:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;person&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
without a closing:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;/person&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
will cause the program parsing the XML to throw errors, advising the user that &lt;b&gt;the element must be closed&lt;/b&gt;. If it is a single line element that doesn&#39;t encapsulate any child elements, it is considered an &quot;empty-element&quot; and, thus, is written as:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;person /&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The mystery, therefore, begins to unravel. For, you see:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;person /&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
is a closed element. From an XML standpoint, is the same as:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;person&amp;gt;&amp;lt;/person&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
which is really all the XML consumer cares about. Which also means that from an HTML standpoint:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;br /&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
is the same as:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;br&amp;gt;&amp;lt;/br&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
But! There is no concept of an empty open/close break-tag element in HTML; break-tag is, by nature, meant to exist on its own, to cause a newline at the end of a piece of text. Break-tag was never meant to encapsulate content like parent/child nodes in XML. So the concept of &quot;wrapping&quot; a break-tag around content is moot in HTML. Therefore, it makes no sense to enforce a break-tag to be closed, and HTML engines know to allow this tag to sit by itself, without a closing tag, or a self-close.&lt;br /&gt;
&lt;h3&gt;
Enter CFML&lt;/h3&gt;
Using the above logic, it stands to reason that:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;cfset var foo = &quot;bar&quot; /&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
is the same as:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;cfset var foo = &quot;bar&quot;&amp;gt;&amp;lt;/cfset&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Now, does this syntax actually exist or have meaning in CFML? No. Just like the HTML example of closing a break-tag, closing a cfset tag in a similar way doesn&#39;t accurately convey a concept that&#39;s needed; the requirement for an opening and closing tag to surround other content. The reason it has validity and importance in XML is that XML is, by nature, &lt;i&gt;undefined&lt;/i&gt;. Each and every time XML is produced, &lt;i&gt;it describes itself&lt;/i&gt;. Which means there is no way for a consumer to know ahead of time &quot;Oh, hey, if you come across a &amp;lt;person&amp;gt; tag with no closer, just assume it&#39;s self-closed.&quot;, like HTML can with a break-tag; break-tags are a part of HTML.&lt;br /&gt;
&lt;br /&gt;
Likewise, cfset is a part of CFML. So, CF servers can be tailored to slurp up both styles, but also have those predefined rules in place, so that when they encounter a cfset-tag with no closer, they can assume it is closed.&lt;br /&gt;
&lt;h3&gt;
CFML Goes Both Ways&lt;/h3&gt;
If that were the end of the story, I could close out the post here, and finish with &quot;use what you like, it really doesn&#39;t matter, it&#39;s a personal style choice.&quot;&lt;br /&gt;
&lt;br /&gt;
Unfortunately, it&#39;s not that simple. What happens when you use a cf tag that can be both written as empty &lt;i&gt;and&lt;/i&gt; as having a start and end tag?&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;cfmodule template=&quot;ctag.cfm&quot; attr1=&quot;foo&quot; attr2=&quot;bar&quot;&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
vs.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;cfmodule template=&quot;ctag.cfm&quot; attr1=&quot;foo&quot; attr2=&quot;bar&quot; /&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;Now&lt;/i&gt;, we have a &lt;b&gt;change in behavior&lt;/b&gt;. The reason is because cfmodule is designed to act as an alternate way of calling a CustomTag, which itself can be written to surround data, implying a start and end tag. When a CustomTag is written this way, additional logic within the tag can be run during ExecutionMode:Start and ExecutionMode:End. In order to allow tags to be checked for which execution mode they are in, the CF engine &lt;i&gt;must call it twice&lt;/i&gt;.&lt;br /&gt;
&lt;h3&gt;
Language Changes&lt;/h3&gt;
What happens when the language itself changes? Consider cfcache from 8 to 9, and the subsequent effect this had on behavior, as relayed on&amp;nbsp;&lt;a href=&quot;http://www.raymondcamden.com/index.cfm/2011/8/11/cfcache-user-Watch-out-for-this-mistake&quot; target=&quot;_blank&quot;&gt;Ray Camden&#39;s blog&lt;/a&gt;. In ColdFusion 8, cfcache was only writable as empty-element:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;cfcache timespan=&quot;#CreateTimeSpan(0,1,0,0)#&quot; /&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;...content to be cached displayed in remainder of template.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
CF8 would parse that tag, and carry on as needed, as it only knew of one way to handle the tag: in seeing it once. However, the syntax of cfcache changed in CF9:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;cfcache timespan=&quot;#CreateTimeSpan(0,1,0,0)#&quot;&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;...content to be cached&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;/cfcache&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
allowing the CF developer more granular control over sub-components of cached data. Knowing what you know now, you can see how self-closing cfcache tags moved from CF8 to CF9 were seen by the CF9 engine:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;cfcache timespan=&quot;#CreateTimeSpan(0,1,0,0)#&quot;&amp;gt;&amp;lt;/cfcache&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;...content to be cached which is instead completely skipped and not cached at all&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
What is worse is that &lt;i&gt;there is no consistency across the entire library, &lt;/i&gt;so no one style can be safely adopted across the board.&amp;nbsp;Some tags will require a closing tag, some will not--and some don&#39;t allow it! &lt;a href=&quot;http://www.webveteran.com/blog/web-coding/coldfusion/coldfusion-tag-usage-syntax/&quot; target=&quot;_blank&quot;&gt;Jules Gravinese provides an excellent resource&lt;/a&gt; for the entire suite of CFML tags (as of CF9) which outline the allowances and troublemakers--if a CF10 resource crops up, I&#39;ll add it here as well.&lt;br /&gt;
&lt;h3&gt;
Use What is Appropriate&lt;/h3&gt;
The point of all of this is to be &lt;b&gt;informed&lt;/b&gt; when you sit down to pick a style (and I encourage you to pick one and stick with it--&lt;i&gt;consistency is key&lt;/i&gt;). But, set your expectations appropriately. Whichever style you choose, you will inevitably come across an instance where you will be forced to violate that style. &lt;i&gt;It will suck&lt;/i&gt;. It will &lt;i&gt;look like shit&lt;/i&gt;. You will have this wonderfully beautiful work of art you call your web application, and it will be horrifically defiled by the presence of the style to which you oppose.&lt;br /&gt;
&lt;br /&gt;
...but, at least you will know &lt;b&gt;why&lt;/b&gt; it had to be written that way, and it won&#39;t be because of personal preference.</content><link rel='replies' type='application/atom+xml' href='http://cfbestpractices.blogspot.com/feeds/8476191792532982949/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cfbestpractices.blogspot.com/2012/11/know-ramifications-of-closing-empty.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7268570014893367744/posts/default/8476191792532982949'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7268570014893367744/posts/default/8476191792532982949'/><link rel='alternate' type='text/html' href='http://cfbestpractices.blogspot.com/2012/11/know-ramifications-of-closing-empty.html' title='Know the Ramifications of Closing Empty-Element Tags'/><author><name>Shawn Holmes</name><uri>http://www.blogger.com/profile/05110605868875966328</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7268570014893367744.post-217724258534208238</id><published>2012-11-05T08:42:00.000-07:00</published><updated>2012-11-05T08:42:37.336-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="architecture"/><category scheme="http://www.blogger.com/atom/ns#" term="best practices"/><category scheme="http://www.blogger.com/atom/ns#" term="coldfusion"/><category scheme="http://www.blogger.com/atom/ns#" term="frameworks"/><category scheme="http://www.blogger.com/atom/ns#" term="generalist"/><category scheme="http://www.blogger.com/atom/ns#" term="web development"/><title type='text'>Start Seeing &quot;The Big Picture&quot;</title><content type='html'>You&#39;ve picked CFML as something you enjoy being paid to do--that&#39;s a start. It would be foolish to not have a core competency in today&#39;s technological industry. But whether you choose CFML, ASP, Ruby, Python, or even (God help you) PHP...&lt;i&gt;this is not enough&lt;/i&gt;. In order to be truly effective at what you do when you develop software, you need to have a &lt;b&gt;broad vision&lt;/b&gt; of what that will look like. The Covey quote sums this up succinctly: &lt;i&gt;&quot;&lt;a href=&quot;http://en.wikipedia.org/wiki/The_Seven_Habits_of_Highly_Effective_People&quot;&gt;Begin with the end in mind&lt;/a&gt;.&quot;&lt;/i&gt;&amp;nbsp;Your vision of what you craft may resemble a cog in the grand machine, but this vision is jaded, because your focus remains &lt;b&gt;on the cog&lt;/b&gt;. What separates amateur developers from experienced one is often this trait, to shift your focus away from the cog &lt;i&gt;to the machine itself&lt;/i&gt;. What does that look like? Well, in environments that lack broad vision, software usually ends up looking more like a massive tentacled nightmare, its programmatic hooks weaving their way through your company&#39;s systems, attaching their claws to machine and to code. In short, these environments that lack the foresight to consider for their surroundings live like &lt;i&gt;parasite and host&lt;/i&gt;, completely reliant on one another for stability.&lt;br /&gt;
&lt;br /&gt;
...can you tell I&#39;ve had some experience in corporate IT?&lt;br /&gt;
&lt;br /&gt;
Melodrama aside, this may not be your situation. Perhaps your experience comes from a smaller company, and have had the luxury of needing only a few custom systems. In this environment, you benefit from refining and honing your skills in a language like CFML, which naturally gives you the sense that you are an expert in your field. This is a hazardous point of view to latch on to, because the challenges that you fail to take on by remaining in your comfort zone shield you from the much bigger picture that exists outside of your realm of expertise.&lt;br /&gt;
&lt;br /&gt;
In other words, if you keep building the same solutions over and over, you&#39;ll not only &lt;b&gt;NOT&lt;/b&gt; grow as a developer, but you&#39;ll begin to take on the skewed perspective that you are an expert at what you do.&lt;br /&gt;
&lt;br /&gt;
In order to truly gain expertise in your field, you need to be able to speak intelligently beyond the realm of just the language you build web applications in. This comes, in turn, from first-hand experience in developing those solutions, and having a hand in different layers in the cake. If your job allows you to take on extra levels of responsibility, in technical areas you&#39;re not normally exposed to, &lt;i&gt;take them on&lt;/i&gt;. If you can strategically &lt;b&gt;learn while doing&lt;/b&gt;, more power to you. If this isn&#39;t an option where you work, perhaps research after hours, or a side-project may be beneficial. Reading isn&#39;t enough, though--you have to actually &lt;i&gt;do something new&lt;/i&gt;.&lt;br /&gt;
&lt;br /&gt;
Let me repeat that.&lt;br /&gt;
&lt;br /&gt;
Reading tech blogs after hours, or picking up the occasional programming book is not enough; &lt;i&gt;you must physically do something with your hands at a keyboard, producing code&lt;/i&gt;.&lt;br /&gt;
&lt;h2&gt;
Working After Work&lt;/h2&gt;
The question of doing extra work in your spare time comes up a lot, especially from programmers getting started in the industry. Unsurprisingly, this question is often met with very polarized answers. Extremely talented (and sometimes angry) developers will often argue against having to do side-projects, but they themselves may benefit from a work environment that empowers them to do their own research / education while at work, so they are growing organically as a part of their process.&lt;br /&gt;
&lt;br /&gt;
Not all developers have that luxury.&lt;br /&gt;
&lt;br /&gt;
CFML developers, especially, can be very easily caught in the trap of working in a very narrow technological zone, which keeps them comfortable but prevents them from developing expertise at the broad architectural level.&amp;nbsp;Programmers with many years under their belt often refer to this as being able to see &quot;the big picture&quot;.&lt;br /&gt;
&lt;br /&gt;
Even if you have mastered CFML, and can troubleshoot any catastrophe or build any MySpace that&#39;s put in front of you, you are working within an industry that is not two dimensional. Consider the nature of the systems you work with when you first started building ColdFusion apps. It may have looked like this:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeuKykJy9R2UeWR5rV2_-ir8xD3mAGOx14rBX0Smd5gGlzxRFv-PQQ3lCJz0D5IrLfoY37exUckXWFWJOXYCSQ6ytReDoMfNjSMdnPZVLLvKUxfqBn_17XhievZX1rEyDGzuWVmlfetk3r/s1600/pic1.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;106&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeuKykJy9R2UeWR5rV2_-ir8xD3mAGOx14rBX0Smd5gGlzxRFv-PQQ3lCJz0D5IrLfoY37exUckXWFWJOXYCSQ6ytReDoMfNjSMdnPZVLLvKUxfqBn_17XhievZX1rEyDGzuWVmlfetk3r/s320/pic1.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
More than likely, you built a CF app, tied it to a database that your company happened to be using at the time (in this case, Microsoft SQL Server) and when you were ready, you tossed your application on a web server (Internet Information Server is an example) and called it a day.&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
Perhaps over time, you worked with a few new databases, changing your view of the development spectrum to this:&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoQttgt6vrc_5mFggztNr0uvPdiYhuX6WZ7YQZu896b7faM6fnToEwW2J8ye8X8uMdmccL10nm1emAVdIIbKwPUOi5E-Ne99FOJBKlW2x06m1VIJfxPCYcmGQaS_hHT4zAFFRcVIBd7vIS/s1600/pic2.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;277&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoQttgt6vrc_5mFggztNr0uvPdiYhuX6WZ7YQZu896b7faM6fnToEwW2J8ye8X8uMdmccL10nm1emAVdIIbKwPUOi5E-Ne99FOJBKlW2x06m1VIJfxPCYcmGQaS_hHT4zAFFRcVIBd7vIS/s320/pic2.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
You may even get the opportunity to move off of the Microsoft platform and play with different web servers:&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJz066OrESLVN5-XijcbnK3cA1cSj6HhPdd4EJFctkQx_Pj1n1RWu0akIgVT5nicjLQryA6CQcBl0NSks9ILJtktzFQQwJ5Mv9hMT0pt24aiOlCmGXee_4LEY0tn-8-nhTPCqwjoZCwJm-/s1600/pic3.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;277&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJz066OrESLVN5-XijcbnK3cA1cSj6HhPdd4EJFctkQx_Pj1n1RWu0akIgVT5nicjLQryA6CQcBl0NSks9ILJtktzFQQwJ5Mv9hMT0pt24aiOlCmGXee_4LEY0tn-8-nhTPCqwjoZCwJm-/s320/pic3.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
Some web application developers are expected to work on the client as well as the server; if you fall in to this category, your view of web app development extends further:&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaSXDD95xSA3e7qEiQwZBbh9ONv7y-ucM6Bp04WWAXdCZCwS9TJ1FiLIf0xfWhySLJ9mLQkv1kscuoq-uF0bGTI6zdEJP1NMuO8fdd_02U7QFdlNbNtVveRsvyXahvGThbsxMYCChpyYDF/s1600/pic4.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;211&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaSXDD95xSA3e7qEiQwZBbh9ONv7y-ucM6Bp04WWAXdCZCwS9TJ1FiLIf0xfWhySLJ9mLQkv1kscuoq-uF0bGTI6zdEJP1NMuO8fdd_02U7QFdlNbNtVveRsvyXahvGThbsxMYCChpyYDF/s320/pic4.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
Many CF developers are content with this tool set; it is enough to get the job done.&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;b&gt;But what if you need to do more than just &quot;get the job done&quot;?&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
What if the day arrives and meeting the &lt;i&gt;status quo&lt;/i&gt; is no longer enough? What if you need to drive innovation at your company? Do you think a picture like this is sufficient?&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
On the flip side, what if your expertise is called into question when your application is loaded into production &lt;i&gt;and everything unwinds?&lt;/i&gt; Is this picture above going to be sufficient enough for you to address the&amp;nbsp;intricacies&amp;nbsp;of:&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Load Balancing / Clustering&lt;/li&gt;
&lt;li&gt;Synchronous vs. Asynchronous Data Communication&lt;/li&gt;
&lt;li&gt;Caching (Client &lt;i&gt;and&lt;/i&gt; Server)&lt;/li&gt;
&lt;li&gt;Security (Users / Roles / Permissions)&lt;/li&gt;
&lt;li&gt;Server / VM Tuning&lt;/li&gt;
&lt;li&gt;Frameworks / MVC&lt;/li&gt;
&lt;li&gt;Threads / Concurrent Programming&lt;/li&gt;
&lt;li&gt;Non-Relational (NoSQL) databases&lt;/li&gt;
&lt;li&gt;VPNs / Tunneling / Network Topology&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
This goes both directions. Having a &lt;i&gt;micro&lt;/i&gt;scopic view of technology is just as important as having a &lt;i&gt;macro&lt;/i&gt;scopic one. Think about all the things ColdFusion does for you automatically--and if those were suddenly taken away, would you be able to cope? Have you ever written a program that:&lt;/div&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;Allocates / Deallocates its own memory,&lt;/li&gt;
&lt;li&gt;Deals with strict variable types,&lt;/li&gt;
&lt;li&gt;Treats many integral types as immutable,&lt;/li&gt;
&lt;li&gt;Has its own Client / Sever handles, accepts socket requests, forks off new processes,&lt;/li&gt;
&lt;li&gt;Caused you to construct HTTP requests / responses by hand,&lt;/li&gt;
&lt;li&gt;Communicated across the wire in a language other than HTTP (ie. SMTP, POP3, IMAP, etc.)&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
The list goes on.&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
A welder has a luxury we don&#39;t. He goes to school for a month, learns a finite amounts of welds, gains a certificate, and can weld using everything he&#39;s learned until the day he retires. We don&#39;t have a finite amount of welds in our tool box. Our tools are constantly changing. New languages sprout up every day, that we (more than likely) have to integrate with. New concepts of applications are being invented that take our traditional methods and render them obsolete. Hardware changes equally as fast; can we expect our web application, designed using concepts we perfected in 2001...to function adequately on a wireless tablet whose technology or network infrastructure didn&#39;t even exist then?&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Whether you are designing a new web&amp;nbsp;application&amp;nbsp;from the ground up, or troubleshooting an existing one, you will need every tool at your disposal, and in order to have the tools--you need to be able to &lt;i&gt;see the big picture&lt;/i&gt;. Your core competency in a particular language will remain your meal-ticket, but if that&#39;s all you have, you&#39;ll be building solutions with blinders and constructing&amp;nbsp;criticisms&amp;nbsp;in the dark. I recently completed the 8 week long troubleshooting session involving a 12 year old system. The app was a classic ASP site (already outdated by today&#39;s tech), utilized ActiveX objects (COM+) that were written in C++ and compiled to DLLs...&lt;i&gt;of which I wasn&#39;t allowed to see any source to&lt;/i&gt;. I was forced to analyze Wireshark logs for network traffic changes and observe behavior that wasn&#39;t 100% reproducible. Luckily, I was able to use my C++ experience to isolate a single function that was a cause of the chaos: a method that read HTTP headers by &lt;b&gt;specifying a byte length&lt;/b&gt;...which the original developers &lt;i&gt;had not made large enough&lt;/i&gt;. It had worked quietly for years, but with the introduction of SSO to our company, new session cookies had been added to the HTTP stream, which were crammed on the end of the headers, and the function began to fail, causing the app (and the systems it touched) to spiral into chaos.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Broadening your knowledge of development, languages, programming, and system architecture is going to grant you a deeper perspective into the systems we must create and maintain. And as time goes on, and your perspective grows, you&#39;ll come to realize (as many &lt;i&gt;generalists&lt;/i&gt; do)&amp;nbsp;that&amp;nbsp;applications&amp;nbsp;look less like this:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaSXDD95xSA3e7qEiQwZBbh9ONv7y-ucM6Bp04WWAXdCZCwS9TJ1FiLIf0xfWhySLJ9mLQkv1kscuoq-uF0bGTI6zdEJP1NMuO8fdd_02U7QFdlNbNtVveRsvyXahvGThbsxMYCChpyYDF/s1600/pic4.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;211&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaSXDD95xSA3e7qEiQwZBbh9ONv7y-ucM6Bp04WWAXdCZCwS9TJ1FiLIf0xfWhySLJ9mLQkv1kscuoq-uF0bGTI6zdEJP1NMuO8fdd_02U7QFdlNbNtVveRsvyXahvGThbsxMYCChpyYDF/s320/pic4.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
and more like this:&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-gr1hdtHZid_oG0gfM72UGVkliBCh2Th5ASM0elDjtmOdA_lmsbbJlXK5JZPz20l5pL_c-jpZ6x9xpzQzrlavANr3KKNmqDZQx6g4SvuNk4MBI3TJyKehfyAEe3ECBU7H8ZQ6ADw1eQtb/s1600/B-shub-niggurath.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-gr1hdtHZid_oG0gfM72UGVkliBCh2Th5ASM0elDjtmOdA_lmsbbJlXK5JZPz20l5pL_c-jpZ6x9xpzQzrlavANr3KKNmqDZQx6g4SvuNk4MBI3TJyKehfyAEe3ECBU7H8ZQ6ADw1eQtb/s320/B-shub-niggurath.jpg&quot; width=&quot;232&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cfbestpractices.blogspot.com/feeds/217724258534208238/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cfbestpractices.blogspot.com/2012/11/start-seeing-big-picture.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7268570014893367744/posts/default/217724258534208238'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7268570014893367744/posts/default/217724258534208238'/><link rel='alternate' type='text/html' href='http://cfbestpractices.blogspot.com/2012/11/start-seeing-big-picture.html' title='Start Seeing &quot;The Big Picture&quot;'/><author><name>Shawn Holmes</name><uri>http://www.blogger.com/profile/05110605868875966328</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeuKykJy9R2UeWR5rV2_-ir8xD3mAGOx14rBX0Smd5gGlzxRFv-PQQ3lCJz0D5IrLfoY37exUckXWFWJOXYCSQ6ytReDoMfNjSMdnPZVLLvKUxfqBn_17XhievZX1rEyDGzuWVmlfetk3r/s72-c/pic1.png" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7268570014893367744.post-1089968125306558702</id><published>2012-05-31T19:42:00.000-06:00</published><updated>2012-05-31T19:43:03.222-06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="best practices"/><category scheme="http://www.blogger.com/atom/ns#" term="CFC"/><category scheme="http://www.blogger.com/atom/ns#" term="cfinclude"/><category scheme="http://www.blogger.com/atom/ns#" term="coldfusion"/><category scheme="http://www.blogger.com/atom/ns#" term="component"/><category scheme="http://www.blogger.com/atom/ns#" term="dependency injection"/><category scheme="http://www.blogger.com/atom/ns#" term="inheritance"/><category scheme="http://www.blogger.com/atom/ns#" term="object-oriented"/><title type='text'>Avoid Using CFINCLUDE Within a CFC</title><content type='html'>When ColdFusion was gaining popularity back in the&amp;nbsp;&lt;a href=&quot;http://en.wikipedia.org/wiki/Browser_wars#The_first_browser_war&quot;&gt;dark ages&lt;/a&gt;&amp;nbsp;&lt;a href=&quot;http://en.wikipedia.org/wiki/America_Online&quot;&gt;of the Internet&lt;/a&gt;, it was to serve a very specific purpose, to allow developers to quickly build dynamic HTML pages that could display...well, dynamic content.&amp;nbsp;Displaying fragments of content dynamically is one of the first lessons a new ColdFusion developer is taught. Headers, navigation, footers, layout...these are some concrete examples of such a fragment of content. A single piece an entire HTML page can be written once, and included in all of the HTML pages in your website. In the classic ColdFusion one-liner formula, this is accomplished by a single tag, CFINCLUDE:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;cfinclude template=&quot;footer.cfm&quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this single one line of code, not only were ColdFusion developers empowered to use best practices (read: code reuse) when developing their dynamic HTML sites, it could be done without a second thought.&lt;br /&gt;
&lt;br /&gt;
And thus, so began the breeding of an entire generation of ColdFusion developers that would build websites...without second thoughts.&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
--&lt;/div&gt;
&lt;br /&gt;
The sun rose and fell, and many seasons came and went. The sands sifting through the hourglass saw the rise and fall of new web browsers, while new technologies began to emerge, and concerted efforts were put towards taking amateurish scripting languages and polishing them into robust programming environments. ColdFusion was no exception. Future revisions added the ability to handle more complex data structures (4), user-defined functions (5), and eventually, it moved from a C++ core to sitting atop Java, introducing us to the concept of having our own objects or &quot;components&quot; (6). It was at this point in ColdFusion&#39;s history that an entirely new group of developers were exposed to ColdFusion: those with a Java background, and whom had primarily been focused on building Java-based applications. These new Java programmers often had serious software engineering backgrounds, and were either Comp Sci graduates with tons of programming theory under their belt, or were of the extreme hacker/nerd culture, and deeply invested in learning/building Java apps, and being exposed to as much Java-related material as possible. It was a movement after all; when Java rose to popularity, its &quot;write once, run anywhere&quot; mantra attracted both conformists and naysayers alike, out of sheer curiosity.&lt;br /&gt;
&lt;br /&gt;
Now, this new group of software engineers that were dabbling in Java-based ColdFusion, and who previously had been tweaking JVMs, managing threads, getting applications to compile and run on multiple platforms and embedded devices, and debating tirelessly over garbage collectors...were mixed in with a very, very different breed of developers; a community which was essentially based around a bunch of individuals tasked with building a dynamic website, displaying a list of database records on a screen, or sending an email when a submit button was clicked. For a good majority of these existing CF developers, their focus had been around dynamic HTML, so &quot;software development&quot; in their eyes was about as advanced as how to properly categorize your folders and files, and how to name your variables correctly. Although I can&#39;t speak for them, I&#39;m certain the Java-based portion of these developers looked upon these debates as a bunch of children fighting over who gets the merry-go-round next on the playground. A simple question like &quot;How should you build your application?&quot; was a no-brainer, &quot;Uh, it should be an object oriented application. End of discussion.&quot; But the concept of object-oriented development was a complete and total mystery to many of the existing members of the ColdFusion community. They had no idea what this new &quot;.CFC&quot; extension was, or why it was added to CF and what importance it held. They had never heard of polymorphism or encapsulation, and I wouldn&#39;t be surprised if they thought&amp;nbsp;inheritance&amp;nbsp;had something to do with what you get when your rich uncle dies.&lt;br /&gt;
&lt;br /&gt;
As time went on, the polarization of the CF community lessened. There were fewer people in the community that were &quot;just old-school CFers&quot; or &quot;just fresh over from a Java project&quot;, more and more had richer backgrounds in both website development and software application development. And, ColdFusion itself continued to evolve into a much more robust software development language, with each new major release adding additional functionality that spoke to both basic dynamic HTML website development, as well as truer object-oriented designs. What has failed to evolve, however, is our approach in teaching new people CFML. We have the capacity to build Enterprise-grade web apps in ColdFusion, and we still fall back on our &quot;Fast Track to ColdFusion&quot; courses when introducing it to new people, focusing on those easy-to-execute tasks, like our famous CFINCLUDE one-liners.&lt;br /&gt;
&lt;br /&gt;
Try not to forget the roots of ColdFusion and what caused its early iterations to provide the functionality that it did: CFINCLUDE was made a part of CFML long before classes/components, user-defined functions, and even complex data structures existed. As developers pushed the early versions of CF to its fullest capabilities, CFINCLUDE found multiple uses, commonly employed to setup global variables across an entire web app. But, when newer versions of CF were released, and cleaner ways of doing the same thing were introduced, CF developers often failed to embrace these new approaches, opting to stay in their comfort zone, and justifying it by saying &quot;I can do it multiple ways in CF&quot; or &quot;There is no right or wrong way to do it&quot;, which is just another way of saying, &quot;I can&#39;t be bothered with learning why I&#39;m possibly wrong, because the current way I&#39;m doing it works.&quot;&lt;br /&gt;
&lt;br /&gt;
When you&#39;re in Java, you can&#39;t just open a class you&#39;re working on, and &quot;include&quot; another class halfway through the methods to gain its functionality. Thought and care has to be put into designing Java classes that interact with others, and often, an entire methodology is agreed upon, just to decide how objects speak to one another. This is far cry from the &quot;easy way out&quot; that CF allows by inserting CFINCLUDE statements blindly across your application to gain access to a certain set of variables or a set of functions. Your focus when building applications in ColdFusion (and quite frankly, in any language) should be:&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;readability&lt;/b&gt;,&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;maintainability&lt;/b&gt;, and&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;consistency in approved development methodologies&lt;/b&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
Let me be the first to say that opening up another developer&#39;s CFC, expecting to see a well-thought out design of private and public methods that accurately describe the purpose of the class...and seeing a bunch of random CFINCLUDEs halfway through the file...breaks all three of those rules. In almost every case, if your CFC needs to utilize functionality somewhere else in your application, the correct way to implement code reuse is either:&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;subclassing the CFC using the &quot;extends&quot; attribute of CFCOMPONENT, which in turn, creates a parent-child hierarchy (Congratulations, you&#39;ve just implemented&amp;nbsp;&lt;span style=&quot;color: #0000ee;&quot;&gt;&lt;u&gt;inheritance&lt;/u&gt;&lt;/span&gt;), or&lt;/li&gt;
&lt;li&gt;injecting a previously created CFC object in, via a getter/setter method; this is the more common route among the&amp;nbsp;&quot;&lt;a href=&quot;http://en.wikipedia.org/wiki/Dependency_injection&quot;&gt;Dependency-Injection&lt;/a&gt;&quot;&amp;nbsp;school of thought which favors an age-old OO best practice: &lt;i&gt;Favor &lt;a href=&quot;http://en.wikipedia.org/wiki/Composition_over_inheritance&quot;&gt;Composition Over&amp;nbsp;Inheritance&lt;/a&gt;&lt;/i&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
A ColdFusion Component (CFC) is a programming structure in ColdFusion that allows you to create classes of objects, which implies that you are working in an Object-Oriented design. So treat it like that. &lt;b&gt;Be the compiler&lt;/b&gt; that CF doesn&#39;t have. Enforce consistency in your design, so that you aren&#39;t randomly calling CFINCLUDE inside a class method, because you know it wouldn&#39;t fly in Java (or any other language). Take the time to &lt;b&gt;learn how objects are supposed to interact with one another&lt;/b&gt;; I recommend looking at existing frameworks that are object-oriented such as &lt;a href=&quot;http://www.coldbox.org/&quot;&gt;ColdBox&lt;/a&gt;. Push yourself beyond &quot;Why should I build my CF app in object-oriented framework?&quot; and start asking the more difficult questions like &quot;What benefits would I reap from using a &lt;a href=&quot;http://en.wikipedia.org/wiki/Dependency_injection&quot;&gt;Dependency-Injection&lt;/a&gt; framework like &lt;a href=&quot;http://www.coldspringframework.org/&quot;&gt;ColdSpring&lt;/a&gt;?&quot; Never forget that &lt;b&gt;CFINCLUDE was added to facilitate the reuse of common display elements, and are not a mechanism to reflect the complexities of a business object&lt;/b&gt;--those needs are satisfied by a different tag, CFCOMPONENT, which rests inside a CFC. So, save CFINCLUDE for your reuse of display elements, and learn about how CFCs can speak to one another, decide on a methodology (like &lt;a href=&quot;http://en.wikipedia.org/wiki/Dependency_injection&quot;&gt;Dependency Injection&lt;/a&gt;), and implement it--and stay consistent!&lt;br /&gt;
&lt;br /&gt;
Do these things, and leave the merry-go-round to the kids.</content><link rel='replies' type='application/atom+xml' href='http://cfbestpractices.blogspot.com/feeds/1089968125306558702/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cfbestpractices.blogspot.com/2012/05/avoid-using-cfinclude-within-cfc.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7268570014893367744/posts/default/1089968125306558702'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7268570014893367744/posts/default/1089968125306558702'/><link rel='alternate' type='text/html' href='http://cfbestpractices.blogspot.com/2012/05/avoid-using-cfinclude-within-cfc.html' title='Avoid Using CFINCLUDE Within a CFC'/><author><name>Shawn Holmes</name><uri>http://www.blogger.com/profile/05110605868875966328</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7268570014893367744.post-4177580435941821912</id><published>2012-04-28T19:00:00.002-06:00</published><updated>2012-07-19T11:27:00.735-06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="best practicies"/><category scheme="http://www.blogger.com/atom/ns#" term="cache"/><category scheme="http://www.blogger.com/atom/ns#" term="cacheduntil"/><category scheme="http://www.blogger.com/atom/ns#" term="cachedwithin"/><category scheme="http://www.blogger.com/atom/ns#" term="caching"/><category scheme="http://www.blogger.com/atom/ns#" term="coldfusion"/><category scheme="http://www.blogger.com/atom/ns#" term="QoQ"/><category scheme="http://www.blogger.com/atom/ns#" term="query"/><category scheme="http://www.blogger.com/atom/ns#" term="query of queries"/><title type='text'>Treat &quot;dbtype=query&quot; and &quot;CachedWithin&quot; as Mutually Exclusive</title><content type='html'>My favorite programming quote is one stated by Terje Mathisen: &lt;i&gt;&quot;Almost all programming can be viewed as an exercise in caching&quot;&lt;/i&gt;. Truer words couldn&#39;t be spoken. There are so many facets of software development that can benefit from caching, yet developers continue to ignore those benefits, or simply do not understand what the value is. &lt;b&gt;CF developers are no exception&lt;/b&gt;. I&#39;ve been called in to support CF sites that &quot;seem to be performing poorly&quot; and, upon further inspection, demonstrate a total lack of understanding of caching. The single worst area this affects in CF development in within the context of queries. Wasteful SQL statements that do not change between users or between sections or sub-sections of a site, or which poll data that isn&#39;t expected to change at all for extended periods of time have no business being re-queried on&lt;i&gt; every single page request&lt;/i&gt;. What ends up happening is, in many cases, the CF developer is pinned down by their manager to &quot;just fix it&quot;, and they scramble to find anything online or in documentation that might help them. They get a gist on caching, and start applying attributes to CFQUERY tags like &quot;CachedWithin&quot;, or taking advantage of a CF idiom known as a &lt;b&gt;Query of Queries.&amp;nbsp;&lt;/b&gt;These are valid techniques on improving performance, and are commonly adopted by CF gurus that aim for Enterprise-level performance.&lt;br /&gt;
&lt;br /&gt;
However, even gurus still make mistakes...and this is one I see a lot.&lt;br /&gt;
&lt;br /&gt;
ColdFusion had a number of different ways that you can improve your SQL performance, as I stated in my intro. These different techniques&amp;nbsp;exist for different purposes; they exist in the language to solve different problems. Because of CF&#39;s flexibility, these options are often able to be paired with one another. However, just as the site says above...just because you &lt;i&gt;can&lt;/i&gt; do something in CF, doesn&#39;t mean you &lt;i&gt;should&lt;/i&gt;. The two options I&#39;m speaking about are the &#39;dbtype=query&#39; option being combined with the &#39;cachedwithin&#39; (and its red-headed stepbrother, &#39;cacheduntil&#39;) option. Let&#39;s first discuss what they are, so we have a better understanding of &lt;b&gt;why&lt;/b&gt; you should prefer to not combine them.&lt;br /&gt;
&lt;br /&gt;
When you execute a SQL statement against a database using a CFQUERY tag, you have the option of querying that entire recordset, as if it were a table in your physical database layer. This is what&#39;s known as a &lt;b&gt;Query of Queries&lt;/b&gt;. Let&#39;s look at this contrived example:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: coldfusion&quot;&gt; &amp;lt;cfquery name=&quot;getCategoryDepartments&quot; datasource=&quot;dsn&quot;&amp;gt;  
 SELECT d.DepartmentID, d.DepartmentName, c.CategoryName, c.CategoryID  
 FROM Departments d  
 INNER JOIN Categories c ON (d.DepartmentID = c.DepartmentID)  
 ORDER BY d.DepartmentID, c.CategoryID  
 &amp;lt;/cfquery&amp;gt;  
 &amp;lt;cfquery name=&quot;getCategories&quot; dbtype=&quot;query&quot;&amp;gt;  
 SELECT CategoryID, CategoryName  
 FROM getCategoryDepartments  
 WHERE DepartmentID = 13  
 &amp;lt;/cfquery&amp;gt;  
&lt;/pre&gt;
&lt;br /&gt;
Here, the first query &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;getCategoryDepartments&lt;/span&gt; executes against a physical database, joining two tables, and building an entire list of Departments, with a list of Categories that are assigned to each Department. Assuming it is a query that applies to our entire site, we may later on decide we only want to show that Categories in a given department. We can then perform a query against that first dataset, using the &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;dbtype=&#39;query&#39;&lt;/span&gt; attribute.&lt;br /&gt;
&lt;br /&gt;
When is this technique useful? For starters, if you&#39;re not an advanced SQL guru and complex statements elude you, this is a convenient way of gaining a subset of data without reworking your initial query. For performance reasons, using a &lt;b&gt;Query of Queries&lt;/b&gt; is fast, since it is executed in memory, and doesn&#39;t require a return trip to your physical database layer. Granted, you can write poor SQL in a Query of Queries and produce less-than-stellar performance, but in general, querying a dataset already in memory is going to gain you CPU cycles, and you&#39;ll see a performance improvement over time.&lt;br /&gt;
&lt;br /&gt;
The second technique for improving SQL performance in CF is by literally commanding ColdFusion to take a SQL statement, and store it in memory for querying purposes, preventing return trips to the server, either by specifying a fixed amount of time to hold on to it for, or by specifying a finite moment in the future to end its life. When you use this technique, you are &lt;b&gt;query caching&lt;/b&gt; in CF, and you do this via the &#39;cachedwithin&#39; or &#39;cacheduntil&#39; attribute. Let&#39;s update our original first query:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: coldfusion&quot;&gt; &amp;lt;cfquery name=&quot;getCategoryDepartments&quot; datasource=&quot;dsn&quot; cachedwithin=&quot;#CreateTimeSpan(1,0,0,0)#&quot;&amp;gt;  
 SELECT d.DepartmentID, d.DepartmentName, c.CategoryName, c.CategoryID  
 FROM Departments d  
 INNER JOIN Categories c ON (d.DepartmentID = c.DepartmentID)  
 ORDER BY d.DepartmentID, c.CategoryID  
 &amp;lt;/cfquery&amp;gt;  
&lt;/pre&gt;
&lt;br /&gt;
By adding the cachedwithin attribute, and passing it the result of a CreateTimeSpan() (set to 1 day), we explicitly force ColdFusion to store the results of that query in memory, so that if the SQL statement is ever called again within that time range, a return trip to the physical database layer is skipped. Instead, the results that were retrieved the first time the SQL statement executed, are re-issued out to the calling template. This technique is obviously very commonly used by CF developers to cache frequently-used, unchanging datasets. For example, if this query needed to be on every page of a CF site, caching it in memory would be an incredibly effective technique to improve the overall site&#39;s performance. Once queried and stored in memory, follow-up calls would also become extremely fast (since again, they&#39;re in memory), and would require no return trips to the server. Perfect for data that does not change (or changes rarely).&lt;br /&gt;
&lt;br /&gt;
So, if we have the cachedwithin option, and we also have a dbtype=query option, and both are in memory and provide huge improvements in speed and efficiency, why &lt;b&gt;wouldn&#39;t&lt;/b&gt; we want to combine them? For starters, there is the subject of memory usage. It&#39;s nice to assume that memory is infinite, especially in this day and age where developers can blissfully write software without every knowing how to allocate/deallocate a shred of memory in their app, happily prancing through their code, and allowing their JVM and Garbage Collector to clean up their mess.&lt;br /&gt;
&lt;br /&gt;
In reality, memory is not infinite. Even virtual memory needs to be manipulated by the JVM in order to provide the illusion that it is infinite, when in reality, it is performing a series of tasks behind the scenes to ensure the best performance possible back to the Java application (the CF server) while moving data stored in JVM heap around between the young, old, and permanent generation. The more movement that goes on, the closer you get to garbage collection, and although GC happens eventually, no matter what you do, you &lt;i&gt;can&lt;/i&gt; build software that is, by nature, causes your garbage collector to be less aggressive. Even in CF. And the first way you do that is &lt;i&gt;you do not treat memory like it is infinite&lt;/i&gt;.&lt;br /&gt;
&lt;br /&gt;
When you execute a query, and use cachedwithin attribute, the results of that query are stored in memory. That SQL is defined by unique attributes, and it is the combination of those attributes that communicate to ColdFusion whether or not this is a new query, or is one that can be pulled from the cache. The attributes include&lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt; the datasource name, the actual SQL statement itself, and the parameters passed to the SQL statement&lt;/b&gt;&lt;/span&gt;, so for example:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: coldfusion&quot;&gt; &amp;lt;cfquery name=&quot;getCategoriesByUser&quot; datasource=&quot;dsn&quot; cachedwithin=&quot;#CreateTimeSpan(1,0,0,0)#&quot;&amp;gt;  
 SELECT d.DepartmentID, d.DepartmentName, c.CategoryName, c.CategoryID  
 FROM Departments d  
 INNER JOIN Categories c ON (d.DepartmentID = c.DepartmentID)  
 WHERE c.UserID = #userID#  
 ORDER BY d.DepartmentID, c.CategoryID  
 &amp;lt;/cfquery&amp;gt;  
&lt;/pre&gt;
&lt;br /&gt;
This is a parameterized CFQUERY. So, how is it cached? Well, if on one page, the #userID# variable is &#39;23&#39;, well...that&#39;s one block of memory in the cache for that particular recordset. If, on another page, the #userID variable is now &#39;102&#39;, guess what? That&#39;s a second block of memory in the cache. The more parameterized a cached query is, the more memory in the JVM is going to get used up (until, of course, it hits the maximum number of cachable queries set in the ColdFusion Administrator).&lt;br /&gt;
&lt;br /&gt;
Now, let&#39;s assume you&#39;d query that cached query, to retrieve a list of results:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: coldfusion&quot;&gt; &amp;lt;cfquery name=&quot;getDeptCategoriesByUser&quot; dbtype=&quot;query&quot;&amp;gt;  
 SELECT *  
 FROM getCategoriesByUser  
 WHERE d.DepartmentID = 13  
 &amp;lt;/cfquery&amp;gt;  
&lt;/pre&gt;
&lt;br /&gt;
Now, you have a Query of Queries that&#39;s super fast, applies only to a specific Department page, and is within the context of a specific user (calling a cached query up above). You might think to yourself, &lt;i&gt;Hell, I crank this performance up even more by caching it, wait until the boss sees &lt;b&gt;this&lt;/b&gt;!&lt;/i&gt;&amp;nbsp;So, you add a cachedwithin attribute:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: coldfusion&quot;&gt; &amp;lt;cfquery name=&quot;getDeptCategoriesByUser&quot; dbtype=&quot;query&quot; cachedwithin=&quot;#CreateTimeSpan(0,0,30,0)#&quot;&amp;gt;  
 SELECT *  
 FROM getCategoriesByUser  
 WHERE d.DepartmentID = 13  
 &amp;lt;/cfquery&amp;gt;  
&lt;/pre&gt;
&lt;br /&gt;
Now you have a super fast cached Query of Queries, and its good for a solid 30 minutes. This puppy should &amp;nbsp;scream, and your site&#39;s performance should be top-notch, right?&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Wrong.&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
The Query of Queries execution is already an&lt;b&gt; in-memory query&lt;/b&gt;. That is, by nature, what its purpose was invented for: To take a recorset, store that set in memory, and work directly off of it, which we already know is fast. When you add a cachedwithin attribute to a Query of Queries, you are now partitioning off more memory in the JVM to store &lt;b&gt;another&lt;/b&gt; parameterization of the initial query. Is there a net gain in performance? No...in practically &lt;b&gt;no&lt;/b&gt; cases will there ever be a gain in performance; as stated earlier, a Query of Queries is already very fast, due to the execution being against a recordset already in memory. What will be gained, however, is an increased amount of memory being used, which only serves to push your Garbage Collector into motion faster.&lt;br /&gt;
&lt;br /&gt;
But you come back to me and argue, &quot;&lt;i&gt;Dude&lt;/i&gt;, memory is cheap. We&#39;re 64-bit now, remember? I&#39;ve got gigs and gigs of memory on our CF server to spare, this doesn&#39;t affect my statements--I get&lt;i&gt; superb&lt;/i&gt; performance by caching both my physical calls &lt;b&gt;and&lt;/b&gt; my Query of Query calls&quot;...&lt;br /&gt;
&lt;br /&gt;
So, I ask you to show me your code. You show me this:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: coldfusion;highlight: [3,8]&quot;&gt; &amp;lt;cfset getDepartments = getDepartmentsByUser( userID ) /&amp;gt;  
 &amp;lt;cfloop query=&quot;getDeparments&quot;&amp;gt;  
  &amp;lt;cfquery name=&quot;getDepartmentContent&quot; cachedwithin=&quot;#CreateTimeSpan(0,0,30,0)#&quot; dbtype=&quot;query&quot;&amp;gt;  
  SELECT Content  
  FROM getDepartments  
  WHERE DepartmentID = #DepartmentID#  
  &amp;lt;/cfquery&amp;gt;  
  &amp;lt;cfquery name=&quot;getDepartmentCategories&quot; cachedwithin=&quot;#CreateTimeSpan(0,0,30,0)#&quot; dbtype=&quot;query&quot;&amp;gt;  
   SELECT Categories  
   FROM getDepartments  
   WHERE DepartmentID = #DepartmentID#  
  &amp;lt;/cfquery&amp;gt;  
  ...  
 &amp;lt;/cfloop&amp;gt;  
&lt;/pre&gt;
&lt;br /&gt;
You go on to tell me that your method, getDepartments, is a function that calls a CFQUERY on another page, querying (cached, of course) for a set of all available departments belonging to a particular user, which you pass to the query as with #userID#.&lt;br /&gt;
&lt;br /&gt;
I ask you how the performance is, and you say tell me tremendous. You&#39;ve got memory to spare on your CF server and after cranking the maximum number of cached queries up to 500 in your CF Admin, you&#39;re running smooth as a baby&#39;s bottom.&lt;br /&gt;
&lt;br /&gt;
Except, there&#39;s one small problem:&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;Every different user in this example gets the same resultset.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
That&#39;s correct. It doesn&#39;t matter if you pass a userID of 13, 102, 5001, or 10002 to getDepartmentsByUser(). The end result for this page is always going to be the same for&lt;i&gt; every single user&lt;/i&gt;.&lt;br /&gt;
&lt;br /&gt;
Confused?&lt;br /&gt;
&lt;br /&gt;
Scroll back a bit to the highlighted text in the previous code snippet. This statement provides the information to ColdFusion which determines, under the hood, whether or not a query is cached, or requires a return trip to database layer.&lt;br /&gt;
&lt;br /&gt;
Figured it out yet?&lt;br /&gt;
&lt;br /&gt;
If you give up, it&#39;s ok. It&#39;s tricky, but you should be aware of it nonetheless: Aside from the datasource and the parameters, ColdFusion looks at the SQL statement itself for uniqueness in determining what is cached, and what requires a return trip to the database for.&lt;br /&gt;
&lt;br /&gt;
So, look at those statements again, and I&#39;ll highlight the problem code:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: coldfusion; highlight:[3]&quot;&gt;&amp;lt;cfquery name=&quot;getDepartmentContent&quot; cachedwithin=&quot;#CreateTimeSpan(0,0,30,0)#&quot; dbtype=&quot;query&quot;&amp;gt;  
  SELECT Content  
  FROM getDepartments
  WHERE DepartmentID = #DepartmentID#  
  &amp;lt;/cfquery&amp;gt;  
&lt;/pre&gt;
&lt;br /&gt;
You see, when the CFQUERY itself is abstracted away, out into its own method, CF has no way of determining if the parameters its wrapping method is passed apply to the Query of Queries call. All that it knows is that:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;It is a SQL statement,&lt;/li&gt;
&lt;li&gt;It is a Query of Queries, and therefore, does not need to access the db layer, and&lt;/li&gt;
&lt;li&gt;It&#39;s cached is based on the DepartmentID parameter, which is the only identifiable piece of SQL that changes.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
So, in creating the &#39;getDepartments&#39; variable, which itself is a query that can be re-queried, you have&amp;nbsp;inadvertently&amp;nbsp;side-stepped ColdFusion&#39;s own methods for determining cache invalidation, and forced it to use the same dataset for each and every user, because the cachedwithin attribute stored the first DepartmentID = 13 call, and continues to use that...&lt;i&gt;forever&lt;/i&gt;.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
And so, the final question remains:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Do &lt;i&gt;you&lt;/i&gt; want to tell your boss? Or shall I?&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cfbestpractices.blogspot.com/feeds/4177580435941821912/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cfbestpractices.blogspot.com/2012/04/treat-dbtypequery-and-cachedwithin-as.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7268570014893367744/posts/default/4177580435941821912'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7268570014893367744/posts/default/4177580435941821912'/><link rel='alternate' type='text/html' href='http://cfbestpractices.blogspot.com/2012/04/treat-dbtypequery-and-cachedwithin-as.html' title='Treat &quot;dbtype=query&quot; and &quot;CachedWithin&quot; as Mutually Exclusive'/><author><name>Shawn Holmes</name><uri>http://www.blogger.com/profile/05110605868875966328</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7268570014893367744.post-3925946161754155535</id><published>2012-04-14T21:12:00.002-06:00</published><updated>2012-07-19T10:38:13.308-06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="best practices"/><category scheme="http://www.blogger.com/atom/ns#" term="caching"/><category scheme="http://www.blogger.com/atom/ns#" term="coldfusion"/><category scheme="http://www.blogger.com/atom/ns#" term="performance"/><category scheme="http://www.blogger.com/atom/ns#" term="proxy"/><category scheme="http://www.blogger.com/atom/ns#" term="threads"/><category scheme="http://www.blogger.com/atom/ns#" term="timeout"/><category scheme="http://www.blogger.com/atom/ns#" term="webservice"/><title type='text'>Plan For Webservices To Go Offline</title><content type='html'>Ah, CF developers. We are quite a breed of programmer. We query one database table of employees and output them into a fancy HTML page, categorized by department, and suddenly we think we are an enterprise-level software architect. Unfortunately, it takes more than a couple of queries to a database and a couple of dynamically generated web pages before you can legitimately sell yourself as a senior software engineer.&lt;br /&gt;
&lt;br /&gt;
Let&#39;s walk through an exercise, shall we?&lt;br /&gt;
&lt;br /&gt;
I need you to consume a webservice. What the webservice does is irrelevant; just express to me that you have the capacity to connect to it and query it. For the sake of this exercise, let&#39;s assume the webservice is good ol&#39; fashioned SOAP-based, which as you know, makes things even easier for us CF developers.&lt;br /&gt;
&lt;br /&gt;
You say, &quot;Give me the endpoint for the WSDL, and the method you want me to call, and this&#39;ll be a cinch.&quot; I give you the endpoint and method name, and you write one line of code (as CF developers are famous for):&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: coldfusion&quot;&gt; &amp;lt;cfinvoke   
  webservice=&quot;http://path.to.the.service.endpoint.com/&quot;   
  method=&quot;theMethod&quot;   
  returnVariable=&quot;data&quot; /&amp;gt;  
&lt;/pre&gt;
&lt;br /&gt;
You then proceed to CFDUMP the data to find out what kind of data you have and, presto, you are everyone&#39;s hero.&lt;br /&gt;
&lt;br /&gt;
Then, I come back and tell you, &quot;It&#39;s not working. We just get errors, something about some Java host not reachable or something...&quot;&lt;br /&gt;
&lt;br /&gt;
So, you return to your code, and run some tests and realize...the webservice has gone offline. Either the host crashed, or perhaps was simply doing maintenance. Actually, when you dig further, you find out that the host has been up all along, but your company&#39;s network infrastructure underwent an upgrade and they temporarily lost their routing info out to the Internet, preventing your CF server from reaching the endpoint. It could be any number of issues, honestly...but the point is still the same: Your two lines of code didn&#39;t cut it. The endpoint became unreachable, and you didn&#39;t account for it.&lt;br /&gt;
&lt;br /&gt;
&quot;No sweat,&quot; you respond, &quot;I&#39;ll wrap a try/catch around this, and when the service goes offline again, we&#39;ll trap for it, and display an error page.&quot;&lt;br /&gt;
&lt;br /&gt;
So, you proceed to do that:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: coldfusion&quot;&gt; &amp;lt;cftry&amp;gt;  
  &amp;lt;cfset isDown = false /&amp;gt;  
  &amp;lt;cfinvoke webservice=&quot;http://path.to.the.service.endpoint.com/&quot;   
    method=&quot;theMethod&quot;   
    returnVariable=&quot;data&quot; /&amp;gt;  
  &amp;lt;cfcatch type=&quot;any&quot;&amp;gt;  
   &amp;lt;cfset isDown = true /&amp;gt;  
  &amp;lt;/cfcatch&amp;gt;  
 &amp;lt;/cftry&amp;gt;  
 &amp;lt;cfif isDown&amp;gt;  
  &amp;lt;!--- friendly error msg here ---&amp;gt;  
 &amp;lt;cfelse&amp;gt;  
  &amp;lt;!--- output the results of &#39;data&#39; here ---&amp;gt;  
 &amp;lt;/cfif&amp;gt;  
&lt;/pre&gt;
&lt;br /&gt;
Bam. Problem solved. You roll this fix into production and all is well with the world.&lt;br /&gt;
&lt;br /&gt;
Until I come back to you again and say, &quot;Yeah, I&#39;m not sure what you did there with that last fix but all of a sudden we&#39;re getting all kinds of reports that the performance of the site is just shit. It&#39;s really, really slow and nobody seems to know what&#39;s happening&quot;.&lt;br /&gt;
&lt;br /&gt;
So, you return a 2nd time, and hop onto the CF server. Sure enough, things are slow as hell. So you dig further and further, and eventually end up in the ColdFusion Administrator Server Monitor and you see...that all the threads are maxed, and not completing. &lt;i&gt;It is a nightmare&lt;/i&gt;. You have no idea what&#39;s going on and why all the threads are maxing out. Your first instinct is to blame the System Administrators, that they&#39;ve patched the box somehow and some new libraries are to blame. They look and can&#39;t see anything out of the ordinary. You look at your analytics--you haven&#39;t received a burst of users, it&#39;s basically serving the same amount of users it always has. You&#39;re pulling your hair out. Finally, with nowhere else to turn, you go back to the last code that was changed--the try/catch around your webservice. You remove the try/catch and discover something that was overlooked: There is no timeout specified on the call to the webservice. You dig further to try to determine what ColdFusion defaults to, in the context of a webservice call, when no timeout is specified, and you realize in your horror...&lt;br /&gt;
&lt;br /&gt;
...that ColdFusion&#39;s default is 0, which equates to &lt;i&gt;no timeout&lt;/i&gt;. It will, essentially, &lt;b&gt;wait forever&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
I&#39;ll quantify that last statement. More specifically, it will wait until the HTTP Request Timeout is hit. And you look at your CF Admin to quickly figure out what that timeout is. And you discover that someone in your organization, in their infinite wisdom, has set it to 500 seconds.&lt;br /&gt;
&lt;br /&gt;
So, by wrapping a try/catch around your webservice consumer code, you&#39;ve hidden yourself from a larger issue that manifested only in a production environment with a ton of users hammering your site, &lt;b&gt;in combination with&lt;/b&gt; the webservice itself going offline again.&lt;br /&gt;
&lt;br /&gt;
You went from being everyone&#39;s hero...to nobody&#39;s.&lt;br /&gt;
&lt;br /&gt;
--&lt;br /&gt;
&lt;br /&gt;
By now, you&#39;re either nodding your head up and down, going &quot;Yup, I totally remember when that happened to me,&quot; or you&#39;re starting to get nervous about some of your own code in production. What&#39;s truly sad about this story is that it is all too familiar for amateur developers that tackle a problem in a very compartmentalized environment. You are given a task, you aren&#39;t quite sure how to do it, so you look up some CF tags on the Adobe LiveDocs, figure out what you need to do, and implement, not understanding the broad picture of the entire system you are affecting with your code.&lt;br /&gt;
&lt;br /&gt;
Let&#39;s go at it from the user&#39;s perspective. Guy comes to your site at 9:31am, and logs in. Part of that login process calls your code to query a webservice. The data returns, and the person sees what they need to see. Then, they leave. Perhaps more users come and go, doing the same thing.&lt;br /&gt;
&lt;br /&gt;
Then, around 12:01pm, the webservice goes offline, and that first user returns to log in again, but can&#39;t. &lt;i&gt;Or can they?&lt;/i&gt; you think to yourself. &lt;i&gt;If he logged in a few hours earlier, perhaps I can do some caching on my end&lt;/i&gt;, you think. &lt;i&gt;This will allow the user to re-query their old data from 9:31am and keep on truckin&#39;, meanwhile, I&#39;ll be logging the service errors, and have it email the sysadmins and myself.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;Hm, but that&#39;s not really going to help the new users that try to log in &lt;b&gt;after&lt;/b&gt; the service has gone offline&lt;/i&gt;, you think. You ponder whether or not there is anything you can do for this use-case. You have an epiphany. &lt;i&gt;Well,&lt;/i&gt;&amp;nbsp;you think, &lt;i&gt;if previous queries succeeded, and are cached, I could at least have the column list available. Perhaps I can return a 0-record query and modify some additional logic on the front end to alert the user that their info isn&#39;t ready or not in the system yet, rather than some blanket error page that means nothing. That way, they know to try again later, and by that point, I&#39;ll be getting my administrative alerts that something&#39;s gone wrong.&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;br /&gt;
And just like that, you are &lt;b&gt;beginning to think about the big picture&lt;/b&gt;. And this is what will separate you from the hobbyists in the long run.&lt;br /&gt;
&lt;br /&gt;
Your reward for starting to think about problems in a different way: The remoteProxy.cfc component, which I have &lt;a href=&quot;https://github.com/Hanzo55/RemoteProxy&quot;&gt;published on GitHub&lt;/a&gt; for you to fork and play with. What is it? It is a wrapper for your webservice calls which does exactly what I&#39;ve described above: It provides a layer of protection for your webservice calls, caching the results of methods that are called when the webservice is up, and if the webservice goes offline or becomes unreachable, it will:&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;Start re-serving the cached queries, rather than dieing abruptly,&lt;/li&gt;
&lt;li&gt;Allow you an opportunity to log the errors, and/or email yourself/administrators,&lt;/li&gt;
&lt;li&gt;Use the column lists from the previously successful cached queries to adequately return a query of 0 results.&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
The remoteProxy.cfc component is &lt;b&gt;not&lt;/b&gt; an enterprise-solution...it&#39;s &lt;i&gt;just a start&lt;/i&gt;. It should provide you a basic framework to build on, but most importantly, it lets you think about solving a problem...not from the programmer&#39;s perspective...but from &lt;i&gt;everyone else&#39;s&lt;/i&gt;: your users, your system admins, etc. Do this, and you will begin to become everyone&#39;s hero again...including mine.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cfbestpractices.blogspot.com/feeds/3925946161754155535/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cfbestpractices.blogspot.com/2012/04/plan-for-webservices-to-go-offline.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7268570014893367744/posts/default/3925946161754155535'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7268570014893367744/posts/default/3925946161754155535'/><link rel='alternate' type='text/html' href='http://cfbestpractices.blogspot.com/2012/04/plan-for-webservices-to-go-offline.html' title='Plan For Webservices To Go Offline'/><author><name>Shawn Holmes</name><uri>http://www.blogger.com/profile/05110605868875966328</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7268570014893367744.post-2308794615877836767</id><published>2012-04-01T21:07:00.004-06:00</published><updated>2015-03-06T19:03:32.346-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="application.cfc"/><category scheme="http://www.blogger.com/atom/ns#" term="best practices"/><category scheme="http://www.blogger.com/atom/ns#" term="cflock"/><category scheme="http://www.blogger.com/atom/ns#" term="coldfusion"/><category scheme="http://www.blogger.com/atom/ns#" term="scope"/><category scheme="http://www.blogger.com/atom/ns#" term="shared memory"/><category scheme="http://www.blogger.com/atom/ns#" term="thread safe"/><category scheme="http://www.blogger.com/atom/ns#" term="variables"/><title type='text'>Know Where You Are Responsible for CFLOCK</title><content type='html'>&lt;b&gt;UPDATE:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
I&#39;m unable to recreate the tests I originally performed to confirm that concurrent incoming threads have the capacity to modify the REQUEST scope before exiting onRequestStart() from within Application.cfc. As a result, I&#39;ve struck out the information below that no longer applies (and, at the very least, cannot be proven in unit tests).&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
---&lt;/div&gt;
&lt;br /&gt;
&lt;i&gt;Original Post&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;CFLOCK&amp;gt; tag seems to continue confuse and bewilder ColdFusion developers. And for good reason. It&#39;s rarely covered in any great detail, and when an attempt is made, people seem to have different explanations of when pieces of your code should be wrapped in a &amp;lt;CFLOCK&amp;gt;. For a time, it seemed like the ColdFusion community couldn&#39;t agree on whether the SESSION scope should be locked or not. Some argued for, others argued against, and even the company responsible (at its time) wasn&#39;t able to give a straight answer--nor could documentation or instruction manuals! The &amp;lt;CFLOCK&amp;gt; tag seemed to remain shrouded in mystery like some sort of magical dark art. The truth is: it is not a mystery nor a magical art. There are definitely hard and fast rules to using &amp;lt;CFLOCK&amp;gt;, and in order to explain it thoroughly and clearly, we must first set the record straight on another far more mysterious and dark topic:&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;In a single ColdFusion request, what is single-threaded, and what is not?&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
First, let&#39;s make sure we understand what a thread is. A thread of execution is the smallest unit of processing scheduled by an operating system. You ask the computer to perform a task, and that task is executed through the processing of sub-tasks, all of which may share access to the same code and memory. Those sub-tasks are the threads. Some threads are known to execute in parallel, independently of one another, within a single process. This is the concept of &lt;i&gt;multithreading&lt;/i&gt;. Although this gross oversimplification of the threading model leaves many finer details out, it hopefully conveys the key point you need to take away:&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;When in the context of a threading model, execution of logic &lt;b&gt;may not necessarily be linear&lt;/b&gt;--but in fact, may provide an opportunity for shared memory to be both written-to and read-from at the same moment in time.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
If you have been a ColdFusion developer for any amount of time, you should probably already know what the shared memory scopes are--that is, the scopes in which multiple users and/or processes may read-to and write-from, namely scopes like:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;APPLICATION&lt;/li&gt;
&lt;li&gt;SESSION&lt;/li&gt;
&lt;li&gt;SERVER&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
But in fact, there are other scopes that you wouldn&#39;t normally consider shared, but in fact, are..but for a brief moment (enough to cause damage, if not careful), such as:&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;REQUEST&lt;/li&gt;
&lt;li&gt;VARIABLES (within a CFC)&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
So, consider the following code:&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: coldfusion&quot;&gt;&amp;lt;cfset user = getUserFromSession( userID ) /&amp;gt;

&amp;lt;cfif user.isLoggedIn&amp;gt;

&amp;nbsp; &amp;lt;cfset SESSION.isAuthenticated = true /&amp;gt;

&amp;lt;/cfif&amp;gt;
&lt;/pre&gt;
&lt;br /&gt;
Can you guarantee to me that the user variable in the 2nd line is the same variable that was returned from the function in the 1st line? To the CF hobbyist, the answer is probably &quot;yes&quot;.&lt;br /&gt;
&lt;br /&gt;
That answer would be &lt;i&gt;wrong&lt;/i&gt;.&lt;br /&gt;
&lt;br /&gt;
To understand why, let&#39;s have a look within the guts of the Application.cfc, the component famous for providing ColdFusion applications with the necessary framework to provide application configuration variables, state timeout and maintenance, and per-request rules that must be run each page click. The Application.cfc, as you may know, contains a number of methods, such as:&lt;br /&gt;
&lt;br /&gt;
onApplicationStart()&lt;br /&gt;
onSessionStart()&lt;br /&gt;
onRequestStart()&lt;br /&gt;
&lt;br /&gt;
These three methods in particular are well-known to be responsible for running once when the application first starts up (onApplicationStart), when a new user session is created (onSessionStart), and every single page request (onRequestStart). Now, since onApplicationStart is known to be the best place to create and manage the APPLICATION scope, any time you set values that are stored in APPLICATION scope from within onApplicationStart, the access to that scope is made safe for you--it is &lt;i&gt;single-threaded&lt;/i&gt;. The same holds true for values you write to the SESSION scope from within onSessionStart; ColdFusion &lt;i&gt;single-threads&lt;/i&gt; calls inside that method, and thus, writes are thread-safe, and do not require locking. This is undoubtedly where official statements from ColdFusion&#39;s production company (at the time) said that you no longer needed to lock your access to these variables came from; the locking was done for you, &lt;i&gt;implicitly&lt;/i&gt;, via those methods&#39; single-threaded access, relieving you of the duty.&lt;br /&gt;
&lt;br /&gt;
However, unlike onApplicationStart and onSessionStart, onRequestStart works quite differently. Access to this method&lt;b&gt; is not single-threaded&lt;/b&gt;, and for good reason: this method is called the most by your application--on every single page click! Single-threading access to this method would be a huge bottleneck, and would disallow your app to scale. Therefore, any variables accessed, particularly those whose scopes provide &lt;i&gt;shared-access&lt;/i&gt; must be handled very carefully.&lt;br /&gt;
&lt;br /&gt;
Have you ever built a CF app, or had to support someone&#39;s CF app, that implemented some sort of &quot;?reload=true&quot; handler? You know, some semblance of URL variable that, when appended to the URL, causes the application to flush its configuration and recall onApplicationStart? If so, it would most likely have to have been placed within onRequestStart, something to the effect of:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: coldfusion&quot;&gt;&amp;lt;cfif IsDefined(&quot;URL.reload&quot;)&amp;gt;

&amp;nbsp; &amp;lt;cfset onApplicationStart()&amp;gt;

&amp;lt;/cfif&amp;gt;
&lt;/pre&gt;
&lt;br /&gt;
Ignoring my &lt;a href=&quot;http://cfbestpractices.blogspot.com/2012/03/prefer-len-and-structkeyexists-over.html&quot;&gt;previous pet peeve&lt;/a&gt;, what is wrong with these three lines of code? What&#39;s wrong is that the onApplicationStart() handler&lt;i&gt; is being called from within&lt;/i&gt; onRequestStart. And although onApplicationStart single-threads access, what do we know does not? &lt;b&gt;onRequestStart&lt;/b&gt;. So, the safety of onApplicationStart&#39;s single-threadedness is lost, and any updates to the APPLICATION scope could potentially be overwritten by other threads.&lt;br /&gt;
&lt;br /&gt;
&lt;strike&gt;What about this code...would &lt;b&gt;it&lt;/b&gt; be thread-safe in onRequestStart?&lt;/strike&gt;&lt;br /&gt;
&lt;strike&gt;&lt;br /&gt;&lt;/strike&gt;
&lt;pre class=&quot;brush: coldfusion&quot;&gt;&lt;strike&gt;&amp;lt;cfset user = getUserFromSession( userID ) /&amp;gt;

&amp;lt;cfif user.isLoggedIn&amp;gt;

&amp;nbsp; &amp;lt;cfset SESSION.isAuthenticated = true /&amp;gt;

&amp;nbsp; &amp;lt;!--- my hidden marker ---&amp;gt;

&amp;lt;/cfif&amp;gt;
&lt;/strike&gt;&lt;/pre&gt;
&lt;strike&gt;&lt;br /&gt;&lt;/strike&gt;
&lt;strike&gt;&lt;i&gt;Absolutely not&lt;/i&gt;. Do you see that CFML comment between lines 3 and 4? Pretend that &lt;b&gt;another complete request started and finished&lt;/b&gt;, just before line 4 is reached, and in that request, an invalid user is passed in, which resulted in user.isLoggedIn evaluating to FALSE...&lt;/strike&gt;&lt;br /&gt;
&lt;strike&gt;&lt;br /&gt;&lt;/strike&gt;
&lt;strike&gt;Suddenly, the &amp;lt;CFLOCK&amp;gt; issue isn&#39;t so black and white, is it?&lt;/strike&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strike&gt;What complicates this problem is that we are inside a method (onRequestStart) that itself is within the confines of a CFC (Application.cfc), and what do we know about variables and scope in a CFC?&lt;/strike&gt;&lt;br /&gt;
&lt;strike&gt;&lt;br /&gt;&lt;/strike&gt;
&lt;strike&gt;Access to unscoped variables automatically pours them into the VARIABLES scope, and in the context of a CFC, the VARIABLES scope persists for the life of that CFC.&lt;/strike&gt;&lt;br /&gt;
&lt;strike&gt;&lt;br /&gt;&lt;/strike&gt;
&lt;strike&gt;Even if that CFC is not cached (which Application.cfc is &lt;b&gt;not&lt;/b&gt;), the VARIABLES scope still persists through the complete HTTP request, and thanks to the fact that onRequestStart is &lt;b&gt;not&lt;/b&gt; single-threaded, this means that the VARIABLES scope is now also a scope that is potentially not thread-safe, even if that is but for a short moment.&lt;/strike&gt;&lt;br /&gt;
&lt;br /&gt;
Take all these factors into consideration, and you can now see how multiple threads all processing their way through onRequestStart may have the potential to step on each others toes...and thus, &amp;lt;CFLOCK&amp;gt; calls are thrust back upon your shoulders of responsibility. Any access to any shared variables, whether they be APPLICATION, SESSION, REQUEST&lt;strike&gt;, or even VARIABLES (within Application.cfc and onRequestStart)...&lt;/strike&gt;must be locked in order to guarantee thread-safety. Otherwise, you&#39;ll end up with valid users being logged out, while invalid users are granted access to stuff that may or may not make your employer re-evaluate your status as the &quot;local CF expert&quot;.&lt;br /&gt;
&lt;br /&gt;
So, in a nutshell:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;APPLICATION reads and writes are safe within onApplicationStart and do not require locks.&lt;/li&gt;
&lt;li&gt;SESSION reads and writes are safe within onSessionStart and also do not require locks.&lt;/li&gt;
&lt;li&gt;If you manually call onAppliationStart() or onSessionStart() from within onRequestStart(), all bets are off; they can no longer be considered thread-safe, and you must employ some form of the &amp;lt;CFLOCK&amp;gt; tag around them.&lt;/li&gt;
&lt;li&gt;&lt;strike&gt;If you do not locally declare variables within onRequestStart, they become a part of the Application.cfc&#39;s VARIABLES scope, which persists for the life of the request, which (if multiple threads come in at once) offer the potential for thread-safety to be invalidated. Solve by locally declaring any temporary variables you work with inside onRequestStart.&lt;/strike&gt;&lt;/li&gt;
&lt;li&gt;&lt;strike&gt;Any updates you make to the REQUEST scope before you leave the safety of onRequestStart should be also wrapped in &amp;lt;CFLOCK&amp;gt;. Once you leave onRequestStart, the REQUEST scope is unique (it is its own copy) to that particular HTTP request, and additional writes to it will not affect other users.&lt;/strike&gt;&lt;/li&gt;
&lt;li&gt;If you employ the technique of copying your APPLICATION or SESSION variables into REQUEST, and only access them via REQUEST once you leave the comfort of Application.cfc, you should safely be free to never worry about wrapping additional code in &amp;lt;CFLOCK&amp;gt;, other than any follow-up writes to APPLICATION and SESSION, which should be chosen judiciously.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://cfbestpractices.blogspot.com/feeds/2308794615877836767/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cfbestpractices.blogspot.com/2012/04/understand-where-you-are-responsible.html#comment-form' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7268570014893367744/posts/default/2308794615877836767'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7268570014893367744/posts/default/2308794615877836767'/><link rel='alternate' type='text/html' href='http://cfbestpractices.blogspot.com/2012/04/understand-where-you-are-responsible.html' title='Know Where You Are Responsible for CFLOCK'/><author><name>Shawn Holmes</name><uri>http://www.blogger.com/profile/05110605868875966328</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7268570014893367744.post-6036541162228200894</id><published>2012-03-17T14:39:00.001-06:00</published><updated>2012-07-19T10:52:53.733-06:00</updated><title type='text'>Understand What Generates Whitespace</title><content type='html'>Time for an experiment.&lt;br /&gt;
&lt;br /&gt;
Go to the last website you built on ColdFusion, and when the homepage loads up, view its source.&lt;br /&gt;
&lt;br /&gt;
If you&#39;ve never built a website on ColdFusion, use this URL as an example:&amp;nbsp;&lt;a href=&quot;http://hill.dpsk12.org/&quot;&gt;http://hill.dpsk12.org/&lt;/a&gt;&amp;nbsp;(a random middle school in Denver that sits on ColdFusion).&lt;br /&gt;
&lt;br /&gt;
If if the source looks similar to the example site above, you may see something like this:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-5zOALyApeB0aYCEmXMN5e6qF9l4tIA8WlW8tkn9Q5t2VKqromK14L5D800oiQgvB465-AYQ8ytHRitkb2mxL-GPER4ldiSV9yeskQUswnRqZwivmFqph-Eg0e73fnUN1iVaZdlsIhYiU/s1600/whitespace.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;245&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-5zOALyApeB0aYCEmXMN5e6qF9l4tIA8WlW8tkn9Q5t2VKqromK14L5D800oiQgvB465-AYQ8ytHRitkb2mxL-GPER4ldiSV9yeskQUswnRqZwivmFqph-Eg0e73fnUN1iVaZdlsIhYiU/s320/whitespace.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
Wow. What&#39;s with those first 14 lines of NOTHING? If you don&#39;t have a bunch of whitespace at the top of your source: Congratulations, you win at the internetz; this post is no less relevant to you...so read on.&lt;br /&gt;
&lt;br /&gt;
When the many trolls of the internet band together and go off on a &quot;&lt;a href=&quot;http://blog.ammar.me/php/why-coldfusion-sucks-329&quot;&gt;Why CF Sucks&lt;/a&gt;&quot; tirade, this is one of the many cards they like to pull; the blatant and seemingly unnecessary amount of whitespace that is generated by the application server. They even go so far as to state that &quot;CF developers needs to have their hands held by providing an override switch in the administrator just to automatically suppress whitespace for them.&quot;&lt;br /&gt;
&lt;br /&gt;
We don&#39;t &lt;i&gt;need&lt;/i&gt; that switch...it&#39;s there for &lt;b&gt;convenience&lt;/b&gt;. Our infamous four-letter word.&lt;br /&gt;
&lt;br /&gt;
What CF developers &lt;i&gt;do&lt;/i&gt; need, however, is an education on what generates whitespace, and the correct way to handle it.&lt;br /&gt;
&lt;br /&gt;
CF templates that perform logic that the application server needs to evaluate cause the app server to &lt;i&gt;replace the bytes used by said tags with equivalent whitespace&lt;/i&gt; in the final html document sent to the browser So, a few lines like:

&lt;pre class=&quot;brush: coldfusion&quot;&gt;
&amp;lt;cfset a = 4 /&amp;gt;
&amp;lt;cfif a gt 0&amp;gt;
  &amp;lt;cfset b = 1 /&amp;gt;
&amp;lt;cfelse&amp;gt;
  &amp;lt;cfset b = 2 /&amp;gt;
&amp;lt;/cfif&amp;gt;

The time is &amp;lt;cfoutput&amp;gt;#Now()#&amp;lt;/cfoutput&amp;gt;!
&lt;/pre&gt;
&lt;br /&gt;
literally becomes:&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre id=&quot;line1&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;
 
&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;The time is {ts &#39;2012-03-17 13:27:08&#39;}!&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
All that whitespace is transmitted with each non-cached HTTP request. As much as you&#39;d like to think it&#39;s non-destructive, it&#39;s ultimately wasteful and impacts performance. A whitespace character still needs to be transmitted across the wire (unless it&#39;s gzipped), and even if it &lt;i&gt;is&lt;/i&gt; gzipped, once uncompressed by the browser, produces an amateurish bloated result (at its best) and can completely cripple the output expected by the client (at its worst).&lt;br /&gt;
&lt;br /&gt;
If you&#39;d like an example of a crippled output, just ask any CF developer whose struggled with trying to produce a valid XML data stream...that was ultimately padded with whitespace. This can lead to the &quot;Content is not allowed in prolog&quot; parse errors, among others.&lt;br /&gt;
&lt;br /&gt;
Another reason that CF generates whitespace is due to the way in which functions and components have a default attribute of &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&quot;output=true&quot;&lt;/span&gt;, unless you specify otherwise. Calls to methods within CFCs and UDFs you in-line in your CF documents will all suffer the same consequence as the example above--the CF logic will be replaced byte-for-byte with whitespace upon interpretation.&lt;br /&gt;
&lt;br /&gt;
You now have my permission to no longer generate wasteful whitespace. Here are the rules:&lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;Stop relying on the &quot;Enable Whitespace Management&quot; option in the CF Admin. &lt;i&gt;Disable it&lt;/i&gt;. In the war against &quot;CF performs poorly&quot; activists, this is one tiny way you can improve the overall performance of the app server. It doesn&#39;t magically solve all whitespace problems anyway. &lt;i&gt;Turn it off. Leave it off&lt;/i&gt;.&lt;/li&gt;
&lt;li&gt;Move your logic to the top of the template, and wrap it in &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;lt;cfsilent&amp;gt;&lt;/span&gt;. If you start employing this technique, you will be on the road to separating business logic from display logic--which is a best practice that transcends ColdFusion. Applied to our example above:&lt;br /&gt;
&lt;pre class=&quot;brush: coldfusion&quot;&gt;
&amp;lt;cfsilent&amp;gt;
&amp;nbsp; &amp;lt;cfset a = 4 /&amp;gt;
&amp;nbsp; &amp;lt;cfif a gt 0&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;lt;cfset b = 1 /&amp;gt;
&amp;nbsp; &amp;lt;cfelse&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;lt;cfset b = 2 /&amp;gt;
&amp;nbsp; &amp;lt;/cfif&amp;gt;
&amp;lt;/cfsilent&amp;gt;The time is &amp;lt;cfoutput&amp;gt;#Now()#&amp;lt;/cfoutput&amp;gt;!&lt;/pre&gt;&lt;br /&gt;produces:&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;The time is {ts &#39;2012-03-17 13:27:08&#39;}!&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Amazing.&lt;br /&gt;&lt;/li&gt;
&lt;li&gt;Building off of #2, if you have queries or loops that produce dynamic results, keep them up in your &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;lt;cfsilent&amp;gt;&lt;/span&gt; block, and save their results using &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;lt;cfsavecontent&amp;gt;&lt;/span&gt;, so that you only require single&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt; &amp;lt;cfoutput&amp;gt;&lt;/span&gt; lines in your code.&lt;/li&gt;
&lt;li&gt;Building off of #3, if you perform &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;lt;cfsavecontent&amp;gt;&lt;/span&gt; calls in your &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;lt;cfsilent&amp;gt;&lt;/span&gt; block, use &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;lt;cfsetting enablecfoutputonly=&quot;true&quot;&amp;gt;&lt;/span&gt; in your &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;lt;cfsilent&amp;gt;&lt;/span&gt; block, and only place &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;lt;cfoutput&amp;gt;&lt;/span&gt; around blocks of code that are generating a result you&#39;ll display later.&lt;br /&gt;&lt;br /&gt;Here&#39;s an example of #3 and #4 together:&lt;br /&gt;
&lt;pre class=&quot;brush: coldfusion&quot;&gt;
&amp;lt;cfsilent&amp;gt;
&amp;nbsp; &amp;lt;cfsetting enablecfoutputonly=&quot;true&quot;&amp;gt;
&amp;nbsp; &amp;lt;cfsavecontent variable=&quot;tableContent&quot;&amp;gt;
&amp;nbsp; &amp;lt;cfloop from=&quot;1&quot; to=&quot;4&quot; index=&quot;i&quot;&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;lt;cfoutput&amp;gt;&amp;lt;tr bgcolor=&quot;#Iif( i MOD 2, De( &#39;white&#39; ), De( &#39;gray&#39; ) )#&quot;&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;lt;td&amp;gt;row #i#&amp;lt;/td&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;lt;td&amp;gt;&amp;amp;nbsp;&amp;lt;/td&amp;gt;
&amp;nbsp; &amp;lt;/tr&amp;gt;&amp;lt;/cfoutput&amp;gt;
&amp;nbsp; &amp;lt;/cfloop&amp;gt;
 &amp;lt;/cfsavecontent&amp;gt;
&amp;lt;/cfsilent&amp;gt;&amp;lt;cfoutput&amp;gt;&amp;lt;table&amp;gt;#tableContent#&amp;lt;/table&amp;gt;&amp;lt;/cfoutput&amp;gt;&lt;/pre&gt;
&lt;br /&gt;Like a BOSS.&lt;br /&gt;&lt;/li&gt;
&lt;li&gt;Keep all in-line &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;lt;cffunction&amp;gt;&lt;/span&gt;s in your &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;lt;cfsilent&amp;gt;&lt;/span&gt; block as well, and &lt;b&gt;always specify&lt;/b&gt; &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;output=&quot;false&quot;&lt;/span&gt;. The only time you ever specify &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;output=&quot;true&quot;&lt;/span&gt; is if the function&lt;i&gt; actually produces HTML output to the screen&lt;/i&gt;. To solve this problem,&lt;b&gt; prefer returning computed HTML as a string&lt;/b&gt; from your functions, rather that blatantly outputting the browser.&lt;/li&gt;
&lt;li&gt;If you use an &lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;b&gt;Application.cfm&lt;/b&gt;&lt;/span&gt;, always put a &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;lt;cfsetting enablecfoutputonly=&quot;true&quot;&amp;gt;&lt;/span&gt; at the start of the document, and a subsequent &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;lt;cfsetting enablecfoutputonly=&quot;false&quot;&amp;gt;&lt;/span&gt; at the end. Never globally suppress output (which would manifest if you left the second call out); prefer to handle it template-by-template, which is a good reason to adopt a framework that has a common system for enable/disabling output, and subsequently, whitespace.&lt;/li&gt;
&lt;li&gt;If you use an &lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;b&gt;Application.cfc&lt;/b&gt;&lt;/span&gt;, ensure that all of the built-in methods you override (ie. onApplicationStart, onRequestStart, onSessionStart, etc.) also are set to &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;output=&quot;false&quot;&lt;/span&gt;. They do not require the output to be set to true to produce HTML content to the browser, a common misconception.&lt;/li&gt;
&lt;li&gt;Finally, review all of your CFCs, and make sure they are declared with &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;output=&quot;false&quot;&lt;/span&gt;:&lt;br /&gt;&lt;br /&gt;
&lt;pre class=&quot;brush: coldfusion&quot;&gt;&amp;lt;cfcomponent output=&quot;false&quot;&amp;gt;&lt;/pre&gt;
&lt;br /&gt;Even if all the methods in the CFC are set to &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;output=&quot;false&quot;&lt;/span&gt;, if the CFC itself is set to &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;output=&quot;true&quot;&lt;/span&gt;, guess what...you&#39;re still going to generate unnecessary whitespace.&lt;/li&gt;
&lt;li&gt;BONUS: If all else fails, and you still cannot trim the whitespace from the start of the document (common in the aforementioned XML generation), use a &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;lt;cfcontent&amp;gt;&lt;/span&gt; call prior to the XML output:&lt;br /&gt;
&lt;pre class=&quot;brush: coldfusion&quot;&gt;&amp;lt;cfcontent type=&quot;text/xml; charset=UTF-8&quot; reset=&quot;true&quot; /&amp;gt;&amp;lt;cfoutput&amp;gt;#myXML#&amp;lt;/cfoutput&amp;gt;&lt;/pre&gt;
&lt;br /&gt;This also works for HTML:&lt;br /&gt;
&lt;pre class=&quot;brush: coldfusion&quot;&gt;&amp;lt;cfcontent type=&quot;text/html; charset=UTF-8&quot; reset=&quot;true&quot; /&amp;gt;&amp;lt;html&amp;gt;&amp;lt;head&amp;gt;...so on&lt;/pre&gt;
&lt;br /&gt;The &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&quot;reset=true&quot;&lt;/span&gt; attribute tells CF to discard any generated output that comes prior to the tag.&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
Do these things in your CF code. Be actively aware of what generates whitespace, and arm yourself with the tools and knowledge to handle it correctly, without having to resort to a CF Admin configuration setting to do the work for you.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cfbestpractices.blogspot.com/feeds/6036541162228200894/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cfbestpractices.blogspot.com/2012/03/understand-what-generates-whitespace.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7268570014893367744/posts/default/6036541162228200894'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7268570014893367744/posts/default/6036541162228200894'/><link rel='alternate' type='text/html' href='http://cfbestpractices.blogspot.com/2012/03/understand-what-generates-whitespace.html' title='Understand What Generates Whitespace'/><author><name>Shawn Holmes</name><uri>http://www.blogger.com/profile/05110605868875966328</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-5zOALyApeB0aYCEmXMN5e6qF9l4tIA8WlW8tkn9Q5t2VKqromK14L5D800oiQgvB465-AYQ8ytHRitkb2mxL-GPER4ldiSV9yeskQUswnRqZwivmFqph-Eg0e73fnUN1iVaZdlsIhYiU/s72-c/whitespace.png" height="72" width="72"/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7268570014893367744.post-8017521218309154986</id><published>2012-03-10T13:09:00.002-07:00</published><updated>2012-04-10T14:25:44.264-06:00</updated><title type='text'>Understand the use of the # symbol</title><content type='html'>Arguably, the very first thing a person learns when they pick up ColdFusion (as it is with many languages) is how to set variables, and output them.&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
You set them like this:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;lt;cfset name = &quot;Shawn&quot; /&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;lt;cfset age = 37 /&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
You output them like this:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;lt;cfoutput&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;My name is: #name#, and I am #age# years old.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;lt;/cfoutput&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Result:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;My name is: Shawn, and I am: 37 years old.&lt;/span&gt; &amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
What happens if we remove the &lt;b&gt;age&lt;/b&gt; variable, and replace it with an actual statement?&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;lt;cfoutput&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;My name is: #name#, and I am #30+7# years old.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;lt;/cfoutput&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Same result:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;My name is: Shawn, and I am: 37 years old.&lt;/span&gt; &amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
This very simple example should paint a very clear picture of what the # symbol is used for in ColdFusion: it is a marker that identifies the start and end of a token or statement to be &lt;i&gt;dynamically evaluated&lt;/i&gt; by CF.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
This means that any time you put a # symbol into code...anywhere...you are forcing ColdFusion to evaluate the statement before carrying on. This is perfectly fine for the display of variables whose results need to be evaluated and displayed. But for 99.9% of all of the remaining functions in ColdFusion, it is unnecessary overhead that forces ColdFusion to make an additional evaluation pass where one simply isn&#39;t needed.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Remember, when you are writing statements in CFML, using tags like &amp;lt;cfset&amp;gt;, &amp;lt;cfif&amp;gt;, etc., ColdFusion knows that are you working with variables and statements, so there is no need to force it into evaluating those statements on-the-fly; the evaluation of variables and logic is baked into those statements already, so if you were to move that inline code of &quot;30+7&quot; into a CF statement, you are actually telling it to do this:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;lt;cfset #age# = #Evaluate(&quot;30+7&quot;)# /&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
In other words, you are telling ColdFusion: Take the string &quot;30+7&quot;, convert it into a statement that can be evaluated (numbers), evaluate that result, then take the &quot;age&quot; variable, figure out what it&#39;s current contents are, &lt;i&gt;then throw them away&lt;/i&gt;, then take the computed results and assign them to the &quot;age&quot; variable.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
As opposed to:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;lt;cfset age = 30 + 7 /&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Which tells ColdFusion: Take the result of 30+7, and assign that value to the &quot;age&quot; variable.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Even without all of the advanced knowledge that Comp Sci. programmers may possess with their intimate knowledge of Java, the JVM, variable assignments, immutable objects, so on, and so forth...but just by reading the plain English translation of the above two statements...&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
...shouldn&#39;t it be obvious which is the more efficient and less wasteful approach?&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Conclusion: Unless you are explicitly using a &amp;lt;cfoutput&amp;gt; on a display template, don&#39;t use a # symbol. Try everything in your power to NOT use a # symbol, ever, unless it is absolutely necessary (and in 99.9% of the time...&lt;i&gt;it isn&#39;t&lt;/i&gt;).&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cfbestpractices.blogspot.com/feeds/8017521218309154986/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cfbestpractices.blogspot.com/2012/03/understand-use-of-symbol.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7268570014893367744/posts/default/8017521218309154986'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7268570014893367744/posts/default/8017521218309154986'/><link rel='alternate' type='text/html' href='http://cfbestpractices.blogspot.com/2012/03/understand-use-of-symbol.html' title='Understand the use of the # symbol'/><author><name>Shawn Holmes</name><uri>http://www.blogger.com/profile/05110605868875966328</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7268570014893367744.post-255188679006770896</id><published>2012-03-05T21:53:00.001-07:00</published><updated>2012-03-07T08:36:31.377-07:00</updated><title type='text'>Don&#39;t Rely on Val() in Logic Statements</title><content type='html'>ColdFusion is a dynamically-typed language. Variable types are flexible enough to change from assignment to assignment. Most long-term CF programmers may even come up to you and state things like &quot;Ah, yes, ColdFusion...it is a typeless language!&quot;&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;i&gt;Being dynamically-typed is not the same as being &quot;typeless&quot;.&lt;/i&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
(Never mind the fact that ColdFusion is an &lt;b&gt;application server&lt;/b&gt;, not a language; &lt;i&gt;CFML&lt;/i&gt; is the language).&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Consider:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;cfset x = 1 /&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
What is the type of variable x?&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;cfoutput&amp;gt;&lt;/span&gt;#x.GetClass()#&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;/cfoutput&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
produces:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
class java.lang.String&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Sad, but true. Our 1 is interpreted as a string. Adding the following code below it:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;cfset y = Val(x) /&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;cfoutput&amp;gt;&lt;/span&gt;#y.GetClass()#&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;/cfoutput&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
produces:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
java.lang.Double&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Aha! So, now we have the type we have come to expected! A true numerical value, a double. What about other types, say...a boolean?&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;cfset x = true /&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;cfoutput&amp;gt;&lt;/span&gt;#x.GetClass()#&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;/cfoutput&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
reveals:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
class java.lang.String
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Not even a simple boolean is safe! It&#39;s just another string, like &quot;Fred&quot;. And we certainly cannot evaluate &quot;Fred&quot; to true or false:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;cfset x = &quot;Fred&quot; /&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;cfif (x)&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
do wonderful things&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;/cfif&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
produces:&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: verdana; font-size: 13pt; line-height: 15pt;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: verdana; font-size: 13pt; line-height: 15pt;&quot;&gt;cannot convert the value &quot;Fred&quot; to a boolean &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
so, why then, if we know that &lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;cfset x = true /&amp;gt;&lt;/span&gt; is simply x being assigned a string &quot;true&quot; under-the-hood, does &lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;cfif (x)&amp;gt;&lt;/span&gt; evaluate correctly?&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;i&gt;Because ColdFusion maintains mechanisms internally to attempt to implicitly cast values to a needed type in order to process logical statements.&lt;/i&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
ColdFusion knows you gave it a string when you made a statement like &lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;cfset x = true /&amp;gt;&lt;/span&gt;, but it also has rules it understands which guide it into casting a string &quot;true&quot; into a boolean literal TRUE.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Now, compare that implicit casting mechanism to the definition of the Val() function:&lt;/div&gt;
&lt;div&gt;
&lt;h1 style=&quot;background-color: white; color: #666666; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 17px;&quot;&gt;



Val&lt;/h1&gt;
&lt;h4 class=&quot;sectiontitle&quot; style=&quot;background-color: white; color: #666666; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; line-height: 17px;&quot;&gt;



Description&lt;/h4&gt;
&lt;div style=&quot;background-color: white; color: #666666; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; line-height: 17px;&quot;&gt;
Converts numeric characters that occur at the beginning of a string to a number.&lt;/div&gt;
&lt;div class=&quot;section&quot; id=&quot;WSc3ff6d0ea77859461172e0811cbec22c24-6e64__WSc3ff6d0ea77859461172e0811cbec20301-7f9a&quot; style=&quot;background-color: white; color: #666666; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; line-height: 17px;&quot;&gt;
&lt;a href=&quot;http://www.blogger.com/blogger.g?blogID=7268570014893367744&quot; name=&quot;WSc3ff6d0ea77859461172e0811cbec22c24-6e64__WSc3ff6d0ea77859461172e0811cbec20301-7f9a&quot; style=&quot;border-bottom-color: rgb(0, 153, 204); border-bottom-style: dashed; border-bottom-width: 1px; color: #3366cc;&quot;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;h4 class=&quot;sectiontitle&quot;&gt;



Returns&lt;/h4&gt;
A number. If conversion fails, returns zero.&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Val() serves a very specific purpose: It looks at the first character(s) of a string, and if those specific characters can be converted to numbers, it does so, otherwise, it returns zero. It is, therefore, used as a convenience to parse string values and convert them to numbers as needed. Note the use of our four letter word, convenience. That means, the programmer (you) should have a reasonably good expectation that the data arriving is going to start with numbers to be converted. It should &lt;b&gt;not&lt;/b&gt;, however, be used &lt;i&gt;carte blache&lt;/i&gt; across all variables that may or may not contain numbers.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Consider a database query, returning a value &quot;isUserValid&quot;, which has been returning with a &quot;1&quot; or a &quot;0&quot; character:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;cfif Val( myQuery.isUserValid )&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
In this case, the 1 or the 0 are converted by Val() into their true numeric form, which can be then be evaluated to a boolean true or false. Now, unbeknownst to you, another CF developer on your project (not as bright as you) has changed where the data comes from, and it is returned from a service as a struct, and the isUseValid is set like so:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;cfset myQuery.isUserValid = true /&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Now what happens when you run this code?&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;cfif Val( myQuery.isUserValid )&amp;gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
What happens is: The boolean value assigned to isUserValid, is not a boolean at all...it is a string value: &quot;true&quot;. And when passed to the Val() function, whose purpose is to look at the first few characters and convert them to a number (if possible), finds the word &quot;true&quot;, what do you think it returns?&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;cfoutput&amp;gt;&lt;/span&gt;#Val( myQuery.isUserValid )#&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;/cfoutput&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
It produces:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
0&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
And what does 0 evaluate to?&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
FALSE&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
And what have we learned from this?&lt;/div&gt;
&lt;div&gt;
&lt;ol&gt;
&lt;li&gt;Val() is specifically used for conversions of strings to numbers. Due to the way types shift freely in ColdFusion, it cannot relied upon for logic statements.&lt;/li&gt;
&lt;li&gt;ColdFusion implicitly casts many types of strings under the hood into appropriate values that CAN be boolean evaluated to TRUE / FALSE. Don&#39;t try to help it.&lt;/li&gt;
&lt;li&gt;Other CF programmers on your project are a menace and not to be trusted.&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cfbestpractices.blogspot.com/feeds/255188679006770896/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cfbestpractices.blogspot.com/2012/03/dont-rely-on-val-in-logic-statements.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7268570014893367744/posts/default/255188679006770896'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7268570014893367744/posts/default/255188679006770896'/><link rel='alternate' type='text/html' href='http://cfbestpractices.blogspot.com/2012/03/dont-rely-on-val-in-logic-statements.html' title='Don&#39;t Rely on Val() in Logic Statements'/><author><name>Shawn Holmes</name><uri>http://www.blogger.com/profile/05110605868875966328</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7268570014893367744.post-8053556046717513506</id><published>2012-03-05T20:23:00.003-07:00</published><updated>2012-03-05T20:23:32.498-07:00</updated><title type='text'>Prefer Len() and StructKeyExists() over IsDefined() and ParameterExists()</title><content type='html'>One of the first functions CFML programmers learn after picking up the language is the dreaded IsDefined(). On the surface, this is a highly convenient piece of code that allows the developer to check for the existence of a variable in a particular scope:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;cfif IsDefined( &#39;url.user_id&#39; )&amp;gt;&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
Here, a line of CFML demonstrates its use, checking for the existence of a variable named &#39;user_id&#39; in the URL scope. If you are a very old-school CFML programmer, you may even remember IsDefined()&#39;s predecessor, ParameterExists(), which still functions today (but is deprecated). However, ColdFusion developers know what deprecation often means, within the context of our infamous app server; we&#39;ll still retain backward&amp;nbsp;compatibility&amp;nbsp;to it for years to come.&lt;br /&gt;
&lt;br /&gt;
It is true, IsDefined() is&amp;nbsp;extraordinarily&amp;nbsp;convenient, because&lt;i&gt; it relieves the developer of any accountability on logical flow through their application&lt;/i&gt;. By using it, you are stating that you &lt;i&gt;think&lt;/i&gt; you have a pretty good idea of how your application is supposed to work, but in some rare instances that &lt;b&gt;elude you&lt;/b&gt;, there seems to be a point where your variables don&#39;t exist. So, rather than digging deeper, gaining insight into the real issue, you band-aid to the problem--while a far greater one persists.&lt;br /&gt;
&lt;br /&gt;
This is &lt;i&gt;not the right thing&lt;/i&gt; to do.&lt;br /&gt;
&lt;br /&gt;
Checking for the existence of variables is expensive, and in many cases, it is lazy. There is a better way.&lt;br /&gt;
&lt;br /&gt;
1. Default those variables which you feel may not necessarily exist through a particular pass into your code, via &amp;lt;cfparam&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;cfparam name=&quot;url.user_id&quot; default=&quot;0&quot; /&amp;gt;&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
2. Rather than checking for the existence of the variable (which you can now guarantee as the developer), check its value or length:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;cfif ( url.user_id ) gt 0&amp;gt;&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;cfif Len ( Trim ( url.user_id ) ) &amp;gt;&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
and if &amp;lt;cfparam&amp;gt; is not an option, then use StructKeyExists() with the scope, like so:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;cfif StructKeyExists( url, &#39;user_id&#39; ) &amp;gt;&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
Using IsDefined() is prone to error, because it is that very same &quot;relief of accountability&quot; that will ultimately come back to haunt you. Consider:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;cfset sl1on = true /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;cfif isDefined(&#39;s11on&#39;)&amp;gt;&lt;br /&gt;
...do something&lt;br /&gt;
&amp;lt;/cfif&amp;gt;&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
The above code will be interpreted by ColdFusion, not throw an error and work perfectly...to the unsuspecting developer. It will also &lt;i&gt;never, ever execute&lt;/i&gt; the code within the body of the IsDefined() logic statement.&lt;br /&gt;
&lt;br /&gt;
Programmers of all shapes and sizes should agree on a fundamental truth: As a programmer, you should understand what goes on in the software you write. You should possess intimate knowledge of how it functions, and what makes it work...and what makes it not work. Using IsDefined() takes you further away from that path, not closer to it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://cfbestpractices.blogspot.com/feeds/8053556046717513506/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cfbestpractices.blogspot.com/2012/03/prefer-len-and-structkeyexists-over.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7268570014893367744/posts/default/8053556046717513506'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7268570014893367744/posts/default/8053556046717513506'/><link rel='alternate' type='text/html' href='http://cfbestpractices.blogspot.com/2012/03/prefer-len-and-structkeyexists-over.html' title='Prefer Len() and StructKeyExists() over IsDefined() and ParameterExists()'/><author><name>Shawn Holmes</name><uri>http://www.blogger.com/profile/05110605868875966328</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7268570014893367744.post-5595023382382854735</id><published>2012-03-05T19:50:00.000-07:00</published><updated>2012-03-05T22:21:09.651-07:00</updated><title type='text'>Why This Blog Exists</title><content type='html'>I created this blog in a fit of rage.&lt;br /&gt;
&lt;br /&gt;
I am a passionate ColdFusion developer. I have been for over 15 years. Having worked with CFML for nearly two decades has taught me two truths:&lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;ColdFusion is not dead, but &lt;a href=&quot;http://aralbalkan.com/1864&quot;&gt;people&lt;/a&gt; &lt;a href=&quot;http://www.computerworld.com/s/article/9020942/The_top_10_dead_or_dying_computer_skills?pageNumber=2&quot;&gt;will inevitably&lt;/a&gt; &lt;a href=&quot;http://jason.pureconcepts.net/2008/07/why_coldfusion_is_dead/&quot;&gt;state that&lt;/a&gt; &lt;a href=&quot;http://devweblog.org/archives/35-ColdFusion-is-Painful.-Stop-Using-It..html&quot;&gt;it is&lt;/a&gt;, until the ends of time, and&lt;/li&gt;
&lt;li&gt;The CF community maintains a healthy amount of individuals that make the rest of us &lt;i&gt;wish it were dead&lt;/i&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
What do I mean by the latter? I mean that they view it as a &lt;b&gt;hobby&lt;/b&gt;, even when they have made it their career, yet people far less technical have come to rely on these &quot;experts&quot; for their so-called expertise, and what they produce is a &lt;i&gt;travesty&lt;/i&gt;.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
There are a good many things that ColdFusion does (and does well), because of the many conveniences it provides to &lt;b&gt;professional&lt;/b&gt; web application developers. However, there is a difference between a professional and a hobbyist:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;i&gt;A professional programmer&amp;nbsp;knows when he&#39;s about to make a bad decision in his code. A hobbyist remains blissfully unaware of the havoc he wreaks.&lt;/i&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
The conveniences that ColdFusion provides should not be taken lightly (professional CF developers know this), they need to be implemented with the same amount of care and foresight that you would come to expect in any other programming language.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
I would like to follow in the footsteps of &lt;a href=&quot;http://www.codinghorror.com/blog/&quot;&gt;other passionate programmers like me&lt;/a&gt;, who have a hunger to improve the programming community&#39;s education, and deliver a Best Practices blog for CF, a central resource where you can definitively learn about &lt;i&gt;why&lt;/i&gt; a CF application is built a certain way, not necessarily &lt;i&gt;how&lt;/i&gt;. My own personal inspiration and format for this blog will be based on &lt;a href=&quot;http://aristeia.com/&quot;&gt;Scott Meyers&lt;/a&gt;&#39; series of excellent &lt;a href=&quot;http://www.amazon.com/Scott-Meyers/e/B004BBEYYW/ref=sr_ntt_srch_lnk_1?qid=1331002095&amp;amp;sr=8-1&quot;&gt;&quot;Effective...&quot; C++ programming books&lt;/a&gt;, which I read years ago and granted me that same insight on exactly &lt;i&gt;why&lt;/i&gt; I should be doing certain things certain ways.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Many years have gone by since picking up ColdFusion, and I truly didn&#39;t feel like we needed this centralized &quot;Best Practices&quot; resource; that the community had just sort of &quot;cleaned up&quot; and taught one another how to function--and that simply isn&#39;t the case. So, we can do it the hard way, by pointing fingers at people, calling them out, saying their code is abhorrent, getting into shouting matches...&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
...or we can do it the easy way. Provide the resource, and silently direct them here.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
As always, I am just as passionate about learning, as I am about enforcing, so if there is anything I state on this blog that&#39;s wildly untrue or inappropriate, I expect the CF community to call me out as well--and I&#39;ll be sure to discuss, debate, and resolve. I&#39;ll admit when I am wrong--and make up for the massive amount of CF developers&#39; whose code I&#39;ve come across--couldn&#39;t admit the same. That way, it won&#39;t matter if ColdFusion is dead or not, we will all be free to still have our own opinions.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
...and when nobody&#39;s looking, you can come here, and learn how to do your job...&lt;i&gt;correctly&lt;/i&gt;.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cfbestpractices.blogspot.com/feeds/5595023382382854735/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cfbestpractices.blogspot.com/2012/03/why-this-blog-exists.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7268570014893367744/posts/default/5595023382382854735'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7268570014893367744/posts/default/5595023382382854735'/><link rel='alternate' type='text/html' href='http://cfbestpractices.blogspot.com/2012/03/why-this-blog-exists.html' title='Why This Blog Exists'/><author><name>Shawn Holmes</name><uri>http://www.blogger.com/profile/05110605868875966328</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry></feed>