Temp variables to call functions - any point???
<cfset newRow = queryAddRow(myQuery,1)>
<cfset temp = querySetCell(myQuery,"turnOverWeek",59000,1)>
<cfset temp = querySetCell(myQuery,"budgetWeek",100000,1)>
<cfset temp = querySetCell(myQuery,"turnOverYear",2000000,1)>
<cfset temp = querySetCell(myQuery,"budgetYear",3000000,1)>
I just looked up queryNew() in cfQuickDocs (excelent site), and when I saw it done this way in the example, it got me thinking. We don't want any variable called "newRow" or "temp". So why bother, when the following works just as well:
<cfset queryAddRow(myQuery,1)>
<cfset querySetCell(myQuery,"turnOverWeek",59000,1)>
<cfset querySetCell(myQuery,"budgetWeek",100000,1)>
<cfset querySetCell(myQuery,"turnOverYear",2000000,1)>
<cfset querySetCell(myQuery,"budgetYear",3000000,1)>
Are there any pros or cons I am not aware of? Or is it just a personal preference?
As for now I think the last version is better in that it entails less typing, and I actually find it more readable.
What do you think?


Glad you made it back safely from the conference.
I never use these kind of temp variables and this is one of the reasons I like to use cfscript where I can as <cfset Object.Method()> (as opposed to simply object.method()) seems pretty strange to me, but it is better than having a variable that is not required.
Code is much more readable if you can distinguish between methods and functions that have a return value and those that don't (or whose return value you are not interested in). The Temp variable muddies this up.
Like Peter, I prefer to use cfscript for this kind of thing wherever I can. However, sometimes it's just "nicer" to do it in tag form, and in that case, I prefer explicitly assigning the result to a var-scoped variable, so that I *know* that the result will get cleaned up at the end of the function.
e.g.
<cfset var garbage = "" />
<cfset newRow = queryAddRow(myQuery,1)>
<cfset garbage = querySetCell(myQuery,"turnOverWeek",59000,1)>
As far as I can tell, it's just a personal preference, but I prefer to be explicit about things wherever i can.
http://www.fusionauthority.com/Techniques/2569-The...
These variables are no longer needed - in previous versions of CF 4.5 or 5 the cfset tag always required an assignment to happen.
Since MX (I think) this is no longer necessary - so there is no advantage in the 1st method unless you are looking for serious backwards compatibility!
Cheers,
Mark
I'll report on this once I actually wake up. :)
Really nice article. Not sure if the small performance gains for cfscript are still true (was it you that said somewhere that had changed in a recent version of CF or am I just spaced out after too much Turkey?!)
While it IS a matter of preference, I'm pretty strongly in favor of no unnecessary variable assignments. If you have a function or method that ONLY has side effects (doesn't have a return value) or where you don't care about the return value, I think it is actually a little misleading to assign that to a variable.