"At times I think and at times I am."
- Paul Valery





Productivity with ColdFusion

With recent articles proclaiming Coldfusion to be dead or dying, there have been several 'rebuttal' posts by members of the CF community defending it from misinformation that every author seemed to have (go figure). Many of those posts did a really good job of showing the readers that CF is alive and kicking and talked about some of Coldfusion's amazing qualities. One of those qualities that seemed to come up in almost every post was that Coldfusion enables you as a developer to be more productive. This is a completely true statement (as you will see) and I totally agree with the authors when they say it. The problem however is that none of the statements made those posts, were backed up with examples.

I think it's really hard to drive home the point of Coldfusion and productivity to non-CF developers without throwing concrete examples in front of them to see first hand. Many of the skeptical readers won't spend the time to figure out on their own if that statement is true or not, most will probably see it as "my language is better than your language" flame bait. I don't blame them for thinking that way given the pretense of the article and no examples to prove it.

My attempt with the rest of this post is to help back up the claim that CF enables you to be more productive. I tried to choose examples of task that are sometimes trivial but also part of the average everyday development.

Language Verbosity
I want to first briefly mention this aspect of Coldfusion as many claim that it is a verbose language. For simple operations it can seem take more characters than needed, but as you will see in the examples below Coldfusion is actually very non-diffuse and abstract. This makes your code much cleaner and concise, improving readability which helps save time overall in your development and refactoring efforts.

Keep this aspect in mind when you think about how you solve the examples below in your favorite language.

Querying the Database
This example shows how easy it is to establish a connection to the database, retrieve data from it and cache the result set for a specified length of time.

<cfquery name="qryGetStuff" datasource="myDSN" cachedWithin="#createTimeSpan(days,hours,minutes,seconds)#">
SELECT * FROM someTable
</cfquery>

Coldfusion will implicitly and efficiently manage datasource connections for you, alleviating the need for you to explicitly open and close connections to the database yourself. This means that you don't have to waste time coming up with a strategy (and coding for) connection management or connection pooling, with CF it's built in and configurable. This definitely saves time when you are dealing with extended 'sessions' and\or accessing multiple data locations.

On top of the connection handling bit, the attribute 'cachedWithin' will cache any given result set for a specified length of time. No extra classes, frameworks or sql semantics are needed, Coldfusion gives you caching capabilities with a single attribute.

Displaying a Result Set
After executing a query Coldfusion will return you a data structure containing the result set (if applicable) and a ton of additional metadata (if specified). Unlike other languages Coldfusion provides a very elegant way to iterate over your result set. The example below will show you how easy it is and some of the metadata elements you have access to.

<!--- Display Metadata about our query --->
<cfoutput>
   
   Record Count: #qryGetStuffMeta.recordCount# <br>
   SQL Statement: #qryGetStuffMeta.sql# <br>
   Column list: #qryGetStuffMeta.cloumnList# <br>
   Execution Time: #qryGetStuffMeta.ExecutionTime# (ms) <br>
   Was the Query cached ? #qryGetStuffMeta.cached# <br>
   
</cfoutput>
   
<!--- Display The result set --->
<cfoutput query="qryGetStuff" startrow="1" maxrows="10">
   
   #column1# #column2# ..... <br>
   
</cfoutput>

Not everyone will need the associate info returned with the result set, but I think its fair to say that looping over the results couldn't be easier. There are no method calls to make or awkward iterators to work with, CF keeps it clean and simple.

'Dumping' an object
Coldfusion has a tag which I consider to be a killer feature, I have yet to find it's counterpart in any other language (that is built in). <cfdump> is a tag that will take any object and serialize it into a collapsible HTML structure for display. <cfdump> works with almost every type of variable simple or complex including non native objects come from Java or .Net... pretty much anything CF can work with can be dumped. This is an extremely useful feature for debugging which painstakingly prevents you from having to write an HTML friendly serialization of objects. Below is the example of the HTML displayed from dumping a complex array:

Dump of complex array

Uploading a file to the server
In a single line of code you can upload a file to the server, have Coldfusion rename it if another files exists with the same name and to give you container fill with tons of metadata when the operation is complete. Take a look at the following example:

