webDU 2007 over for another year

I have just made it home again after another great WebDu! Every year Geoff and and the folks a Daemon have put on a great show and this year was no exception so that guys! I look forward to next years already!

Now, a second thanks to the folks that came to my session on Instant messaging applications in CF, I will have the slides and the code up here sometime in the next 24 hours - Some of you pointed out a few things I could fix during my presentation so I want to fix that up first, so stay tuned.

Scopes in CFC

I know there is always a load of talk (and even arguments) about the various scopes inside a CFC. Brian Kotek has just posed the Definitive guide on CFC Scopes.

this is a must read for anyone that writes CFCs

Whitespace and ColdFusion

For those of you who missed it, Ray has just posed a really good summary of the things you can do to tackle whitespace in CF, it is well worth a look!

Working out which OS ColdFusion is running on

We have a mixed development enviroment at the office, some folks on Linux (ubuntu) some on Macs and some on PCs so it is crittical that we make sure that we code properly for all platforms and the one thing that always catches us out is the path seperator.

In the hunt for the perfect solution we knew we had to go to Java to ask it which platform it is running on. A quick search in google finds Luis Majano's post on the subject and this great little code snippet.

<cfscript>
//Get a reference to the System class
objSystem = createObject("java", "java.lang.System");
//Get the property desired, in this case, the file separator
fileSeparator = objSystem.getProperty("file.separator");
</cfscript>

A nice little thing to add to our application.cfc and cache for later

Google Maps and ColdFusion

After a few days of playing arround, I have whipped up a nice little custom tag to help you get users to enter their location.

It is up on our dev team blog over at RedBalloon Labs or you can go straight to it - Using Google Maps as a form field in ColdFusion

What? Tim Buntel is back!!

Wow! I have just read Damon's blog entry announcing to the world that Tim is back in the CF team!

that is great news! Welcome back Tim!

Google Sitemaps in CanvasWiki

Damm Ray Camdem is fast!

I was playing arround with Ray's CanvasWiki last night and in an effort to gain an insight into how it worked, I whipped up a new view for it that returns a google sitemap and then sent it over to Ray for him to look at and maybe include into the core distribution.

I wake up this morning to find that he has already released a new version of Canvas and included my view in it.

How cool is that?

RedBalloon Labs

As the development team at RedBalloon has been growing, we have been focusing on more cool stuff. We know that we do a fair chunk of 'normal' development but there are some things that we do that are quite cool, that we want to share with the ColdFusion community and so we wanted somewhere to put it.

At the moment there is only the blog there but we have plans. Head over to labs.redbd.net and check it out.

Whitespace and CFC

Continuing along the whitespace theme, it is time to look at CFCs. CFCs give the ColdFusion programmer a level of abstraction that they have been craving since just after the introduction of the custom tag. They are great for encapsulating away some of the messier business logic in our applications and help keep the weight of our actual cfm files down.

Unfortunately when it comes to whitespace, they can be just as bad as putting all that logic at the top of the page as by default the do nothing to help suppress whitespace, they just dump it all to the page.

Luckily there is a property on the cffunction tag that tells it to suppress all output from within the function call itself. Just simply add output="false" to all of your functions and you will notice a dramatic reduce in the amount of whitespace returned to your page by your custom functions.

What if my function outputs html code? Well this will force that HTML code not to be returned to the browser so you will need to make sure that you do not turn it on if you have your presentation layer code inside your CFC.

You can also get whitespace from the lines in-between your functions in your CFC, so if you have really well spread out CFCs you will want to add output="false" to the cfcomponent tag as well to make sure there is not extra space dumped out when you instanciate your cfc

How can I see how much whitespace is in my CFC? Well the easiest way is to just test it... get a blank page and call it like so.

<!-- start -->
<cfscript>
    test = createObject("component","testCFC");
</cfscript>
<!-- stop -->
Then view the source and count the lines between the comments. There should be just one. Now you know that there is no whitespace between your functions. The next step is a little harder, you need to repeat this process for every funciton in the CFC. Yes, it is a little over the top but if you are seirous about reducing you page weight this is the way to do it!.

ColdFusion and Whitespace - Part 3

So far we have looked at ways of controlling chunks of whitespace inside a page but many developers find that most of their cfprocessing and therefore their whitespace is at the top of their page. This is great from a whitespace suppression perspective, we can just reset the output buffer. You can do this with cfcontent and the reset option <cfcontent reset="true"> will erase everything in the output buffer.

Christian Cantrell posted way back in 2003 of a great little way to access the underlying Java architecture of CF to do the same thing.

<cfscript>
getPageContext().getOut().clearBuffer();
writeOutput(someContent);
</cfscript>

There are a few other neat tricks out there to help you put your CF output on a diet. We use CF_Accelerate all over the place to cache some of our slower code. As well as doing that, it can also clean out some of the whitespace in the cached content before it is cached.

Now that we have the tools to help us attack whitespace, lets have a look at some examples of good and bad code from a whitespace perspective.

The main cause of whitespace is the gaps between tags. When you end a tag, CF looks a the gap between it and the next tag and it decided that this is not CF code to be executed and thus returns it to the browser.

Below is a simple loop that displays a users name if the display flag is set to true. If you were to run this on a standard cf page you would get about five blank lines between each name. These come from the tabs and the new line characters around the ColdFusion tags.

<cfoutput query=q>
    <cfif q.display>
<p>#q.firstname# #q.lastname#</p>
    </cfif>
</cfoutput>

Lets use cfsetting to put this on a diet

<cfsetting enablecfoutputonly="true">
<cfloop query="q">
    <cfif q.display>
        <cfoutput><p>#q.firstname# #q.lastname#</p></cfoutput>
    </cfif>
</cfloop>
<cfsetting enablecfoutputonly="false">

Now as Jeff Coughlin pointed out in his comment to part 1, this is very old school - it was the sort of stuff I was coding back in CF4.5 to reduce the whitespace in my pages but it is still very valid today. Yes it does make it a little more code when you first write it but it much lighter.

If you want to go one step further, CFScript is the ultimate in control. Only the characters that you tell it to return make it to the browser.

<cfscript>
    for(i=1;i lte q.recordCount;i=i+1)
    {
        if(q.display[i])
        {
            writeoutput("<p>" & q.firstname[i] & " " & q.lastname[i] & "</p>");
        }
    }
</cfscript>
I am sure that someone is going to tell me I could have removed some of the formatting in this example and brought it down to just three lines of code but I wanted to put it all in so you know what you are getting into if you take the cfscript route.

How do I make the choice? Well for me it is easy. If there is a chunk of code that generates a tonne of whitespace and it is on every page then it is a candidate for converting into either straight CFScript or some for of serious whitespace suppression.

Take the calendar on this page, it uses a lot of whitespace. The whole control is dynamic so why not write it in cfscript. It will take some time to get it just right and it will not be as easy for novice ColdFusion developers to debug but it will be much lighter on your page.

The next target on our whitespace hitlist will be CFCs so stick arround...

More Entries

BlogCFC was created by Raymond Camden. This blog is running version 5.9.4.001. Contact Blog Owner