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: http://taoofcode.net/promise-anti-patterns/

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

Introduction


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.

Conclusion


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.

Saturday, February 15, 2014

How To Record Video-cast GIF Animations

I'm playing with some game dev fun on the side, and as part of motivating myself I hope to have something to post to /r/GameDev's Screenshot Saturday event every weekend. This quickly got me wanting to show off something other than a still image, so I went looking for how people produce the animated screenshot GIFs I see developers posting of their games all over. Here's what I found:

Step 1: CamStudio

This open source screencast recorder is very simple, very light, free, and does a good job of recording a basic AVI screencast of a portion of your screen. You can set it to record just the part of your screen where your game is running, so you don't need to worry with post-processing crop. I tried a few other tools before this, and all of them had more up-front configuration and none of them really worked well. Either they only recorded black, or they had awful framerates, or they made my whole system crawl.

Just get CamStudio.

Step 2: VirtualDub

Maybe there is another more straight forward tool, but the first I tried (OpenAviToGif) just crashed after the first frame. VirtualDub seems to be for some other features that, frankly, I didn't even pay attention to. But, it has a simple export feature that can convert your CamStudio recorded AVI into a GIF very easily.

Download VirtualDub from their Sourceforge page.

Step 3: Post to /r/GameDev

Just do it, every Saturday. Tons of fun!

and as an extra, here's my first:


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.