Thursday, December 24, 2009

CF9: One Difference Between the New Ternary Operator and IIF()

When I first saw the ternary operator in java, I confess I did not like it. To my way of thinking, it was odd and cryptic, and I did not see the advantage over a standard if/else statement. Well, I have come a long way. For simple comparisons I now prefer its sleeker syntax over the more bulky if/else construct.


So it was quite pleasing to see the ternary introduced in ColdFusion 9. I never much cared for the IIF() function. (Too reminiscent of MS Access I suppose) But stylistic preferences aside, I just noticed the behavior of the ternary operator differs from IIF() in another sense. It uses delayed evaluation.

Take this silly example. When someCondition is true, only the variable foo is defined.

   <cfset someCondition = true />
<cfif someCondition>
<cfset foo = 123>
<cfelse>
<cfset foo = 321>
<cfset bar = 88>
</cfif>


If you are using the new ternary operator, a statement like the one below would succeed. It does not matter that the variable bar is not defined. Because the second variable is only evaluated when someCondition is false.
 
<cfset result = someCondition ? foo : bar />

However, using IIF(), both variables are evaluated immediately, whether they are used or not. So this code would result in an error: "variable BAR is undefined"

 
<cfset result = IIF(someCondition, foo, bar) />
To avoid the error you would need to use the DE() function, which just feels awkward to me.

<cfset result = IIF(someCondition, foo, DE("bar")) />

So in this sense at least, the ternary operator is a bit smarter than the old IIF() function.

...Read More

Wednesday, December 23, 2009

Merry Christmas To All

I just wanted wish everyone in the CF community a Merry Christmas. I hope you all have a wonderful holiday. See you next year!

(My favorite holiday tune)


Why NORAD Tracks Santa

...Read More

Tuesday, December 22, 2009

Wrap and Scale Text (The Ghost of Code Past)

A long time ago, I wrote a cfc for wrapping and scaling image text to fit within a given set of dimensions. I always thought it was something better done in java. Well, I finally got around to creating a java version, and it is a lot simpler to use. I also improved the algorithm used to auto-scale the text, and may write up an entry on that later. Plus I finally added support for line breaks (left-to-right text only).




Options:

Alignment: Supports left, right or center alignment
Antialiasing: Enable or disable text anti-aliasing
AutoScale: Enlarge or reduce text size to best fit the text within the given area
DrawText: Enable or disable text drawing (Could be used for measuring only)
Font: Supports custom font name, style or size
Color: Supports custom text color
Ignore Margin: (Experimental) Ignores top and bottom padding on first/last line of text for a better text fit
Increment: Supports fractional font sizes and custom incrementing

It works very well for small to moderate amounts of text. Out of curiosity, I did test it with about 62,000 characters. (Crazy, I know). While it did work, it took a while. So if you are thinking of trying to calculate the font size needed to print War and Peace on a matchbook cover, think again. It is just not designed for that kind of volume.

Anyway, I tried to make the library as flexible as possible. So for example, you could wrap text without auto-scaling. You could also get the measurements of the wrapped text without actually drawing it onto your image. (I am not sure there is a need for this. But if you wanted to do it, you could!).

Next up, I am thinking of wrapped text with mixed styles (...I do love a challenge)

Source: wrapImageText-0.1a.zip


Update 2009-12-22: Fixed wrong file version in source

...Read More

Wednesday, December 16, 2009

ColdFusion + Tomcat + Apache + Windows (Take the plunge)

For a while now I have wanted to experiment with other CF engines. But kept putting it off for various reasons. Well, this weekend I decided to take the plunge. My first attempt was using CF9's multi-server install with JRUN which met with only partial success. For whatever reason I was never able to get the Open BlueDragon WAR file to deploy correctly under JRUN.

Thanks to a suggestion from Peter J. Farrell, I decided to try using Tomcat instead. Surprisingly, it was very simple to setup. I followed some excellent posts by Todd Rafferty and Matt Woodward and now have all three engines up and running with Tomcat + Apache on windows (.. and yes Todd, it is very fast!). Now be warned, Matt's entry is long. But it is very informative, so read it all. (Besides, if you read my blog I know you are used to long entries.)

Now I am busily reading up on Tomcat and Apache, wondering why I did not do this sooner...


Update: When I installed Tomcat I selected a different JVM than the one that ships with CF9. So the first time I tried to invoke a web service CF threw an error saying the "...class file has wrong version 49.0, should be 48.0". In case you run into this issue, it was easily solved after reading a handy post by Rupesh Kumar: ColdFusion and WebService : "class file has wrong version. Note, after updating the tools.jar, I did have to stop and restart Tomcat for the change to fully take effect.

...Read More

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Header image adapted from atomicjeep