YOUR FEEDBACK
Gregor Rosenauer wrote: well, not what's your take on this? Did I miss a second page of this article or...
AJAXWorld RIA Conference
Early Bird Savings Expire Friday Register Today and SAVE !..


2008 East
DIAMOND SPONSOR:
Data Direct
Frontiers in Data Access: The Coming Wave in Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
Intel
Virtualization – Path to Predictive Enterprise
Green Hills
IT Security in a Hostile World
JBoss / freedom oss
Practical SOA Approach
GOLD SPONSORS:
Software AG
The Art & Science of SOA: How Governance Enables Adoption
PlateSpin
Effective Planning for Virtual Infrastructure Growth
Fujitsu
Automated Business Process Discovery & Virtualization Service
Ceedo
Workspace Virtualization
Click For 2007 West
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
TOP COLDFUSION LINKS


We Programmers Need Etudes
Why musicians have a leg up on us programmers

Have you ever noticed the correlation between musicians and programmers? Some of the best programmers I know are or have been musicians. I recently had a class where 70% of the students were active musicians - some even with CDs of their work.

Perhaps one of the reasons for this pairing is that both musicians and programmers have their heads immersed in abstract concepts while their fingers (literally!) translate these into tangible "products." Musicians take concepts such as keys, scales, and silence and combine these into melodies that can be played; programmers take concepts such as types, variables, and encapsulation and combine these into programs that can be run.

Both endeavors, under their respective skins, are based on mathematics. (When computer science began to be taught at universities, it was an offering of the Mathematics department.) Both musicians and programmers learn a catalog of patterns, which they commonly use. And both music and programming require a high degree of both creativity and discipline: one without the other is not enough to achieve on the highest levels.

But musicians have a leg up on us programmers: they have etudes. An etude, literally a "study," is a piece of music written for purposes of practicing or displaying technique. For every instrument, study books (etudes) are available to help musicians learn specific techniques and practice in particular areas.

Etudes are helpful because they recognize that, in order for something to be truly useful, it must become natural and easy for you to use. It's paradoxical but true: to achieve such effortlessness, you must expend a great deal of effort, and to achieve mastery, there's nothing as helpful as practice. It's all very good, for example, to know that a certain bit of information might be represented as XML, but without intimate familiarity with XML in general and ColdFusion's processing of XML in particular, you're unlikely to use XML on a real programming project.

There's surely no shortage of programming books that explain concepts (such as XML), but there's a dearth of structured practice pieces that test how well you can translate from the conceptual to the tangible. In short, we programmers need etudes. My plan, over the next several months, is to help provide just such practice pieces. I hope you'll find them helpful and I'd love to get your ideas on subjects for etudes (hal@halhelms.com). This month's etude deals with ColdFusion arrays. You can download the solution from www.halhelms.com/etudes/1.cfm. Ready?

Problems

  1. The following table represents the average monthly prices for gold for the years, 1994-1996. Create an array to store this information. (Note: The month names and years are part of the array.)
  2. Programmatically determine: what month had the highest average for all three years?
  3. Programmatically determine: what year had the highest single monthly price for gold?
  4. Sort the gold array by the average price of all months in ascending order. For example, suppose the average price of gold was: 1994 - 376.58; 1995 - 372.92; 1996 - 384.23. Then, the array's first element should be the prices for gold during 1995, followed by 1994 and finishing with 1996.
  5. Remove the non-price information in the gold array above - that is, the abbreviations of the months in the first row and the year numbers in the first column.
