Apollo AsyncIO and inline functions

I’ve been doing a bit of coding with Apollo’s asynchronous file I/O API. I’m curious to see how People-Who-Do-This-For-A-Living are handling this sort of thing. For me, it’s very easy and intuitive to treat it like I would an AJAX call, and thus my code is littered with blocks that look like this:

var fileRoot:File = new File(xmlPrefs.fileRoot);
var fileWhatever:File = fileRoot.resolve('whatever.xml');
if(fileWhatever.exists && !fileWhatever.isDirectory) {
	var fs:FileStream = new FileStream();
	fs.addEventListener(Event.COMPLETE, function(event:Event):void{
		var fs:FileStream = event.target as FileStream;
		var x:XML = XML(fs.readUTFBytes(fs.bytesAvailable));
		fs.close();
		/* do some XML magic here */
	});
	fs.addEventListener(IOErrorEvent.IO_ERROR,function(event:Event):void{
		/* do something graceful to handle the error here */
	});
	fs.openAsync(fileWhatever, FileMode.READ);
} // if whatever.xml

But I also understand that some people might find it more preferable to refactor that into something like this:

function onOpenWhatever(event:Event):void {
	var fs:FileStream = event.target as FileStream;
	var x:XML = XML(fs.readUTFBytes(fs.bytesAvailable));
	fs.close();
	/* do some XML magic here */
} // onOpenWhatever
function onOpenWhateverFailure(event:Event):void {
	/* do something graceful to handle the error here */
} // onOpenWhateverFailure
// later ...
var fileRoot:File = new File(xmlPrefs.fileRoot);
var fileWhatever:File = fileRoot.resolve('whatever.xml');
if(fileWhatever.exists && !fileWhatever.isDirectory) {
	var fs:FileStream = new FileStream();
	fs.addEventListener(Event.COMPLETE, onOpenWhatever);
	fs.addEventListener(IOErrorEvent.IO_ERROR, onOpenWhateverFailure);
	fs.openAsync(fileWhatever, FileMode.READ);
} // if whatever.xml

I had actually started with the latter syntax, with each callback having its own function, but I quickly found that my code was inflating with a boatload of tiny functions that only handled one file, as each file has its own idiosyncratic formatting. It just seemed more logical and readable to me to pull the functions into the places they were going to be used.

Sure, as far as code reuse goes, if you can refactor the code to rid yourself of copy-n-paste code then that’s great, but for little one-off functions like this? And is the loss of locality worth it?

What do you pros think? Inline functions or distinct blocks for everything?

Published by

Rick Osborne

I am a web geek who has been doing this sort of thing entirely too long. I rant, I muse, I whine. That is, I am not at all atypical for my breed.