As many Flex developers know, the Flex Cookbook and ActionScript Cookbook series can be very helpful when you’re trying to quickly solve a problem. These cookbooks specialize in the type of problem that you just know has been solved many times already by other developers—just not by you. Or maybe you have but you can’t remember the specific classes needed and the specific sequence of steps.
The recently published Flex 4 Cookbook continues this tradition of offering useful snippets of commonly used code. Unfortunately the sample code provided in the download is difficult to use. I’ve tried several times to use sample code from the Flex 4 Cookbook and each time I’ve done so I’ve been annoyed and frustrated. The last time around I decided to take notes. Here are the hassles I encountered.
First, the samples are not part of a Flex project so you have to first create a project and then copy the MXML files into it. Next, the MXML files as provided in the ZIP file don’t have valid MXML filenames so you have to rename them before you can use them in a project. For example, the file for the example I wanted is named 24_8.mxml. That’s not a valid Flex filename—MXML filenames have to be valid ActionScript class names. So, I manually renamed the file.
Next, some samples require assets like XML files or images but those assets are not provided. For example, sample 24-1 doesn’t compile because books.xml does not exist and the font file it references doesn’t exist.
Getting back to the specific sample I was interested in, sample 24-8—it doesn’t compile because the namespaces are not declared correctly. Not too hard to fix: create a new MXML application and copy and paste the generated namespace code from the new app into the cookbook sample code.
But even after making all these manual fixes, I still had problems. The code was not showing the correct syntax coloring nor would it indent properly. I tried selecting all the code in the file and pressing Ctrl+I. That’s a quick way to correct the indentation for an entire file in Flash Builder. However, it didn’t work on this file. I checked for syntax errors but there were none. I tried closing the file and reopening it. I tried exiting Flash Builder and restarting. Nothing helped until I eventually figured out that the file is in a non-DOS format. I think it’s an issue with the order of the \r and \n characters at the ends of lines. I don’t know what OS the code came from but it doesn’t play well in Flash Builder on Windows. Once I knew the problem I was able to work around it by opening the file in UltraEdit and having it convert the file to DOS file format.
After all of those travails I was finally able to get the 27 lines of sample code working. But that’s way more trouble than I expect from sample code. I expect it to just work. This was the exact opposite.