Coding Resolutions for 2009

A new year is here and with it my list of resolutions for the coding year. Therefore, I resolve to:

  • Write a working program in Java that is not a simple textbook example. I'm talking something I can use in the real world.
  • Write an application using ColdBox. A new year, a new framework. ColdBox just looks interesting to work with. I have not written on an event-driven framework before (go Fusebox!) so ColdBox ought to be a good introduction.
  • Attend CFUnited. Okay, I admit I cheated on this one. I'm already registered, but since the conference is not until August, I'm writing it down as a goal.
  • Release an open source application written in ColdFusion. This is a tough one that I hope I'll find time to accomplish.

There they are, out in the wild, my coding resolutions for the year. I met (most) of my goals for 2008. I hope to keep that trend for 2009.

On the Naming of Things

One of the challenges we face as developers is maintaining code written by other people. Yes, the ever present "they" who just cannot seem to live up to your own expectations and standards of how code should be written. I'm always struck by how hard it is to read other people's code, even if it well commented, contains properly scoped variables, and is written in a language I know.

[More]

MDCFUG Presentation on Zip Files

Last night I had the opportunity to speak at the Maryland CFUG about working with zip files in ColdFusion. Since this was my first time speaking outside of the office environment, I admit I was a little apprehensive. I spent some time worrying about everything that could go wrong: getting stuck in traffic (not a problem, it was a federal holiday so traffic was light around the DC metro area), my presentation and code getting corrupted (MDCFUG asked for a copy of the presentation ahead of time), Connect not working (OK, there were some lag issues there, something about Macs and Adobe Connect, but nothing major for me).

When it came time for me to start, however, all of those worries were gone. The folks a TeraTech run a good meeting, even small CFUGs. The turn out was light, most likely due to the holiday, but I wasn't talking to an empty room. Thanks to everyone who came and asked questions. Overall, the experience was positive and I will be looking for opportunities to speak again.

Reading Binary Files in a Zip File Before CF8

In my last post about working with zip files, I examined how to list the contents of an archive without using CF8's cfzip tag. Now that we have a list of files, let's look at how to read one of the entries.

[More]

Finding a Random Row

Every now and then I get a requirement to do something to a random table record. The most common is to retrieve a random record for display. Other times it is part of a data scrambling routine. So how is this done?

Most database platforms have a function that generates a unique identifier. SQL Server has NEWID(), which generates a GUID:

SELECT NEWID()

Example Output: 0B7869E5-1428-4CA9-9D5A-CDA0E9756DC2

MySQL has RAND(), which generates a random floating-point value:

SELECT RAND();

Example Output: 0.93845168309142

Using these functions, retrieving a random row from a table becomes as simple as:

SQL Server

SELECT TOP 1 *
FROM mytable
ORDER BY NEWID()

MySQL

SELECT *
FROM mytable
ORDER BY RAND()
LIMIT 1;

So what is really going on behind the scenes? The key to the randomness is the ORDER BY clause. As each row in the table is evaluated, it is assigned a unique value via the NEWID() or RAND() function. The value changes for each row and each time the query is executed.

Fusebox 5.5's NoXML Option - First Impressions

I have worked with Fusebox for several years, primarily version 3 and version 5.1. At work we have a large (I'm talking 8K+ files) application written in Fusebox 3, along with a few apps written in Fusebox 2 that pop up every now and then on the maintenance radar. It is time to start thinking of a large-scale rearchitecture and refactoring of the FB3 app, so I decided to take a look at the noxml option for Fusebox 5.5. I really like the use of CFCs over a big switch file, whether it is the FB3 fbx_switch.cfm or FB5 circuit.xml. I am a little unclear on how it all works. I decided to add a new view circuit and found that I could not nest them and have the implicit pickup work. This worked:
views
 + myview
But this did not:
views
 + new
  + myview
This definitely needs more exploration since I might want to have the option of grouping circuits together to help code organization.

Listing Zip File Contents Before CF8

ColdFusion 8 brought us the cfzip tag family, which greatly simplified working with zip files in ColdFusion. But, what about those of us who are not yet, for whatever reason, upgraded to CF8? Since ColdFusion has access to the Java API, we can use the classes in the java.util.Zip package to give us a hand.

[More]

Under the CF Hood: Peeking at Object Inheritance

I was chatting with a coworker today about random ColdFusion topics, and we ended up talking about the Java that underlies CF. He pointed me towards a few good resources for "peeking under the hood" that really stood out.

Simon Whatley's blog entry about finding out what makes up CF arrays and structures makes use of Java reflection to follow the inheritance path for those two object types. After reading it, I decided to write up a quick function for dumping the full class hierarchy of any Java object.

Note: Other UDFs do exist that do this, including the rather complete getJavaMetaData() function on CFLib.

function getClassHierarchy( obj ) {
    var local = structNew();
    local.hierarchy = arrayNew(1);
    
    local.currentClass = arguments.obj.getClass();
    arrayAppend(local.hierarchy, local.currentClass.getName());

    /* All java objects ultimately extend java.lang.Object, so stop once we reach it. */
    while (local.currentClass.getName() NEQ "java.lang.Object") {
        local.currentClass = local.currentClass.getSuperClass();
        arrayAppend(local.hierarchy, local.currentClass.getName());
    }
    
    return local.hierarchy;
}

The getClass() and getSuperClass() methods are the heavy lifters of the Java reflection API. The getSuperClass() method allows us to traverse up the inheritance chain until we reach java.lang.Object, the base class for all Java objects.

The results of this function are very informative. For example, the class hierarchy for a ColdFusion struct (using ColdFusion 7.0.2) is:

  • coldfusion.runtime.Struct
  • coldfusion.util.FastHashtable
  • java.util.Hashtable
  • java.util.Dictionary
  • java.lang.Object

Just a little peek into what makes CF tick.

A New Look

The title says it all. I recently started a new job so why not celebrate with a new look-and-feel. I should have more time and brain cells to write in the coming weeks too.

Friendly UDFs - Validating Function Arguments

I use UDFs (user defined functions) frequently in my coding. One recent function I wrote got me thinking about the importance of validating the data that is passed to the function as an argument.

[More]

More Entries