Archives For programming

MozCamp Asia Report

2011/11/28 — 1 Comment

I recently attended MozCamp Asia in Kuala Lumpur, Malaysia. It was a pretty fun and amazing experience. Meeting Mozilla contributors from all over Asia was of course, the highlight of the trip.

I did a talk at the conference called “From Web Developer to Firefox Hacker“.  There were a number of web developers at the conference and I wanted to convey to them that their skills are relevant to hacking on Firefox itself. Lowering the bar to hacking on Firefox is hard to do – and technical, patch-creating contributors are very important to the project. Someone who comes into the community as a newbie from a talk or workshop like this, this year, may become a rockstar next year, you never know.

The questions I kept asking myself in putting together this presentation were “How to start?”,  “Am I covering X or Y in sufficient detail?”,  “Am I scaring potential contributors with too much detail about the Mozilla process?”, and “Did I forget anything important?” There is just so much detail to cover.

A realization dawned on me while putting together these slides: our documentation is very well done at this point. I was impressed with a lot of new MDN content I linked to that did not exist when I started 3 years ago. Slowly we are chipping away at making technical contribution easier. Perhaps what we need are a few more “starting point” type documents, presentations and workshops to kick off a slew of new patches, no matter how small.

The talk went well.  A few new folks pinged me on irc in the days after, and I was invited to Taipei to give the talk as an in-depth workshop. A Chinese translation was also discussed.

Now is as good a time as ever for technical contributors to learn our process and contribute to Firefox. It may not be easy, but it is easier than ever:)

Advertisements

>For ages, I have relied on Douglas Crockford’s excellent supplant() for my string formatting needs in JS. It works great with whatever objects you have already hanging around in your code:

var myObj = { name: "Conan", surname: "The Barbarian" };

"{name} {surname}".supplant(myObj);

// result:

"Conan The Barbarian"

This is great, reliable and all that…

But it is a bit verbose in the cases where you do not already have an object hanging around with the correct properties. I tried to find an existing implementation, but most of them were still too verbose relying on hacks like: “{$1} {$2}” or “$1 $2” or worse.

I want a “printf” or Python-style string interpolation:

#python

"%s %s" % ("Conan", "The Barbarian",)

// result:

"Conan The Barbarian"

That’s what I want! – only for strings, it doesn’t have to be too feature-rich. Eventually it would be nice if it could handle integers and floats etc.

After some questions and answers on irc in #js, I was able to get something rudimentary going. I started with supplant, and added support for arrays and arguments.

Behold! String.printf():


String.prototype.printf = function (obj) {
var useArguments = false;
var _arguments = arguments;
var i = -1;
if (typeof _arguments[0] == "string") {
useArguments = true;
}
if (obj instanceof Array || useArguments) {
return this.replace(/\%s/g,
function (a, b) {
i++;
if (useArguments) {
if (typeof _arguments[i] == 'string') {
return _arguments[i];
}
else {
throw new Error("Arguments element is an invalid type");
}
}
return obj[i];
});
}
else {
return this.replace(/{([^{}]*)}/g,
function (a, b) {
var r = obj[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
});
}
};

Examples:

"{f} {b}".printf({f: "foo", b: "bar"});

"%s %s".printf(["foo", "bar"]);

"%s %s".printf("foo", "bar");

// all of which give this result:

"foo bar"

Let me know if you think of more tweaks for this – or problems. It pains me every time I see concatenation in JS code. Question, which is more efficient? ‘+’ or String.replace()?

Cheers!