Thursday, May 15, 2014

Mozilla, DRM, and Fighting Smart

News came out of Mozilla that was of zero surprise to me, and many others, but did upset and surprise a great number of people. Mozilla, long time champion of a free and open web, is backing down on their stance to never incorporate the new DRM mechanisms in HTML5 into Firefox. Firefox will officially support blackbox DRM’ed content played back exclusively through closed source components.

The new DRM support in HTML5 has been inevitable for some time. It is not inevitable because DRM is a good thing (it is not) or because the media companies are too powerful to fight (they are not). The inevitability of this beachhead was all due to two names on the draft authorship list: a developer from Google (the company that brings you the Chrome browser) and a developer from Microsoft (of Internet Explorer). When this DRM spec was first proposed it was obviously inevitable because it did not come from outside, but from within, and with a foothold in two of the most popular browsers in the world. It was obvious that from Day One both would support these new capabilities, that Netflix (also a co-author) and many other media sites would utilize it, and that users would be left with either a severely limited web experience or the option to leave Firefox behind.

Mozilla tried to make a stand and it was entirely admirable.

It was not, however, practical even for a second.

DRM can be beaten. DRM can be made irrelevant. DRM can even be made detrimental to the very media corporation profits that drove it into existence in the first place! This is not the day when these statements can be made in the present tense. The fight we have before us is a long-haul fight.

Had Firefox been kept out of this game entirely, it could not participate in that fight at all. We could all see the writing on the wall when Mozilla so valiantly tried to make their stand. Had they continued, we would have seen them launch (I’m sure of this) some campaign to push DRM free video portals as alternatives, to showcase that now all video content requires these measures. They would have been laughably limited and done little more than to showcase the (current) need to give users access to the content they actually do care about.

That is why, for the time being, this was the right move.

Firefox will continue to allow users to access Netflix and Hulu and Youtube content that requires silly measures to make content owners comfortable. Staying in the fight today will allow Mozilla to contribute to the fight for a long time coming, and I do think this will be a long time fight.

I just don’t know what that fight will entail.

Saturday, March 01, 2014

Link: Promise Anti-Patterns

Over on Tao of Code, there is a great little write up on Promise anti-patterns. Having been using promises heavily for the past year in my Javascript, I can say I've hit at least half of these and probably the other half without realizing it. I recommend you check it out if you are or soon will be using Promises!
Promises are very simple once you get your head around them, but there are a few gotchas that can leave you with your head scratching. Here are a few that got me.
Get the full scoop:

Saturday, February 22, 2014

Seeking Questions (and DOM/HTML5 Game Layout)

I had been pondering some architecture issues after a day of refactoring and cleanup on an HTML5 game I’m building at work. Some common data/UI integration problems were bugging me, mostly just for the feeling of good separation, and I was about to post to r/gamedev:

I'm looking for advice on some minor architect issues in some HTML5 gamedev work I've been up to. My background is as a web developer professionally and a very trivial game developer as a hobby, and I"m only recently combining those. However, for lots of reasons related to the positioning of this game as *part* of a larger web project at work, it isn't a traditional <canvas> HTML5 game, but being done with a combination of DOM and concern-separated logic.

I'm having trouble figuring out how where to draw the lines between the bits that implement my UI and the bigs that run logic behind it, and how to keep them in sync efficiently. There are patterns for this, but I don't feel like my usual approaches make sense in a game context, so I'm looking for any advice you've got.

This is a card game, though the situation and question would apply to other types of games, as well. Right now, I have a new refactoring that gives me two parts: a card logic component, which shuffles the deck, draws cards, manages a discard pile, play states, etc; and, all the animations and transitions of the card sprites that I display.

BAM. Right there the answer smacked me in the face. And at that moment my first thought was how interesting it is for the act of writing out a problem to trigger the solution in your head, and this isn’t a new idea in the world of even for myself. It is, however, something I want to cultivate, both in myself and others.

Write more and often, about your projects.

I try to keep a development journal and used to do a better job of it. I’m going to look at setting up some kind of prompt to keep myself reminded through the day and maybe that will help. But, on to the solution here.

If I were responding to this post, rather than writing it, maybe this is what I would say:

