I have a hard time writing slow ActionScript 3

I have a problem going backwards. When I learned attachMovie in Flash 5, that function was the death knell for my Director career. “Create something from nothing!?!?” :: queue Quake sound :: “GODLIKE!!! (nsfw)“.

In Director, everything you wanted to be on a depth had to have had something there originally, put there on the “score” (Director’s timeline). If you didn’t do this at author-time, you couldn’t swap things out on that depth. The attachMovie function in Flash was great because this didn’t matter; I had thousands of depths to play with; the sky was the limit!

I later learned the sky on _root had a limit; it was called the Settings Panel. Lakitu wouldn’t even go near that elevation. It didn’t matter though; creating new MovieClip’s allowed millions of depths that designers & coders never used; it was wonderful.

To be fair, you could abstract this stuff in Director. The crew over at Director-Online had at least 2 sprite engines that emulated what Flash did, with extra features. Furthermore, I couldn’t really appreciate this stuff back then anyway; I couldn’t even return values from functions. I just had functions set global variables. Class? Que? n00b.

I tried going back after I learned some things, but I just couldn’t do it. Flash offered too much. It was soo fast. So much more effective. ActionScript just seemed more organized than Lingo.

After getting over my frustration at AS1, I learned that I could create re-usable stuff. While I loved to re-invent the wheel ALL THE FRIKIN’ TIME, it did get tiring after awhile, especially when my 4th generation Scrollbar still lacked track clicking and scroll buttons. Creating re-usable tools was hot.

After getting over my furious frustration with AS2, and learning enough design patterns to pretend like I knew what I was doing, I slowly developed a loathing for AS1. I then had a hatred for anything not AS1; aka, code puked on frames.

I remember seeing a lot of creative things done with prototype in ActionScript 1, however, and even some still in AS2. So, I tried to go back, and experiment. I always ended up getting bit in the arse in the end.

When AS3 came out, the early version, it actually wasn’t that different (for me) from AS2. AddChild vs. createNewMovieClip, aka the new DisplayList was pretty easy. Protected vs. private, and other namespace features made sense pretty quickly too. The only hard thing was learning the immensely large new API. It’s one thing to remember that a CheckBox emits both a click AND a change event in the Flex SDK, and you need to care about the latter… it’s another to remember that navigateToURL is in the flash.utils.* package. Even the built-in stuff is immensely large.

What is interesting, though, is that I never developed a hatred for AS2. Because of the complete and utter lack of communication between the 2 AVM’s, combined with Flex’ slow rise to power meant a lot of parallel AS2 and AS3 work. If you asked me if I wanted to do an AS2 or AS3 project, I’d probably respond AS3, unless your deadline was a week or less.

…and that’s my point. Writing AS3 is slow. It’s finally reached a maturity as a programming language; a real language, not a “scripting language” because it now officially compiles to machine code on 3 differentchip sets. As a real language, it has all the features (most, hehe… private constructor what, abstract classes who?) a traditional programmer would expect from a programming language.

My boss, a professional Java Developer who now does a lot of PHP (and I’m sure has a larger illustrious career I don’t know about yet), said that a strongly-typed language should be making me faster, not slower.

In shock and anger, I pulled my phone from my pocket, called up “Bullshit”, and requested he leap upon my bosses’ head.

There is a reason I click the “Flash” icon on my task bar, do File > New, hit F9, and test a new String parsing algorithm there in AS3 instead of going the ActionScript project route in Flex Builder… or even “new ActionScript File” and making it launching it as an Application. Flash is faster. Sure, once you get rolling, you can have some good test bed code combined with test cases even run by ANT in Flex. I’d still argue the instant gratification in Flash beats Flex.

…but it doesn’t stop there. I mean, after hearing about things such as “int promotion” and “compiler optimizations based on clear, strongly-typed variables”, AS3 can get out of control. Parsing a pipe (|) delimited string, properties separated by commas (,), with name value pairs separated by equal signs (=) has gone from 15 lines in pre AS1 to 40 in AS3 if you ensure every parsed variable is strongly typed. For an algorithm that’s only run one time very rarely, AS3 doesn’t really benefit here.

Fine? Then why not NOT strongly type so much? Only strongly-type to ensure you don’t get any type casting exceptions, and move on with life?