<cffile action="upload" fileField="myFormFieldName" nameConflict="makeUnique" destination="../parentdirectory" />

The 'filefield' attribute holds the html form field name used for the file and the 'nameConflict' attributes controls how CF will handle the upload when another file already exists with the same name. Other 'nameConflict' values can be "Error" to throw an exception, "skip" to do nothing and "overwrite" to overwrite the contents (default behavior).

In addition, the call above will return a container with a ton of information about the uploaded file. Here is are the attributes that are provided:

  • attemptedServerFile - Initial name ColdFusion used when attempting to save a file
  • clientDirectory - Directory location of the file uploaded from the client's system
  • clientFile - Name of the file uploaded from the client's system
  • clientFileExt - Extension of the uploaded file on the client system (without a period)
  • clientFileName - Name of the uploaded file on the client system (without an extension)
  • contentSubType - MIME content subtype of the saved file
  • contentType - MIME content type of the saved file
  • dateLastAccessed - Date and time the uploaded file was last accessed
  • fileExisted - Whether the file already existed with the same path (yes or no)
  • fileSize - Size of the uploaded file
  • fileWasAppended - Whether ColdFusion appended uploaded file to a file (yes or no)
  • fileWasOverwritten - Whether ColdFusion overwrote a file (yes or no)
  • fileWasRenamed - Whether uploaded file renamed to avoid a name conflict (yes or no)
  • fileWasSaved - Whether ColdFusion saves a file (yes or no)
  • oldFileSize - Size of a file that was overwritten in the file upload operation
  • serverDirectory - Directory of the file saved on the server
  • serverFile - Filename of the file saved on the server
  • serverFileExt - Extension of the uploaded file on the server (without a period)
  • serverFileName - Name of the uploaded file on the server (without an extension)
  • timeCreated - Time the uploaded file was created
  • timeLastModified - Date and time of the last modification to the uploaded file


Working with Web Services
Working with web service can sometimes be complex and a pain. This is especially true when dealing with SOAP based service. Many developers are shying away from them for the more simplistic REST model because they are easier to consume and publish but with Coldfusion, working with SOAP services couldn't be easier.

Publishing Services
To expose any piece of functionality as a consumable web service in Coldfusion, you simply provide a value of "remote" in the 'access' attribute of a <cffunction> tag... Thats it... In other web based languages you either need some sort of configuration set-up, extra classes or external mods to achieve what CF can give you in one simple attribute:

<cffunction name="doSomething" access="remote" returnType="MyCustomObject">
   <cfargument name="arg1" type="numeric" required="true"/>
   <cfargument name="arg2" type="array " required="false" default="#arrayNew(1)#"/>
   ...
   do something
   ...
   <cfreturn myCustomObj />
</cffunction>

The WSDL for this service is auto generated and is immediately available for consumers, simply navigate to the containing Coldfusion component (cfc) and append 'wsdl' to the query string, for example http://www.mysite.com/location/to/Service.cfc?wsdl.

Consuming Services Just like publishing, consuming a service is extremely easy. The example below shows how you might consume a service that looks like the one above.

<!--- Build a container for the webservice call --->
   <cfset serviceArgs = structNew()>
   <cfset serviceArgs.arg1 = 42>
   <cfset serviceArgs.arg2 = arrayNew(1)>
   <cfset serviceArgs.arg2[1] = "Hello">
   <cfset serviceArgs.arg2[2] = "World">

   <!--- Consume Webservice --->
   <cfinvoke webservice="http://www.mysite.com/location/to/Service.cfc?wsdl"
       mehod="doSomething"
       argumentCollection="#serviceArgs#"
       returnVariable="results" />

After the web service call, the variable 'results' will hold a local copy of the 'MyCustomObject' object returned from the service.

The examples that I covered are only a small subset of the many ways that CF can enable you to be more productive. It's combination of features and abstractness make it possible for developers to get more done in less time, hence Coldfusion enables developers to be more productive.


Comments
 Posted By Larry Buta at 3/26/08 4:16 AM
Another overlooked aspect of cfc's ease of use is how easily a cfc can be called from a flex application. So, for instance, if you have logic that was written for a purely HTML application you can swap that out for an RIA without changing your server side code! What could be easier?
Add Comment