Google Blogoscoped

Friday, February 8, 2008

Why Does Google Obfuscate Their Code?

Why do you think Google obfuscates their client side code before serving it to us... like much of their JavaScript or HTML? Here's a quote from a typical JS file of theirs, missing line breaks, self-explanatory variables and so on (which we can assume all do exist in the original Google production code):

function ig_a(a,b){var c=function(){};c.prototype=a.prototype;b.prototype=new c}function _gel(a){return document.getElementById?document.getElementById(a):null}function _gelstn(a){if(a=="*"&&document.all)return document.all;return document.getElementsByTagName?document.getElementsByTagName(a):[]}function _gelsbyregex(a,b){var c=_gelstn(a),d=[];for(var e=0;e<c.length;e++){if(b.test(c[e].id)){d.push(c[e])}}return d}function _uc(a){return a.toUpperCase()}function _trim(a){return a.replace(/^s*|s*$/g, "")}function _esc(a){return window.encodeURIComponent?encodeURIComponent(a):escape(a)}var ig_aa=function(a){return ...

Is this just so Google can save space to cause less bandwidth issues? Or do they try to hide some of their functionality from view, too (even when knowing obfuscation isn't secure either)? Because it seems Google actually benefits from people doing friendly hacks with their code – they even released a Greasemonkey API for Gmail to make such hacks easier. Or think of how the Google Maps API came about, in the words of then-Google employee Pamela Fox (and let's not forget any API also causes increased bandwidth challenges):

I guess started and I think what happened is that somebody reverse-engineered Google Maps. And I think Google just liked it so much that they decided, let’s make it an API. And that’s the way a lot of things happen these days; people out there realizing, “hey, this could be useful for me too,” and then showing how it could be done. And you know, then us making that official.

Imagine if Google would output friendlier client-side code; while generating bigger file sizes, it seems that this way they'd almost deliver a quick & dirty "API" for many of their tools. People would additionally be able to learn from Google's code. And getting developers on board after all isn't an altruistic act; it can make sense even for a commercial company, as these developers then are more inclined to spread the word on Google's services to other users. I'm not saying that this would necessary be worth the price though – what do you think?

Update: Hank in the comment offers another explanation: "This code is not even handwritten... it's compiled from Java-Code that probably looks a lot different." (I wonder how many Google JavaScript files are just output from the Google Web Toolkit?)
And Niraj offers another solution: "If it is in fact, just a bandwidth thing, it'd be cool of them to put out some pages with explanations of what's going on or with the original code [in the] cases of Javascript. Then people could learn the functionality if they wanted to understand/hack it, but they wouldn't be increasing their page sizes." [Thanks Hank and Niraj!]

Update 2: A commenter identifying themselves as Some Googler writes that "99.9% of users don't want to download the comments in the code, it's just a waste of time, and we don't want you looking at them either as they're full of references to internal systems and codenames that aren't meaningful outside the company." Someone by the name of Xoogler in the comments writes, "It's compressed Javascript, from hand-written Javascript ... Few projects at Google use GWT [Google Web Toolkit]."


Blog  |  Forum     more >> Archive | Feed | Google's blogs | About


This site unofficially covers Google™ and more with some rights reserved. Join our forum!