…because… I … can’t. It’s hard to stop. I’ve been trained by the fear of the software engineer army to strongly-type everything. To ensure the compiler has a crystal clear understanding of everything, and that my code will run at moch-10. I can have faith that the old AVM will never even need gas, because the key in the ignition for it will never be turned. All the code I write now ensures everything is typed, and all namespaces are correct.

I’ve tried abandoning namespaces and using *, Object, and even just abandoning type-casting in general for quick tests or prototypes. It’s just as time goes on, I find the code I add to those things starts getting more AS3 like.

Bottom line, it’s really really hard to break the habit. Every time I go back to AS2 for some random project, I’m reminded how damn fast it is to create things in it. Half the exceptions I get in AS3 I really don’t care about, and don’t need to know about; they don’t impact the project from working if they are small in scope. A lot of my data is either primitives or simply arrays.

For smaller projects, AS2 is still the bomb. For smaller algorithms, less strong-typing just results in less, more readable code.

I can see why professional Ruby, JavaScript, and Python developers still exist. I don’t care what anyone says, loosely typed scripting languages still kick ass. While I wish their engines weren’t so damn slow for larger projects, lately, I’ve just been finding that for a lot of smaller scoped projects or areas, I’m glad I have the loosely typed option.

While I fully believe the majority of the industry will use C# for Silverlight 1.1 going forward, MAN was it refreshing to write it in JavaScript for 1.0. Taking a break from AS3’s strict ways was great. SmartFoxServer has an option similar to later builds of Red5. You can write in the standard Java for performance reasons OR in your scripting language of choice (JavaScript, Python, etc). For a lot of work, scripting languages are good enough, and since they are good enough, you can write less code, faster to get the same result. That last part is debatable, but not by me. After having a career in both, to me they both clearly have their place.

…what I can’t figure out is how to drop the “good habits” that AS3 teaches you to go back to fast and furious ways of scripting languages.

Ruby Chronicles #1: Blocks and Yield

A “wtf” at lunch today while reading this book. There is a section on the 2 types of blocks in Ruby’s methods, and a description of the “yield” method. Totally jacked; it’s basically like sending an anonymous function in ActionScript seperate from a method’s parameters. The function then in turn runs that block of code for every yield statement. The yield statement can also pass parameters to this code block. Why you’d do this is beyond me, but when I yield, I’ll be using yield… imagine that?

For example, here’s Ruby calling a method, passing a block that the yield will run inside of the method:

def method
   puts "start of method"
   yield("this'll be arg1", "this'll be arg2")
   yield("this'll be arg1", "this'll be arg2")
   puts "end of method"
end

method {|arg1, arg2| puts arg1 + ", " + arg2}

It’ll output (I think):

start of method
this’ll be arg1, this’ll be arg2
this’ll be arg1, this’ll be arg2
end of method

The equivalent in ActionScript 1 is:

function method ( someFunc )
{
   trace("start of function");
   someFunc.call(this, "this'll be arg1", "this'll be arg2");
   someFunc.call(this, "this'll be arg1", "this'll be arg2");
   trace("end of function");
}


method(function(arg1, arg2){ trace(arg1 + ", " + arg2); });

Which outputs:

start of function
this’ll be arg1, this’ll be arg2
this’ll be arg1, this’ll be arg2
end of function

The only difference is the ActionScript example sends it as an argument whereas in Ruby, it appears to be sent as a seperate entity from the methods invocation parameters.

Don’t get me started on the Archeologist degree one needs to read printf statements…

Anyway, weird stuff.

Agile Web Development with Rails First Impressions

Can’t sleep (as usual) so figured I’d write up my first impressions of Ruby and Rails after reading the first 8 chapters of Agile Web Development with Rails Sunday afternoon. Let me precursor this with I’ve only read 8 chapters in 1 book in a 7 hour period over 2 days. Anything stated below are my first impressions, and my opinions are preliminary and fleeting at best. As I learn more, I know what I think will change.

Rails? Really nice.

Ruby? No opinions yet, don’t know enough about the language.

ERb? Hate it.

Rails itself, at least from an implementation standpoint, seems to do all the things people hand-code themselves a lot of times. They keep re-iterating convention over configuration. This statement scores a lot of points with me because there are a lot of frustrating things I’ve had to do with Java projects. OpenAMF + Hibernate + Spring == XML hell. While I usually don’t have to deal with it since I’m client boy, I inevitably was drawn in on a few occasions, and hated it. I don’t mind configuring something if it’s going to work when I’m done, but time and time again, I felt like I was doing a form of coding (as were the Java guys) whilst playing with XML. I’m sure it scales, but I never really saw it run for more than 1 hour.

