Archive for the ‘Tips & Tricks’ Category

StringBuffer vs. List

Friday, July 25th, 2008

Just a quick tip that Shayne pointed out to me (after I had spent a few minutes staring at my code), use StringBuffer if you ever need to have a modifiable string.

Coldfusion uses the Java String class rather than StringBuffer. This means that everytime you modify a string, ColdFusion creates a copy of it in the background and doesn’t de-allocate the old one. Obviously this can quickly eat through your server’s memory. If you plan on only doing a few string modifications (or never modifying it), go ahead and use ColdFusion’s built in string class. Otherwise it will be better to use a StringBuffer.  If you ever want to use a StringBuffer object as a normal ColdFusion string, just use the StringBuffer’s toString() method.

Another benefit to StringBuffer is the number of functions available to you for things like substrings and treating the string as an array.  For a good example of replacing a ColdFusion string with a StringBuffer see this post.

A Temporary Solution To <cfscript /> Syntax Highlighting In TextMate and E-TextEditor

Thursday, June 12th, 2008

I had a co-worker ask me recently - is there any syntax support for <cfscript /> in TextMate/E-TextEditor. In short - I don’t think so, or at least my Google searching didn’t reveal anything immediate. In lieu of going back with nothing - I decided to patch up a solution that would work in the mean time.

What I did was add an entry to the tmLanguage file for ColdFusion syntax - that adds JavaScript syntax highlighting to <cfscript /> blocks. It’s not perfect - but better than all one color.

To use - open up your ColdFusion bundle, and find the ColdFusion.tmLanguage file. Within the XML tree, paste the following snippet and reload your bundles. It doesn’t matter where you paste it so much - as long as it fits within the structure.

Enjoy

        <dict>
        <key>begin</key>
        <string>(?:^\s+)?<((?i:cfscript))\b(?![^>]*/>)</string>
        <key>captures</key>
        <dict>
            <key>1</key>
            <dict>
                <key>name</key>
                <string>entity.name.tag.cfm</string>
            </dict>
        </dict>
        <key>end</key>
        <string></((?i:cfscript))>(?:\s*\n)?</string>
        <key>name</key>
        <string>meta.tag.any.cfm</string>
        <key>patterns</key>
        <array>
            <dict>
                <key>include</key>
                <string>#tag-stuff</string>
            </dict>
            <dict>
                <key>begin</key>
                <string>(?<=>)</string>
                <key>end</key>
                <string>(?=</(?i:cfscript))</string>
                <key>name</key>
                <string>meta.tag.any.cfm</string>
                <key>patterns</key>
                <array>
                    <dict>
                        <key>include</key>
                        <string>source.js</string>
                    </dict>
                </array>
            </dict>
        </array>
    </dict>

Reduce CF Markup In Queries Through A Simple SQL Trick

Wednesday, April 30th, 2008

Here are a couple examples of how SQL offers the ability to simplify your SQL/CF mix-in markup.

<cfquery name="qFoo" datasource="bigdb">
    UPDATE mytable SET
    <cfloop item="MyObj" collection="ObjectsGalore">
        #MyObj.column# = <cfqueryparam value="#MyObj.value#" />,
    </cfloop>
    1=1
</cfquery>

<cfquery name="qFoo" datasource="bigdb">
    SELECT * FROM mytable
    WHERE 1=1
    <cfif Len(arguments.somethingOptional)>
        AND baz = <cfqueryparam value="#arguments.somethingOptional#" />
    </cfif>
    <cfif Len(arguments.anotherOptional)>
        AND qux = <cfqueryparam value="#arguments.anotherOptional#" />
    </cfif>
</cfquery>