Productivity with ColdFusion

 Monday October 29, 2007  ·  1082 views  ·  0 comments

With recent articles proclaiming Coldfusion to be dead or dying, there have been several 'rebuttal' posts by members of the CF community. Many of those rebuttals did a good job of showing other readers that Coldfusion is alive and kicking. One theme that constantly came up was that Coldfusion enables you as a developer to be more productive. In my opinion, I couldn't agree more.

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
Many people claim that Coldfusion is a verbose language. For simple operations it can seem take more characters than needed, but as you will see with the examples below Coldfusion is actually very non-diffuse and abstract. This can make your code much cleaner and be more concise, improving readability and helping save time overall during development, refactoring efforts and code reviews.

Keep this aspect in mind when you think about how you might 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.


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.

Grouping Query Results for Display
SQL statements that JOIN a one-to-many relationship will produce multiple records for the 'one' item in the one-to-many relationship. Quite often clients will want to see those records grouped together on the display, combining the results so that they show as an atomic unit when placed next to each other. In Coldfusion displaying grouped results is as easy adding a single attribute to an output tag.

Take for example the data set below, it represents a list of members and the services they have payed for.

 memberid	memberName	serviceName	serviceCost
-------------------------------------------------------------
1 Justin A Photo Service 5.50
1 Justin A Music Service 10.00
1 Justin A Video Service 15.00
2 Michelle J Photo Service 5.50
2 Michelle J Video Service 15.00

Your client might want to see the results in a typical alternating row fashion like the ones below.

Justin A Photo Service ($5.50)
Music Service ($10.00)
Video Service ($15.00)
$30.50
Michelle J Photo Service ($5.50)
Video Service ($15.00)
$20.50


To achieve this kind of grouping in the display you typically have to come up with some type of conditional logic to determine when to start the next member. This isn't overly complex logic but none-the-less it has to be written. In Coldfusion you don't need to even think about it. With the editing u need the 'group' attribute and an extra set of output tags.


CF can group on any column returned in the result set and achieve the desired output without conditional logic. The example above is probably not best one to show how this can be a time saving feature of Coldfusion, but it does show you can retain clean code in the face of scenarios like that which over time increase productivity due to clean readability.

 

'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 reject it if the file isn't of a certian MIME type, have Coldfusion resolve naming conflicts and then when finished, return to you a structure containing a ton of file and the operation. Take a look at the following example:


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 <cffile> 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:


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.

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


  • There are no comments.
 

Leave Feedback


Name


Email
Email will not be displayed

Website
( Optional )

Feedback

Post your feedback, HTML will not be rendered, only plain text.


Security

Answer the math problem below.
= 
Subscribe
Receive emails when others submit comments