I’ve been pretty shielded from the ColdFusion project I’m currently on. The JRun + CF + SQL setup was an all day affair, and I only had 1 hiccup since. While I attribute part of that success to an extremely talented team, I must say I’ve been impressed; the only times things break is when my code is involved. In all fairness, the server-side code isn’t really worthwhile without the client, and vice-versa. Can’t test it till I get done and start to integrate.

Still, the VO’s are a monotonous pain to write, and I’m sure the server-side CRUD methods are as well. It should by automated at this stage of the game.

I remember when learning OOP, then design patterns, then frameworks like ARP and Cairngorm, you start following conventions. They are known, ingrained, and the decision to use them is intentional. You knowingly write more code to accomplish the same thing knowing that the extra code pays off later. This is where I’ve seen conventions, in a small part, pay off time and time again. So, while I’m sure there are a few things Rails disregards on purpose, I have faith in conventions, and how they have improved my programming results over the years. Thus, I have faith in Rails.

I still think, though, configurations are powerful and should not be disregarded. Just not sure where that fine line is, nor the ramifications for crossing one or the other.

It boggles my mind to think this hasn’t been implemented yet in Java, CF, or PHP yet. I’ve heard briefly information about Tapestry, and when it was explained, it sounded like the same thing, only the AWDWR book took a stab at it. I know CF has a gazillion frameworks for it, and even read 2 blog entries about CF on Rails awhile ago, so I’m sure someone’s already got something “good enough”. I’ve heard of Cake and a few other PHP frameworks, but the thing I’ve always found about PHP stuff is that unless you can read and understand PHP, you cannot really grasp the power of most of what I’ve seen. For example, going to the bookstore, or even your local user group, and you get someone to give you the gist of Fusebox for ColdFusion, or Rails for Ruby… but Cake for PHP? I don’t know, I just get the impression that the communities are smaller, and those who get it are smart enough to do so without documentation, or some sort of developer evangelism. Bottom line, I don’t trust my knowledge of server-side frameworks to really question that something of Rail’s caliber doesn’t already exist. I just know in reading those 8 chapters, I got it pretty quickly and would rather do that than write DAO’s in PHP all day. If I were a full-time server-side developer, I would probably know more about what tools are truly available and thus would have better context.

Nothing to say about Ruby yet.

The ERb’s bring back nightmares of ASP & PHP projects I’ve seen which have sql statements embedded in the page, and someone wanted me to “modify” it. Ugh, run. While I think their templating mechanism is straightforward and simple, I still feel that ERb’s are gross. One third of that is my past experience with ASP, PHP, and JSP which is admittingly (and thankfully) little. For simple projects, or simple data accessing components for Flash, they do their job and do it well. For anything else, hell no. The 2nd third is because to me, CSS & HTML are limiting. While HTML & CSS certainly offer a neat way to show text, display it, and control layout (for the most part), the whole page based metaphor feels like it’s only scratching the surface of Ruby on Rail’s potential. While I think they did an effective job using Controllers to not only give scoped variables for the view to use and built-in mixin methods which inject functionality at runtime, it’s the actual “pages” that makes me dislike it. Why pages? I know, I haven’t hit the AJAX parts yet, but still, I’d rather use Flex to handle all of the state, sections, and even session data. However, I can’t figure out yet how I’d write my Controller code once you add a stateful client to the mix. Steven says you can expose Ruby as webservices… perhaps that’s the ticket?

Furthermore, it’d be really neat to learn some of the guts of the automation routines and write MXML & AS files on the fly, compile with mxmlc, and generate those auto-CRUD pages into a single SWF; a Flex app with a richer, and more central & self-contained GUI as a front end. Again, those CRUD pages aren’t necessarily supposed to be your de-facto admin pages, but if you have a better GUI tool that can integrate into Rail’s automated nature, why not use it?

Overall, the ActiveRecord, after talking to Steven on the phone, and then reading about it Sunday, pretty much is what hooked me. While I still feel the pull of databases driving my projects, writing a class representation of a table and having that transparently save, update, etc. is just off the hook. I still don’t get why some of that stuff, like parent_of and foreign keys, etc. isn’t automated, but the book had a few footnotes talking about database disparities.

Either way, it’s fuggin’ pimp looking for a version 1.0 framework.