Quote of the Week

"We build our computer (systems) the way we build our cities: over time, without a plan, on top of ruins." - Ellen Ullman

Quote of the Week

Remember rule #6*

*Don't take yourself so goddamned seriously.

Quote of the Week

"Always implement things when you actually need them, never when you just forsee that you need them." - Ron Jeffries

This quote reminds us to be mindful of what we are trying to accomplish. For example, don't write a switch statement when you only have one condition to check. Trust that when the time comes to add conditions the next developer knows when a switch will be beneficial. If you can't, your team has bigger problems.

Quote of the Week

"No code is faster than no code." - Merb Motto

Quote of the Week

"Refactored code either dies a hero or lives long enough to see itself become the legacy". - Mathieu Ferment

Removing the IF in the code

Yesterday I came upon this interesting read on the Anti-If pattern. It has some good tips on how to remove if-statements from your code. As I primarily work with ColdFusion, I thought it would be good to document a trick I learned to eliminate if-statements.


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!

The surreal world

The one piece of custom code I added to the blogCFC engine that I wanted back was the ability to customize the keywords on an individual entry. I wrote that code 3 years ago and certainly don't recall what I did. However, it was easy to add back in.

I wrote a blog post about it! It was quite surreal to read my own blog post on how to update blogCFC to do what I wanted. But, it took 5 mins and boom, it was back in place!

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.

More Entries