Solutions
  1. What's needed is a two-dimensional array. There are two options for this. To conserve space, we'll just get you started with creating the array. If you really get stuck, you can download the full program.

    Option A: Create a monolithic two-dimensional array and populate it See Listing 1.

    Option B: Create a series of single-dimensional array and "stack" them See Listing 2.

    Whichever way you opt for, it's important to understand that you're producing exactly the same array. If you <cfdump> both goldA and goldB, you'll see they're identical. The reason? A two-dimensional array is nothing more than an array of arrays. Similarly, a three-dimensional array is just an array of arrays of arrays. And while you can't create more than three-dimensional arrays in a monolithic fashion, you certainly can by stacking arrays.

    Understanding that multi-dimensional arrays are nothing other than arrays of arrays will also help you use ColdFusion's various array functions, most of which are meant to work with one-dimensional arrays. Does that mean that these functions can't be used with multi-dimensional arrays? They certainly can - if the argument you supply to the array function is a one-dimensional array.

    For example, consider the ArrayAvg function. This function requires a one-dimensional array. Pass it a multi-dimensional array (such as our gold arrays) and the function will throw an exception.

    But you can use the ArrayAvg function - if you provide it with single-dimensional arrays:

    <cfset avg1994 = ArrayAvg(goldA[2]) />

    In this example, goldA[2] points to the single-dimensional array of goldA:

    Now, while the ArrayAvg function doesn't produce any errors, it also doesn't produce the correct answer because it will average the entire row, including the "1994" found in the first element.

  2. Each of these problems helps reinforce the idea that multi-dimensional arrays are simply arrays of arrays. See Listing 3.
  3. To determine the year with the single highest monthly price for gold. See Listing 4:
  4. I'm not going to show you the code for this one, because I hope you'll work through this one on your own. I'll give you a couple of hints, though. You won't be able to use ArraySort. You'll need to come up with your own sorting algorithm. Sorting algorithms have received a lot of study (and argument). If you already have your own favorite one, by all means use it. If you're unsure of how to do a sort, look at Listing 5. It uses an insertion sort to sort a one-dimensional array. I'm asking you to sort a two-dimensional array, but by now this shouldn't create any great problems: you know a multi-dimensional array is nothing but stacked single-dimensional arrays Listing 5.

    Unless you're an old pro with arrays (and sorting algorithms), this will probably cause you some grief, but that's the point of etudes: to place you in a contrived situation so that you can work on specific weak areas. Work through it and only download the answer if it's a choice between that and flinging yourself out of the window. (My personal philosophy is to prefer the admission of defeat to the act of defenestration - but that's just me.)

  5. After that last problem, this one is easy:

    <cfset ArrayDeleteAt(goldA, 1) />
    <cfloop from="1" to="#ArrayLen(goldA)#" index="rowNumber">
    <cfset ArrayDeleteAt(goldA[rowNumber], 1) />
    </cfloop>
Well, that's a good start on an etude for ColdFusion arrays. Hopefully, you'll be inspired to create your own problems to work on.
About Hal Helms
Hal Helms is a well-known speaker/writer/strategist on software development issues. His monthly column in CFDJ contains his Musings on Software Development and he has written and contributed to several books. Hal holds training sessions on Java, ColdFusion, and software development processes. He authors a popular monthly newsletter series. For more information, contact him at hal@halhelms.com or see his website, www.halhelms.com.

YOUR FEEDBACK
Christine Salib wrote: Excellent article, but the solution link does not work - www.halhelms.com/etudes/1.cfm.
CFDJ LATEST STORIES . . .
Rich Internet Applications offer the potential to fundamentally change the user experience and in doing so, yield significant business benefits. The theme of this October's AJAX World Conference & Expo 2008 West is 'Beyond AJAX to the RIA Era' and the Call for Papers, which is still op...
Join Scott Guthrie as he discusses Microsoft’s commitment to web standards development, Rich Internet Applications and how Microsoft is contributing to help move the web forward. Join Adobe’s Kevin Lynch as he demonstrates how Flash and HTML come together to make the most engaging,...
Virtualization has become a critical part of Enterprise IT strategy. Why and how has it become one of the most important change agents in our industry? To answer these questions I had the good fortune recently to be able to speak to a select group of top IT industry executives who join...
SQL Injection attacks are one of the easiest ways to hack into a website. One recent hack, using a script from verynx.cn, involves injecting sql into a web form that then appends some JavaScript code into fields in a database that then gets executed on the client side when a user views...
Recursion Software released a private beta version of their Voyager mobile platform, with powerful interoperability for Android, Microsoft .NET and Compact Framework (CF), all Java editions (JME CDC, JSE and JEE), and more than 15 embedded operating systems. The Voyager platform is a p...
2008 is going to be an important year for Rich Internet Applications. Most organizations are delivering or planning to deliver Rich Internet Applications; however, at the same time, most IT managers are facing a dilemma: which Rich Internet Application technology and platform to use? T...
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


SYS-CON FEATURED WHITEPAPERS

MOST READ THIS WEEK
ADS BY GOOGLE