ColdFusion 9 CFC Var Scoping

Recently I was assigned a new client that uses mostly ColdFusion 9 for their applications. In the past the majority of my clients have not taken the opportunity to upgrade from version 8, and there has not really been much reason to that I’ve run into as well. However, this opportunity creates the needed effort to make sure I know the differences between 8 and 9, such as var scoping.

As you probably know the variables scope in a CFC is private to that CFC. In other words the object calling that component may not access this scope’s values until it is returned to it. This scope can be set using the “variables” prefix to the variable name. If you skip the scope, it will be assigned to the variables scope. The following code sets the same variable and will persist between calls to each method the component contains.

<cffunction>
    <cfset variables.var1 = "Example 1">
    <cfset var1 = "Example 2">
    <!--- resulting in both variables.var1 and var1 values set to "Example 2" --->
</cffunction>

Normally you would want to declare variables private to each method so that your values are never overwritten by other process. This is normally done by using the var keyword. However in ColdFusion 9 you may use the local scope instead. This local scope does not persist between calls to the component’s methods. For example the following two lines work the same (the var keyword would not be required in this instance if using CF9):

<cffunction>
    <cfset var var1="Example 1">
    <cfset local.var1="Example 2">
    <cfreturn local.var1> <!--- Returns "Example 2", returning var1 would result in an error as it does not exist --->
</cffunction>

The question now becomes “What if I var scoped a local struct to use in previous versions of ColdFusion (var local={})?” The answer is you are good to go. ColdFusion 9 will just create a struct named “local” in the local scope (local.local).  So if you are coding for CF9 there is no reason to var scope a local struct as you would just be creating an unused struct. Instead use the already existent local scope. But you don’t need to go back and re-factor your code to remove all the local var scopes as an empty struct will not cause any harm unless you where counting on local.local to be there in the past. The following example is backwards compatible in CF9.

<cffunction>
    <cfset var local = {}> <!--- In CF9 this will result in local.local, so leave this out --->
    <cfset local.var1 = "Example1">
    <cfreturn local.var1>
</cffunction>

Plastic Trash Bags Last Forever In A Landfill… Until Now

I’ve been increasingly “green” conscious over the past few years. I tend to buy anything a little greener such as biodegradable soaps, recycled paper towels, printer paper and cardboard boxes. I also try to remember to bring in the re-usable shopping bags to the grocery store, recycle as much as practically possible, and fill my vehicles with semi-renewable fuels such as E10 and E85.

Now plastic garbage bags have bothered me for years. Right now virtually every piece of plastic ever invented (unless it was incinerated) still exists. Americans use 3 billion pounds of plastic bags annually, the vast majority of which end up in landfills*.

I won’t resort to using paper bags because they don’t tie up and when they get wet from your garbage they fall apart. So I’ve always used plastic garbage bags to keep things sanitary. Some times evils in life are necessary for the greater good.

But awhile back I noticed a new garbage bag product on the grocery store shelf. After examining it I found an awesome alternative to my plastic garbage bags: a plastic garbage bag that goes away in the landfill with no harmful residue.

This bag has organic materials bonded with the plastic it’s made from. This bond allows the plastic to be eaten by microbes found in modern landfills resulting in just sugars, fatty acids,  amino acids. These leftovers are then eaten even more resulting in water, CO2, Methane, and biomass withing 1 to 15 years. There’s a big concern over CO2 (global warming), but I’d rather have a little CO2 released which is consumed by living plants than a garbage bag that will never disappear entirely. The Methane is also captured by many landfills to run generators to operate the facility and beyond.

These bags, in my opinion, are a little more flimsy than standard bags I tend to buy, but I’ve never had one fall apart on me yet. As an extra bonus this product was made from 40% recycled plastic, the box was made from 100% recycled cardboard, and it didn’t use any glossy ink. There’s more than one out there but this one was from “Green Genius” as cost around, if not less, than the other bags on the shelf.

