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.

test = createObject("component","testCFC");

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!.