Jimmy Breck-McKye

A lazy programmer

Octopress Makes Blogging About Programming a Cinch

Are you a programmer? Do you want to blog about your ideas, but want it to be as simple as possible? Octopress might be the tool for you.

As a software engineer working on a particular problem or technology, you probably find yourself making observations about what does and doesn’t work. But what happens to these insights? Writing them down in blog format can help consolidate your ideas, and it allows you to show your engagement to future potential employers. I want to talk a little about the blogging solution that I use, Octopress, and why I think it’s a great choice for software engineers in particular.

Using jsPerf: A How-to Guide

If you’re interested in testing the performance of a particular piece of JavaScript, you might be interested in jsPerf.

jsPerf allows you to run and compare the speed of arbitrary snippets of JavaScript against a blob of HTML of your choice. This means you can test not just pure JavaScript, but DOM manipulation and jQuery calls too.

Slideshow: Breaking the 1000ms ‘Time to Glass’ Mobile Barrier

I found a nice slideshow by Google’s Ilya Grigorik a few weeks ago, taking an overview of the essential issues in optimizing initial web page render time. The talk was mobile-oriented, but many of the same concerns apply to desktop sites. You can find the slides on Ilya Grigorik’s blog

Some key points:

  • 4G is not a ‘magic bullet’ for mobile performance problems. Our customers are going to be stuck with 3G for quite some time, and TCP-slow-start imposes harsh bottlenecks.
  • Waking up the radio on mobile devices is slow. Radio is typically turned off after idling for 100ms; subsequent requests suffer a significant warm-up penalty, even on 3G (where it can be up to 2.5 seconds)
  • Rendering is blocked as the browser constructs the DOM and CSSOM. Inlining ATF (above-the-fold) styles and bootstrapping further styles asynchronously may be a useful strategy
  • Inlining styles for above-the-fold content might be a wise strategy

Protected Members in JavaScript

If you’ve done much OOP in JavaScript, you can probably already simulate private member variables, by putting variables in a constructor’s closure (if not, go read this summary by Douglas Crockford). But you may not know that you can also emulate the protected status of C++ and Java — variables shared between parent and child classes, yet not exposed as public. This is occasionally useful, but is a little obscure to implement in JS if you don’t know how.

The basic idea is use the ‘parasitic constructor’ pattern — where a child constructor directly calls the parent constructor — and passing in an object defined in the context of the child constructor. The parent constructor decorates this object, and because JavaScript object arguments are effectively passed by reference, the members of this object are visible within the child constructor. As such, your child and parent constructors have a ‘shared secret’ object that effectively acts as a map of all the protected members.

JavaScript Function Variables Don’t Have to Be Anonymous

If you’ve used JavaScript much, you’ll probably know one of its very convenient features — the ability to assign functions to variables, send them as arguments, and return them as values. This lets you write code like this, where functions themselves return other functions:

Anonymous function bonanza
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
   This code creates a basic 'curry' function 
   (see my earlier post for an explanation)
   It relies heavily on function variables.
*/

var add = function(x, y) { // <- A function variable
    return x + y;
};

var curryUtil = {
    curry : function(functionToCurry, x) { // <- A function object property
        return function(y) {               // <- A function return value
            return functionToCurry(x,y);
        };
    }
};

var add7 = curryUtil.curry(add, 7);
add7(3); // returns 10

var double = curryUtil.curry(function(x,y){ // <- A function literal argument
   return x * y;
}, 2);
double(8); // returns 16

The problem with this, however, is that all the functions in the above example are anonymous — they aren’t named. Beyond making your code more obscure than it needs to be, this also makes debugging them a problem

Why I Like Parasitic Inheritance

When you’re starting out with JavaScript, it doesn’t take long for the question of object oriented programming to raise its head. As soon as you want to build anything non-trivial you’re going to want some means of structuring your solution and imposing some kind of abstraction on your code. Most tutorials these days emphasise use of the prototype chain – either through function.prototype or the ECMAScript 4 Object.create() interface. I, however, am still fond of the more basic parasitic inheritance approach – where inheritance is basically a special case of composition – and I wanted to outline my reasons why.

JavaScript Curry

If you’ve had much exposure to functional programming, you might have heard of ‘currying’. This funny term refers to a kind of functional composition: taking a function that takes two (or more) parameters, and turning it into a function that takes less parameters, and automatically applies the other arguments, which have been ‘baked’ into it.

For example, let’s say we have an ‘add’ function – function add(x,y) { return x + y }. I might have an instance where I want to add various numbers to a constant – say, 7. Rather than constantly calling add(7, x), currying lets me create an ‘add7’ function. Whatever I pass, add7 always adds seven to it.

Document Loading and DOM Lifecycle Events

Document loading and load events can be a bit confusing for newcomers. Multiple names for the same basic things, incompletely documented, ambiguously explained and those ever-present browser inconsistencies don’t exactly help. I want to try and remedy this by providing a rundown of document loading lifecycle events.

Disabling Firefox Safe Mode

A couple of weeks ago I faced a problem where Firefox’s “Enter Safe Mode” dialog was causing problems for a Selenium test suite I was trying to run.

Basically, Firefox would sometimes throw the dialog and my test framework (naturally) wouldn’t be able to dismiss it without my help. This meant the Firefox instance stayed open, so if I tried running the suite again, I’d get an exception:

1
org.openqa.selenium.WebDriverException: Unable to bind to locking port 7054 within 45000 ms

Annoying. To me, it seemed the simplest solution was to merely disable Safe Mode entirely, as I would never need it during a test. Rummaging around in Stack Overflow and the Mozilla bugtracker, I found this ticket that proposed I could either:

  • set an environment variable, MOZ_DISABLE_SAFE_MODE, or
  • pass a config setting, maxResumedCrashes = -1 to about:flags or a user profile

According to the webdriver documentation, you can set a custom user profile flag fairly easily with profile.setPreferences("key","value")

Disabling Safe Mode was simple:

1
profile.setPreferences("toolkit.startup.max_resumed_crashes", "-1");

If you’ve got the same problem, you might want to give it a try.

Introducing Web Accessibility

A couple of weeks ago I gave a presentation, “Introducing web accessibility”, and I thought it might be worth sharing with the wider world. It provides a broad overview for developers who’ve never encountered accessibility before.

Some key points covered:

  • Visually impaired users use ‘normal’ browsers with CSS and JavaScript usually enabled. Don’t rely on your non-JS/CSS fallbacks for accessibility.
  • Assistive technology sits on top of browsers; it doesn’t replace them. So you cannot track / UA sniff users who need concessions.
  • How semantics encoded in your document actually reach the screen reader, and the points of failure in this journey
  • How screen reader users typically traverse a document
  • Common problems and their solutions
  • How to actually validate accessibility, and the importance of testing

You can get a hold of it here.