December 02, 2004
Writing code to write your code
As a programmer, you have the ability to make your life easier by writing code to avoid repetitive tasks and large amounts of data entry. I've been noticing that I do this a lot.. how about you? Here are two quick examples...
When I was working with the language translations of SolVE, I had a file saved as UTF-8 with characters not supported by typical English fonts. I had a hard time getting Java to recognize the characters correctly in the file to display them properly. The workaround was to escape the characters as their Unicode equivalent ('\u' followed by four hex digits).
That meant taking each character, figuring out what it's Unicode value was, and replacing it with \u0732 (where 0732 would be the character's Unicode value). For large files, I knew this was going to be a pain and I definitely didn't want to do the conversion process by hand.
Enter Flash. I wrote a small script that would take the .properties file containing the string keys (language translations) and output the same file with the characters Unicode-escaped. The main loop was something like... for every line pasted into a text field on the stage, for every character in that line (after the first = sign), output "\u" + the_line.charCodeAt(pos).toString(16) to the output panel.
It worked really well - and the time it took to write the script (maybe 5-10 minutes) was much less compared to the time it would've taken to convert the entire file by hand. All I had to do was paste the contents of the .properties in a text field, click a button, and then copy the output panel contents back into the .properties file and everything worked great.
Another quick example - I needed to fill a data provider for a combobox with the state names and abbreviations. Writing 50 lines of code by hand with a lot of data entry and potential for spelling mistakes wasn't my idea of fun - so I wrote a little script to write the code for me.
I found a state combobox on an HTML form somewhere, copied the
<option value="AL">Alabama</option><option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
... etc
Then, I wrote a small script to process those option tags:
btn.clickHandler = function() { var arr = txt.text.split("\r"); for (var i = 0; i < arr.length; i++) { // if the line is blank, skip it if (!arr[i].length) continue; abbr = arr[i].substring(15,17); state = arr[i].substring(arr[i].indexOf(">")+1, arr[i].lastIndexOf("<")); trace("state_dp.push({label:\"" + state + " (" + abbr + ")\"});"); } }
Now, I ran the movie, clicked the button, and my output window was filled with statements like:
state_dp.push({label:"Alabama (AL)"}); state_dp.push({label:"Alaska (AK)"}); state_dp.push({label:"Arizona (AZ)"}); // etc
So, I didn't have to type in one state name at all - I simply had to find the HTML version (which was found easily), write a small script to process the input, and then copy the results of that script and paste that into the .fla where I needed a combo for all of the state names.
Little things like this can really make your life easier and save you a lot of time. That's why the Flash JavaScript API is so powerful as well...
I've also done this with ColdFusion. I'd take the results of a query and format them into ActionScript code, then copy the HTML generated view into an ActionScript file for the Flash movie.
How many of you write code to write your code for you?

Comments
I love JSFL, that's powerful language to gain lot of time with coding.
I often use it to generate quickly classes or patterns configurations (event classes, MVC or Singleton skeleton, main application and properties stub code ...).
Peter Hall made a cool script to auto-generate forms code.
http://www.peterjoel.com/blog/index.php?archive=2004_10_01_archive.xml#109918863850924082
That's a good example to illustrate this topic : "Code to write your code".
With Peter's authorization, i built and published a new version. I started this hack for my personal use.
No more Forms and DepthManager in this version.
I needed more generic context with MovieClips support (alpha, width and height properties) and depths hardcoded with some settings to modify depth offset and interval + properties prefix.
This script changes my life each day (thanx again Peter) and it's there : http://www.tweenpix.net/archives/000515.html
A good coder is a pure lazy guy, hehe ... ;)
Posted by: Francis Bourre at December 2, 2004 02:57 PM
Francis, Peter's code is really some piece of work! Like you, I have a modified version to produces form code almost similar to code I write by hand... it's a real life saver...
... also, sometimes we don't even have to go as far as writing _real_ code... for most things text based, a trusty editor and regexp is all you need...
take darron's second example for instance, (you could argue which is more difficult but) if you dumped the text into a good editor with regexp support, and search for:
<option value="([A-Z][A-Z])">([A-z]*)</option> (or many other variants that come back to the same thing) and replaced with state_dp.push({label:"\2 (\1)"});, that should also do the trick... i do this sort of thing almost all the time
Posted by: Emmanuel Okyere at December 2, 2004 05:03 PM
Earlier this year, i had to came up with a smart way to convert 100+ Excell files that the client deliverd to configuration files. The configuration files were actually Actionscript files, containing large arrays of words. Doing this by hand would be tedious and error-prone, copy/paste from Excell into Actionscript arrays...no way.
So i spent a few days hacking with Perl and some Perl modules that enabled me to read the Excell files and write to Actionscript files. I then ran the script recursively through a list of folders and within a minute i had a couple of 100 configuration files with only little manual editting to be done.
Whenever i have to do textparsing or converting large amount textfiles from one format to another, i tend to fall back on Perl with some regexes to do the dirty work. It's a fun break from 'real' programming :)
Posted by: Owen van Dijk at December 3, 2004 07:20 AM
I'll second on the underutilized power of regexp...
Aren't we forgetting macros here? I spent the better part of yesterday afternoon writing a jEdit macro to run an automatic copy/paste/find/replace.
So, for example, if I have a line like:
mc1.prop1 = mc1.func(value1) + (null == var1) ? 1 : var1;
Repeated for a bunch of mc's with the same naming conventions, I can run the macro for 2...n and save time, not only in typing, but in bugs arising from copy/paste errors.
Posted by: Ben Jackson at December 3, 2004 07:35 AM