Separation is great, but at some point these different parts just have to talk to each other. At least, something has to connect them. You’re going to be redundant if you model all the card state twice, both in your logic and your UI, so keep one of them dumb. That’s probably going to be the UI.

Those visuals need to respond to the state changes in the logic, rather than trying to mirror them.

That logic going on, tracking your cards moving between the deck and the hand and discard pile and whatever other states you have needs to allow some reponse to that state changing. When the logic draws a hand from the deck, the visuals need to know which three cards to slide across the screen into your hand. When you play a card, the player needs to see that happen.

Just fire a simple event when the state of a card changes in the logic, and let the DOM update accordingly.

Sunday, February 16, 2014

Javascript Module Loaders Considered Harmful


I’m coming to an opinion of Javascript module loaders that is profoundly negative and I’d like to express why I think they are, generally, a bad idea. However, I do think they have a place, which I’ll get to at the end.

Now, I understand I might be in the minority here. Between the competing specifications of CommonJS and AMD modules, loader systems like RequireJS or the (honestly really awesome) Google Module Server, and the huge cultural influence of Node on the Javascript world, you’d be hard pressed to argue against Javascript modules these days. Scripts are old hat, too stupid, too inflexible. Everyone knows that and no one would make an argument in their favor, right?

I’m going to step out on a limb and say “Javascript Module Loaders Considered Harmful” and I know the baggage involved with declaring something “Considered Harmful”. I mean every ounce of context that phrase carries with it, and I hope I can persuade you.

Harm #1: Confused Debuggers

Module loaders often make debugging difficult in a number of obvious and subtle ways.

Compression, transpiling, and other pre-processing in the pipelines that typically support module loaders (which are not always necessary, but are in use in tandem consistently) rewrite scripts from the input module to the source that actually gets parsed by the browser. There is a frequent mismatch when debugging in the browser between your actual source and the code you see in the debugger. Source Maps are helping this, but support for them in both browsers and tool chains is still underwhelming.

Often times, even when the source is unchanged or the source maps work properly, the simple nature of the dynamic tags used by module loaders causes issue. Debuggers might not recognize the same script between page loads, without an inline <script> node, causing breakpoints to be lost, for example.

It is very hard to justify module loaders on their benefit when they can make debugging your code, the very thing they aim to help you organize and simplify, so much more difficult.

Harm #2: Module Load Order

Perhaps the single most common and frustrating thing to deal with in module loaders is the order of load or script execution. Compared to the expected load order of a series of <script> tags in your page, understanding when each module will be executed can be an exercise in frustration quickly.

Execution order from module loaders is usually a factor of the race to which modules are downloaded first and the inter-module dependencies defined which will keep actual execution of a downloaded script pending until its dependencies have all been loaded.

There is obvious merit here and helping us to define and understand dependencies, and to provide clear references between them in the form of things like require() calls, is certainly admirable.

However, the fact is that the vast majority of our projects don’t have such complicated intra-module dependencies to really justify this.

Harm #3: Workflow Complication

Simply put, using a module loader instead of simple scripts is simply harder to work with.

There are well known difficulties integrating other libraries that don’t use the same module system. That there are multiple such systems makes this worse. Library authors are forced to provide extra wrappers for all the different loaders or pick a side when their users have or care not to.

When you bring in new developers to a project or when a developer finds a library she wants to use which has decided to distribute itself as a module in a specific system, like RequireJS, there is an added barrier to use with little apparent benefit to a developer who just expected another <script> tag to their existing project. Is it worth integrating someone else’s preferred loader to use their simple library? I fear this may lead to fragmentation of our already disperse ecosystem.


The harms are not justified compared to the benefit when the majority of web applications utilize such a small and constant set of Javascript.

There is a case for module loaders, which can only really be made when the costs are outweighed by the benefits, and this is only really verified when the costs of not using a module loader are greater than the costs when you do use a module loader. Costs of not using a module loader are incurred when you have a large enough body of code that loading all of it at the start of your app is detrimental to performance, user experience, or both.
I write here about programming, how to program better, things I think are neat and are related to programming. I might write other things at my personal website.

I am happily employed by the excellent Caktus Group, located in beautiful and friendly Carrboro, NC, where I work with Python, Django, and Javascript.