Scope Nazi
Some people are anal retentive about not using pound signs where they aren't necessary (my boss), some people are anal retentive about var-scoping local variables in components (Raymond Camden); and then there's Me. I'm anal retentive about scoping all of your variables. Consider this. When you execute this line of code:
There is only one thing that could happen: A variable in the "variables" scope is set with the value "bar". (Unless you're in a CFC, in which case you should be using var scope, like Ray advocates. ;) On the other hand, when you execute this line of code:
The value could potentially be read from one of NINE scopes, in MX7. To clarify, I'm fine with implying your variable scope, however, it irks me to no end when you infer your variable scope. (What's the difference between implying and inferring?) To wit, a colleague pointed out that she was having issues with this code:
<CFQUERY name="orders" datasource="#application.DS#">
SELECT distinct ORDERS.ordid, ORDplacedOn, ORDbillFirstName, ORDbillLastName,
ORDTotal, OrdTotal + isnull(Adjusttotal,0) as netsales, ORDERS.OSTATUS_id,
ordassignedTo, OSTATUSname, orders.ORDshipAmount, p.prodassembly_reqd
FROM ORDERS, OrderStatus, payflowpro,
(Select ordid, sum(adjusttotal) as adjusttotal from OrderAdjust where result=0 group by ordid) as OrderAdjust,
OrderItem OI, ItemView I, Product P, ordershipaddr osa
WHERE ord_completed = 1
AND ...
<CFOUTPUT query="orders">
<CFQUERY name="getManu" dataSource="#application.DS#">
select distinct manname, manufacturer.manid, itemname, albumid
from OrderItem, itemview as item, product, manufacturer
where orderitem.ordid=#orders.ord_id#
and item.itemid=orderitem.item_id
and item.prodid=product.prodid
and product.manid = manufacturer.manid
The issue was that sometimes the variable prod_assembly_reqd was undefined. The resolution was simply to use proper scoping. This is the code I sent back:
Using explicit scoping like this leaves zero room for confusion where the data is coming from. The last line, where I used the scope "?????", I noted that this is probably inferring the Variables scope, but that she should check on it and make sure she indicates which it is.
Posted in Best Practices | ColdFusion