Coldfusion's Evaluate()

Today I was going through an application turned over by a contractor and I began noticing that evaluate() was used frequently through out the code.

I thought by now we’d all know that using Coldfusion’s evaluate() function should be avoided. Evaluate() is a performance hit and is an indication of sloppy code. It may be possible that there are situations where it is unavoidable, but in my experience if you are considering to use evaluate() to solve a problem its a red flag that there is a better solution if you just think a little longer.

Structures

evaluate() example:


     

With Structures it is easy to avoid using evaluate() as you can dynamically create the struct key such as:


     

Queries

evaluate() example:



     

Queries are not much different than structures. The trick is that you’ll need to explicitly define the current row. This is something that is normally implicit.



     

Function Calls



Dynamically declaring function calls is a neat trick. The example code is a very (very) basic generic getter function, perhaps utilized within onMissingMethod(). Given any string it will try to call a getter function for that property. There is no other syntax to create this call. But using evaluate() should be a red flag. When you get into a situation such as this you should really ask yourself why you are organizing your code this way.

Perhaps you could forgo building a named function for each property and restructure your generic getter function to work like this:



or possibly even simpler as:



You should definitely be asking yourself why you would call a generic getter function if you built named getter functions. (Maybe you’ve made the methods private?) This is a simple example, but either way evaluate() is most often a sign of a larger coding problem.

AnythingToXML in Mura CMS

Brian Rinaldi over at RemoteSynthesis.com contacted me today and told me something that got me pretty excited. Evidently Mura CMS uses my AnythingToXML as part of its core requirements. I had to download Mura and see for myself; it does!

From the stats on RIAforge I can see that a few people have downloaded AnythingToXML but I never know if it worked out for them. Its great to see something that you built to get you out of a jam taking on a life of its own and helping other people out.

I have yet to work on a project using Mura, but from what I hear its a great content management system and is developer friendly. Hopefully I’ll get a chance to work with it soon.

AnythingToXML and XMLToAnything Updates

I spent some time updating AnythingToXML last month with a feature that allows for type hinting. I was trying to avoid this, but XMLToAnything really can not be made smart enough to know that something was once an array with only one element in it. Instead it will interpret it as a Coldfusion struct. Since XMLToAnything was written to reverse XML created with AnythingToXML I added the type hinting to the XML generation. If there is no type hinting it will still work as before and attempt a best-guess on what the XML should become.

Coldfusion’s Missing Huffman code table entry error Round 3

If you’ve been following along I’ve been trying to track down why some JPGs create errors when using CFIMAGE but most JPGs do not.

The work around I posted previously still did not work for some JPGs.

Looking back at the error reports I received a co-worker and I noticed that most of the errors came internationally from one locale. When we contacted that office we discovered that they were using MS Paint to edit and save their JPGs before uploading them. Switching them over to using MS Office Picture Manager to save the images made everything upload correctly.

I’m not sure if the bug is with MS Paint, Coldfusion, or the underlying Java, but if you’ve done everything and still are getting an error when working with images in Coldfusion (or Java?) then checking the source of your images may be be helpful in debugging.

Did anyone find MS Paint to be the root of their JPG image problems?

Coldfusion’s Missing Huffman code table entry error Redux

UPDATE

Updating Coldfusion with the newest patch did not work. We are still seeing the “Missing Huffman” error in production. This occurs a few times a day. I’m now attempting to use the work-around posted in the Adobe Thread on the issue.

Below is my version of the work around:


	
	
	
		
			newImageName =  createUUID() & ".jpg";
			// create a file object representing the path to the saved image
			outFile = createObject("java", "java.io.File").init( arguments.destination_path );

			// extract the underlying BufferedImage from your CF image object
			bi = ImageGetBufferedImage(myImage);

			ImageIO = createObject("java", "javax.imageio.ImageIO");
			// use ImageIO (instead of cfimage) to physically save the image to disk
			ImageIO.write( bi, "jpeg", outFile );
		
		
			
		
	
	

Coldfusion's Missing Huffman code table entry error

UPDATE

While working on a content management system with functionality to allow users to upload images there was an error that would occur seemingly at random. When a user uploaded a jpg nine times out of ten it would work fine, but every so often there would be an error “Missing Huffman code table entry”.

After some Googling I discovered on this blog that this is a bug in Coldfusion 8. Adobe has patched it and you can fix the issue by making sure you’ve installed the latest hot patch to Coldfusion 8. You can download the patch here.

Using Coldfusion's ImageNew() with imageOverlay()

UPDATE: read here

I am working on a web app where users are allowed to upload images. All the images need to be square when displayed so I planned to use the imageOverlay() function in Coldfusion to create a blank square image and then overlay the uploaded image on top of it. I was using the imageNew() function to create the square image dynamically based on the height and width of the uploaded image. It seemed like it would work, but every time I uploaded an image imageOverlay() would crap out with the error “Overlay operation requires the two sources to match in number of bands and data type.”

I did a Google search for this error and there was only one relevant result with this error from Ray Camden’s blog. He couldn’t figure out how to fix this error either and created a work around by duplicating and then transforming his original image to his needs.

I really wanted to use ImageNew(). The functionality was there, but it just wouldn’t work. This felt just like another Coldfusion ‘gotcha’ (isDefined(“session.myVar”)!) but figured I could come up with a work-around. My solution was to save the file to a temp directory and then load it again. Once this is done the image will work with imageOverlay.

There’s got to be a better way to make this work. Anyone?

Below is my workaround:








Coldfusion SESSION and COOKIE scope

Working with both SESSION and COOKIE scope tripped me up today so I wanted to illustrate what went wrong so hopefully others can avoid this.

Example 1:


This sets a session variable that will exist for the life of the session.

Example 2:


This sets a cookie that will exist for the life of the browser session.

Example 3:


This sets a cookie that will expire in 8 hours.

In my application.cfc I had set:

" />

I had incorrectly thought that this timespan would also apply to the COOKIE scope, but it does not.

Using Coldfusion's ValueList() to evaluate dynamic Query column names

Occasionally I use Coldfusion’s valuelist() function to generate a comma separated list from a query. The syntax of this function will only allow the function call use a static variable and cannot use a dynamically generated value.

I just found a great tip from Richard Davies’ blog: You can use the array ToList function in place of the valuelist() function which will evaluate correctly at runtime.

arrayToList(queryName.ColumnName)