ColdFusion Tips: Identify common items in 2 lists

It's been forever since I've written anything here. I know, I'm such a slacker and I have no excuse. But after discovering the ColdFusion solution I'm about to share, I knew I had to write about it.

Yesterday as quick test I attempted to create a solution for the following: Given 2 lists of names, create a 3rd, sorted list that contains ONLY those names that appear in both lists.

Pretty straightfoward, right? Before I show my first attempt I think it is important to understand that I always strive to answer "yes" to 3 questions when creating a solution:

  1. Does the solution accomplish the goal?
  2. Is the code easy to read/understand?
  3. Is the code performant?

My straightforward solution became:

view plain print about
1friends = "Chris,Ashley,Joe,Mike,Ritch,Jennifer";
2family = "Ashley,Jennifer,Nancy,Mae";
3framily = "";
4for( name in friends ){
5    if( findNoCase( name, family ) ){
6        framily = listAppend( framily, name );
7    }
9framily = listSort( framily, "textnocase" );

In essence, loop through all the names in list "one". If the name also appears in list "two", add it to "common". If one of the lists was shorter (as "two" is)

I think my solution checks all those boxes. But looking at it I just knew there was a better approach. I played around with map but it didn't feel better, just more difficult to read. Then I unearthed array.retainAll(). As a ColdFusion developer I had never heard of it. It's not a method included in the documentation at CFDocs Array Functions.

Dear reader, retainAll() is a JAVA method, called in the format array1.retainAll(array2), that retains only common array elements of array1 and array2. Exactly what I wanted to do!

I changed my code to:

view plain print about
1friends = "Chris,Ashley,Joe,Mike,Ritch,Jennifer";
2family = "Ashley,Jennifer,Nancy,Mae";
3framily = listToArray( friends );
4framily.retainAll( listToArray( family ) );
5framily = listSort( arrayToList( framily ), "textnocase" );

This code accomplishes the goal and is mostly easy to understand. 2 of my 3 requirements for an acceptable solution.The use of retainAll() may not be clear for any CF developer with no java experience (like myself). But this can be mitigated by adding a comment, so I don't consider it a bad thing.

But how about the third question? How does it perform? Better than the original!

The average run-time over 10,000,000 iterations: Loop-based solution: 0.006364ms RetainAll-based solution: 0.0044926ms

It would be premature optimization to choose the retainAll-based solution based solely on the runt-time numbers, though. At that speed, the difference is really negligible. For me, the retainAll-based solution is the better choice because the code is more concise and easier to debug/maintain.

Interesting take on programmers

I was reading this article earlier and found this quote interesting: "You are a wizard harry. Programming is basically magic. Think about it - we study hard and pore over our books of lore (the computer + internet), to make spells (programs). Wizards can teach other wizards how to cast their magic. We can make things happen in the world spontaneously, and create wonderous devices anyone can use. (Like the internet and mobile phones). These devices can do things that would be impossible without our skills."

Yeah! Programming is just like that!

Sublime Text 2

I had been using Dreamweaver for development as long as I can remember; 10 years at least. When I started my new job 6 months ago I was told that nearly all the ColdFusion developers used it and it was no big deal to get a license. However, not too much later I got the itch to try a new editor. I saw some tweets concerning Package Control – once you have this installed, you have a resource within ST2 to install and manage a vast array of plugins.

From with the package manager I installed: ColdFusion for CF syntax functionality.

Automatic Backups. This is awesome. When you save a file, a backup will be saved to a local folder. It's almost like having a built in versioning system. This works great for me at work because all our development files are stored on the network. Recently a snafu occurred and an important folder was deleted. Backups were restored from the night before, but because of automatic backups I had a backup of all my work from up to a few minutes ago.

BracketHighlighter can be good for keeping your sanity. When you highlight an opening brace, bracket or parenthesis, it will highlight the closing match for it. If you do work in javascript or jquery, I would consider this a must have!

TrailingSpaces is something small with a big impact, for me. All it does is highlight any time a line ends in a space. This may seem like nothing, but we have a standard that specifically addresses this. If a line of code ends with a space, you will fail your code review. So this is a great frustation saver.

If you are frustrated with your current IDE/editor or just looking for something different give Sublime Text 2 a shot. I don't think you will be disappointed.

Why your site is slow

As a web developer I want my projects to accomplish 2 things. I want to exceed the expectations of my client and provide the user with a good experience. A lot of developers get into the mindset of "if it works, I'm done". They don't take the opportunity to make sure the users are going to have a good experience. I don't blame them. It's tedious to test compatibility and boring to make sure your error catching gracefully handles issues that may arise. But, ironically, a slow load time can quickly ruin a users website experience. I break load-time into 2 categories: real and perceived. Real load-time is the actual time it takes all elements and scripting to be 100% loaded and ready. Perceived load-time is the wait-time a user experiences before they can start using a site. A "fast" site optimizes for the former, while exploiting the latter. So, here are 3 things techniques I use to affect page load-time without affecting my coding.


Is real time processing really required?

As processing power has increased, data caching has jumped to the forefront and user-demand for immediate response has grown, developers seem to have moved away from batch processing. But in a lot of situations, this can be a better approach than real-time processing.


SQL Server Tuning: Stored Procedures vs Ad Hoc Queries

When I first started writing Coldfusion applications I was very naive about data and architecture. I can admit it. I did not use components and I wrote (and often re-wrote) queries in-line when needed. As my development matured and I began to more fully understand SQL Server as an independent platform I recognized my failures.


Let Bartlet Be Bartlet

My friends know I am a huge fan of the tv show The West Wing, hence the title of this post. As I was writing my post on using stored procedures, this one slapped me in the head and began forcing its way out. Too often developers think their language of choice is the end-all be-all of the development world. As a result they force it to do every necessary function when sometimes other tools, already readily available, can provide not only a better method, but can lead to exponential performance gains.


SQL Server Tuning: Using the Tuning Wizard

A post in a troubleshooting forum on Linked In got me thinking about query optimization. The original poster asked about identifying and solving deadlock situations. Many great responses preceded mine, but it got me thinking about how I troubleshoot these problems. So I thought I would write it up over a series of posts.


IIS 7 Search Engine Optimization toolkit

It has been a while since I have been excited about development. I hope that today signifies a change in that. I spent most of the day doing some freebie work for one of my clients. You see, I downloaded the Search Engine Optimization Toolkit for IIS 7 today.


Previous Entries