Blog

  • Who Has Time for Unit Tests?

    A couple months ago, I read about dpUInt, yet another unit testing framework for Flex. I got it from reading the docs, it seemed easy to add to an existing or new project, and I’m all gung ho for giving it a try. If it sucks, I know I have both FlexUnit and VisualFlexUnit as back up frameworks.

    Test Driven Development practitioners are very passionate about what they do. They give off this air that my early OOP mentor’s also did, “Once you go TDD, you never go back”. The funny thing about OOP in Flash is how quickly it dies a pathetic little death when a deadline rears its head.

    The amount of sustainable OOP in Flash is proportional to the deadline length. If I have 2 months, classes & framework for the win. If I have 2 days, screw you, on(press) { copyPasteCodeOnObjectsAndInFrames(); } and then show client early ’cause you know they’ll have massive changes.

    Is unit testing like this? When I read Peter Bell’s blog, and other unit testing scenarios, I get the sense that an existing solid software base is used. Templates are modified, run through tests to ensure the new code & design works, and then the code is deployed. This cookie cutter pattern repeats, and can repeat, because the original software is solid, is customizable for a wide variety of common customer needs, and unit tests ensure everything keeps humming along.

    The other sense I get is developers already know what they’re going to write.

    I’ve never been in either scenario. Either I’ve had to build from scratch, re-write from scratch, or build atop of crap because the client heard “re-write” and thus freaked out (thus re-writing from scratch). Even in the best planned waterfall based projects with good IA’s, and somewhat flexible deadlines, it’s always been a process of discovery, modified requirements, and re-factoring/re-coding. This is in partly why I enjoy programming in Flash & Flex. Not only do I get to do that stuff with code, but also with design.

    Therefore, it’s really hard for me to imagine how I could START writing test cases first in my project. The closest I’ve come to writing “tests” is when I create a new FLA to test a component that’s acting weird. Instead of testing the entire project, I’ll just boot up a new FLA in Flash, import the component, create it, throw some fake data at it and see what happens. In this way, I can more quickly find the problem, AND re-use the testing FLA (70% of the FLA’s anyway) at a later date if the component acts up again. This is after the fact, though.

    The same thing in Flex. If I inspect the HTTP traffic through Charles, and see that the webservice is returning everything fine, and debugging doesn’t reveal anything obvious, I’ll set up a test file that instantiates controls in a really simple environment. Instead of a large app, it’s 2 controls that interact with simple fake data. Again, this really helps me isolate problems and more easily find them. It also makes it easier for me to not only test later, but quickly test smaller portions of my code.

    Granted, sometimes I don’t like “going to all that trouble” and keep doing risk debugging; where you swear that in just 1 more compile you’ll find the bug and fix it… yet this is the 4th time you’ve said that and you could of already been well on your way creating a test harness to more quickly and assuredly find and fix the problem…

    Again, though, after the fact.

    Like everything in programming, I’m still reading more sources to get a better idea, it’s just hard to get a good feel for who in the Flex & Flash community is doing unit testing, and how. With the influx of traditional developers in the Flex world, I’m seeing more talk about it, yet not enough case studies with good details on how the work flow actually works from requirements gathering to sitting down and coding. Flash devs’ll try anything, just point us to the right way!

    People would ask me to justify the time it takes to actually use Cairngorm. In their example code, all they really did was neatly wrap 1 line of code setting 1 variable on a single ModelLocator in a Command. I’d respond that either they wait 3 months, and compare the newest Command in SVN with the one they emailed me to get their answer. If that didn’t make any sense, or they just didn’t want to have faith in JXL, I’d instead point them to a lighter weight framework.

    Am I in the same predicament? Are my deadlines, lack of hardcore requirements, and/or inability to easily articulate a base design preventing me from really benefiting from formal unit testing or do I just need to read more?

  • I Will Not Write a Book

    I keep getting emails with offers to write a book, or to help contribute in writing a book. While I’ve entertained the idea of doing a chapter (or 3) on Cairngorm, that’s just what it is, and will always be; an idea that I have no intentions of bringing to fruition.

    From my experience and talking to others in the Flash community, books pay jack both in lump sum payout and royalties. This is compounded with insane amount of time and energy it takes to write a good technical programming book, especially in a hot market right now. I could spend 4 months making shit money on a book that has a shelf life of 1 year with pathetic royalties NOT improving my skill set… or I could spend 4 months making hundreds of dollars an hour writing code improving my skill set.

    Gee, hard decision.

    Not only that, but finding technical editors, good ones who know ActionScript AND have time to proof read your chapters and test your code, is impossible. I know from personal experience, I was a technical editor for 2 books in 2005 and 2006, saw the challenge to get chapters checked even with a good amount of technical editors. I sucked at it, and felt horrible after words. Did I mention I didn’t get paid as a technical editor either? Not that I deserved it. Oh wait, a free book… um… I make that in 10 minutes of coding. Ungrateful bastard, I know. Seriously, who do you know that is good at ActionScript and is NOT busy as hell? Thought so. How do you make them not busy? Offer a higher rate than their next gig or just get lucky that they somehow feel the need to add technical editor to their resume.

    If you are a publisher reading my blog thinking that I’m a decent “candidate” to consider for authoring a book, while I appreciate your consideration, move on. The only book I want to write is only half technical, and have no clue if it would sell.

    …then again, I did get burnt out pretty bad at the beginning of 2007, quit consulting, and played Gears of War on my XBox for 3 months straight unemployed… I don’t recall that positively affecting my skillset although I did meet a lot of cool whacko’s on XBox Live. *ahem*.

    Ok, in that case, if someone can justify why it’s career advisable (…yet another resume bullet point does NOT count), how to get these publishers to front decent bling (aka, I can make $85 to $120 an hour contracting/consulting independently, non-firm associated right now for reference), and how to sell them on my whacky ideas I think would sell, then I might consider it.

    I mean, just because someone wrote a book doesn’t make them impressive to me. I’ve read some really bad Flash and programming books as well as some bad ass ones. There seems to be this benefit that if you write a book, you’re immediately perceived as an expert, even if you really aren’t, which is complete bs. Then again, there is something insanely pimp and philanthropic, and therefore self-fullfilling, about pouring your heart, soul, and knowledge into a writ for the pure goal of helping your fellows. Seriously, on the fence big time. Guy Watson and I think alike on this (at least he and did 3 years ago :: shrugs :: ).

    I’d argue spending one’s time coding, working on open source, and blogging, even if aforementioned tasks are done independently, are worth booku’s more to your personal career in both time, less stress, and money, both short and long term. Am I wrong? Naive?

  • Rails is Ghetto

    Rails is Ghetto: Zed, a developer, rants on his way out of the Ruby on Rails community. If you can navigate through the vitrol, there are some great 1 liners in there about software development & consulting. Not work safe.

    Via SixSigns.

  • PureMVC Frustrations and Code Behind Thoughts

    Over the holiday’s, I went to PureMVC.org to read over the documentation. PureMVC, although a year old, is the new framework on the block for developing Rich Internet Applications atop the Flash Player. There are plans to port the framework to other languages and runtimes like PHP & Silverlight in the future. Like Cairngorm, it’s a framework that uses coding conventions based on design patterns, namely the meta pattern, Model View Controller written in ActionScript 3. Unlike Cairngorm, however, it has no ties to the Flex SDK. This means you can use it with Flash CS3, Flex, or just pure ActionScript 3 projects.

    If you are a computer scientist, you’ll love the documentation. It’s chock full of design pattern discussions, justifications on framework implementations, and visual diagrams all in a nicely organized website. If you’re like me, the docs will get old after 30 seconds. I went straight to the example Flex project you can download to learn how it works.

    Upon digging, I found some familiar coding styles, and some unfamiliar. When I saw the use of View Mediators, however, I immediately got a bad taste in my mouth. I composed myself, and sent off a non-leading email off to Joe Berkovitz asking his opinion. I’m still waiting to hear back from Thomas B. I won’t speak for their replies, but it’s nice to know that not everyone finds PureMVC uber-perfect as the blogsphere’s reaction this past year has suggested.

    My first gripe is with View Mediator’s. A View is form, GUI, or some other visual control; it’s what the user sees in MVC. View’s can read from the Model, and ask the Controller to do things, inform the Controller of user events, and be informed by the Controller of the Model changing. A Mediator is a class that allows various classes to talk to each other without tight coupling. It is the de-facto design pattern in developing Flash & Flex applications. Farata Systems has a great post talking about it in context. A Flash Developer explanation is, “It’s a MovieClip that holds other MovieClip’s and allows them to talk to each other without them ‘knowing’ about each other”.

    In PureMVC, a View Mediator is created for each View. You don’t need to create this for all View’s since some via Composition (View’s used inside of other View’s) aren’t all that complicated and thus you don’t need to create all this code for such a simple class. The purpose of the Mediator as I’ve seen it used in the example Arch101Demo is to respond to Application state changes and/or events in the form of Notifications. Secondly, it can interact with Proxies to data; basically liaison’s to the data.

    There are a few problems I have with the first part. The Notifications contents are not strongly typed and thus suffer from the same problem that Cairngorm’s Delegates do that implement mx.rpc.IResponder. In Cairngorm, your Commands (classes that do Controller logic) can implement an interface called IResponder. This allows them to work with Delegate’s, classes that make web service calls. They return either a result or a fault. While the onFault is great in that you get back a FaultEvent that contains detailed failure information, ResultEvent is useless. You have to make assumptions on the data you get back since ResultEvent.result is cast as an Object. If I ask the server to give me a Person object matching an specific ID, I should get that back in my result event. If not, something went wrong, and my fault should be called. Instead, you have to force the Command (assuming you follow the standard Cairngorm Command way of implementing the IResponder interface) to “parse” data. Parsing data should not be in the realm of Controller logic, and instead be done strictly by services. Notification’s getBody, and other functions suffer from this same problem.

    It’s not the end of the world; people cast flash.display.Loader.content to what they “think” they are loading all the time. However, PureMVC had the opportunity to get things right here and they didn’t. The INotification interface’s list of functions (excluding getName) should at the very least return a type interface instead of Object. If you’re going to follow the whole “programming by contract” methodology, do it across the board and not just where you feel like it to support your framework.

    The other gripe is with ApplicationFacade events. You define Notification types (like Event types) that your Application will dispatch as constants. Your Mediator’s can then listen for these specific Notifications and react to them. This allows multiple Medator’s to listen to a single Notification. Whether they re-created their own event system instead of using Flash Player’s built in EventDispatcher system with custom Event classes for framework reasons, or whether they were trying to prevent application developers from burning themselves with un-registered Notification listeners causing memory leaks isn’t my main frustration. What is bad is that I fail to see how putting all of these events on ApplicationFacade will allow your application to scale in size.

    The whole reason you extend the AS3 Event class is to provide context to those who respond to those events. Some don’t care about context; for example, Application startup. However, when clicking on a List control, you need to know what item was selected, what was the previous item if any, what is the index of that item, and for View’s a strongly typed reference in case the Event bubbled up to you from a deeply nested View and you have no way to access the List in a parent child way. You can’t do that if you have a bunch of event names in the form of Notifications. Granted, you could package the payload into the Notification itself so it can act like an event, but the Notification has no strongly-typed way of doing that based on the INotification interface, and suddenly your packing in information into a Notification that others may not need. Granted part deux, yes, you could create your own Notification class, but explain to me why we’re not extending the built in Event class again? Event’s can bubble and be intercepted.

    Mediator’s have a reference to the View instance they are representing. The problem is, it’s up to the developer to handle this marriage. Cairngorm had something similar called a ViewHelper. It’s now deprecated, but the point of a ViewHelper was two-fold. First, it did the same thing Mediator’s in PureMVC try to do; greatly simplify the amount of logic in View’s. Second, they provide a non-GUI based class that can interact with others, preventing the View to have to know too much about the framework it’s in. In Cairngorm, they had an extra bonus of working well with a ViewLocator; a globally accessible class that allowed anyone to access a View’s ViewHelper by a magic string; it flattened the tree of GUI View’s.

    Mediator’s in PureMVC are definitely successful in those goals. If you look at their code and what the View’s in the example app are doing, it’s certainly more readable. There are also a lot less dependencies than say, Cairngorm where the second a View dispatches an event via CairngormEventDispatcher, it “knows” about Cairngorm.

    However, the down-side was in Cairngorm, ViewHelpers were created by the View’s. There was a 1 to 1 relationship that was instantiated when the GUI View was created. You couldn’t really screw that up. Although it was opt-in, a lot of people new to Flex just created 1 ViewHelper for every View. In PureMVC, they suggest you not only instantiate a Mediator yourself in a Command, but you also hand it the View instance yourself. While not major since ActionScript 3 allows us some strong-typing to ensure we “have the View we think we have”, why in the hell is not done for you? It’s an area where a developer can mess up; either they forget to actually create their Mediator, or pass in the wrong View reference. This is minor, but the whole point of a framework is to help you write less code, not more.

    Worse, what if I have multiple View’s? Do I then have to have each dispatch a new Notification (onUserEditViewCreate) just so I can create a new Mediator with a new View instance reference in a Command? WTF… the framework should handle these low-level details.

    The biggest gripe I have with PureMVC, and the one that made me all negative was how Mediator’s have access to Model Proxies. The ONLY person in MVC who should be able to SET data on the Model is the Controller. A Mediator, although merely having a reference to a View, IS associated with a View. It should NOT be setting data on the Model; the Commands should be doing this by listening to events (Notifications, whatever) dispatched from the Mediator’s. Just because a Command creates them doesn’t suddenly bequeath them Controller ‘esque powers. This is probably done this way because of my last gripe…

    …PureMVC Commands do not follow a true Controller pattern of setting data. One is instantiating a Mediator (StartupCommand) while the other is updating the Model (DeleteUserCommand). What the hell? I’d argue the DeleteUserCommand is correct, and the StartupCommand is wrong. In fairness, that’s because I’m comparing them to Cairngorm Commands. I’ll admit, this is the weakest argument I have, yet you have to agree it’s not consistent on what Commands are really for based on the example. Updating data? Creating things to have them update data? What if the Command wants to intercept the Notification and do something first, preventing the Mediator from doing it? Without a core Controller, you now have multiple types of classes setting data on Proxies. I can see it now… “Did I set the data in the Command or the Mediators…? Man, this’ll be a long morning…”.

    Thoughts on Code Behind

    On a side note, this is the second time I’ve seen a framework cater to a View code behind model. The theory and practice of code behind as it relates to Flex has been talked about and lauded a lot so I’ll leave it to Google to give you more details. The idea is that you utilize 2 classes or files; 1 for GUI and 1 for logic. The reasoning behind it is “separation of concerns”, the traditional computer science idea of making a large program easier to manage by breaking it up into pieces that each manage their area of interest. A lot of code in complicated View’s has absolutely nothing with drawing visual graphics to the screen, and is merely the logic behind the View. The current reason I don’t support this methodology in Flex is that FlexBuilder does not support it well.

    Silverlight & WPF on the other hand have tools built around this idea. In Blend, you control the actual real guts of the View; it’s layout, visual look, and transitions via XAML (like MXML). In Visual Studio, you code the logic behind it in C# or VB (like AS). In Flex, this currently feels forced. While convention helps, you don’t really get any help from the IDE in doing this methodology. This doesn’t bother me as I like coding my View’s to be 1 file and encapsulated. This can reduce readability, however, for complicated View’s that have a lot of code that doesn’t really relate directly to how the View displays itself.

    Cairngorm was the first framework I saw advocate this methodology via the aforementioned ViewHelper, leading me to believe that this is something Enterprise Java devs are used to doing. PureMVC is the second with the added twist of using the code behind not just as a separation of concerns, but also as a mediator the rest of the program that the View interacts with, hence the appropriate name. I typically use Mediator’s via Composition like Farata Systems linked above describes it. It is definitely a creative way of handling SoC and code behind in a framework way.

    Maybe if Thermo could create well written MXML and Flex 4 well written generated code-behind, I’d dig the idea and actually use it on projects.