I strongly recommend you switch to the product and help earth and humanity out just a little. As more and more people catch on it all ads up.

*Some information from thegreengenius.com

Me and My Droid

It’s been about 8 months since I picked up my HTC Droid Incredible Smartphone.

At the time I had a BlackBerry Storm. I picked it up because my boss and manager both owned one and I thought it would be both cool and useful. But what I found with the Storm was just a mobile, yet expensive way to read my email and attempt to browse the Internet. Its tactile response screen was a good idea, but was cumbersome trying to use the on-screen keyboard. Its typing suggestions and corrections where pretty awful as well. The apps I downloaded where basically used to try and make it run better. The worst part is I had to physically remove the battery each day to make it run correctly. The techs over at Verizon made some tweaks to help, but told me this was the norm. At the time though, this was about the best fully touch-screen Smart Phone on the market. Android was just some awful  alternative that I don’t think Verizon even offered at the time. But that soon changed.

Continue reading

Twitter + Mercedes = Something Close to the Batmobile

What happens when you tweet your car? Ha! Nothing – you have a dumb car.

But what happens when you tweet to the Mercedes Prototype? You get really close parking. The remote controlled Batmobile via Twitter has arrived.

Continue reading

“How to spot outdated JavaScript”

I ran into a blog entry today by David B. Calhoun on “How to spot outdated Javascript”. I found it to be such a great resource that I had to put it here so I can always reference it.

There’s a gazillion references out there on how to do things in JavaScript, but hardly all of them are correct. So how do you know what not to do?

This blog entry not only tells you what not to do but why not to do it.

Check it out here: http://davidbcalhoun.com/2011/how-to-spot-outdated-javascript

“The Scrollwheel”

Yes, I’m Mr. Poker Face!

SELECT * Schema Caching using JDBC

If you use JDBC drivers for your ODBC source in ColdFusion, you may notice that you don’t return a new column entered into your database table using SELECT *. You may also get an error such as “Syntax error converting the varchar value ‘billboard’ to a column of data type int”.

I’ve known about this for awhile from Mark Kruger, but other people have asked me about this nuance.

Basically when you use SELECT * in your query, it caches the database schema.

Example Code:

<cfquery name="q1" datasource="#application.DSN#">
SELECT * FROM TABLE1
WHERE COLA = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="A">
</cfquery>

Three things can fix this:

  1. Update your code to include the specific column names, such as “SELECT COLA, COLB”. This will also increase performance since SQL doesn’t have to lookup the table columns.
  2. Change your query somehow, even adding a space
  3. Restart the ColdFusion Service. This will flush out the schema cache.

Live About Us Photos

Today Andrew Wirick tweeted to check out secretpenguin.com’s about us page. Not only did it grab my attention right away (nice design) but after scrolling down a bit more I found quite the surprise looking at the photos of the staff. Not wanting to give it away check it out for yourself. Remind you of something in a movie you may have once saw? Very creative…

http://secretpenguin.com/about/

Dreamweaver CS4 hangs at “Initializing Extension Data”

I found today my Dreamweaver CS4 hanging at the “Initializing Extension Data” startup screen. The resolution I found was to delete the site cache at “C:\Users\[user]\AppData\Roaming\Adobe\Dreamweaver CS4\en_US\Configuration\SiteCache”. I’m running Windows 7 on a domain so your path may vary a little.

After restarting Dreamweaver it updated the site cache and started working again.

I think it may have freaked out on a large cache after I deleted the actual folder.

IIS 7, ColdFusion Connector, Default Document

Today I setup a new site in IIS 7, added the default document for index.cfm, then ran the ColdFusion connector. That was the wrong thing todo as now I have a “Cannot add duplicate collection entry of type ‘add’ with unique key attribute ‘value’ set to ‘index.cfm'”.

To resolve the issue I removed the line from the web.config file.

<add value="index.cfm" />