Simon Baynes: Web Development

Var(ing) Variables

A real bug bear of mine is people not var(ing) variables in their functions. Many people think this is just an inconvenience but
trust me it is nothing compared to trying to debug code that is acting strange only to discover some hours later that your
variables are being overwriten by another function.

Some common errors include:-
Not var(ing) your indexes

<--- This is just a made up function that replicates structKeyList() --->
<cffunction name="getStructKeyList" returntype="string">
    <cfargument name="stStruct" type="struct" required="true">
    <cfargument name="sDelimiter" type="string" required="false" default=",">
        // now here is where we are var(ing) our variables  
        var lstKey  = "";   
        var iKey = 0; // this is something that many people forget and you must var ALL your local variables

        // loop through collection to get the key list
        for (iKey in arguments.stStruct) {
            lstKeys = listAppend(lstKeys, iKey, arguments.sDelimiter);

        return lstKeys;

Not var(ing) queries

<cffunction name="getQuery" returntype="query">
    <--- You must also var your queries --->
    <cfset var qGetData = "">

    <cfquery name="qGetData" datasource="myDSN">
        SELECT *
        FROM tbl_myTable

    <cfreturn qGetData>

So you see, it is easy to overlook all your local variables, but you really must var all of them as it will make your life immeasurably easier once your functions get more complicated.

By Simon Baynes