The Hostway Blog

Google's Closure Compiler

The Google Closure Compiler is one of the latest tools released by Google in its never-ending quest for enhanced user experience. Unlike iGoogle and Google apps, the Closure Compiler is not aimed at the end user though. Instead it is one of the suite of tools designed to help Web developers create faster, cleaner code. That's because Google has started including page speed as one of it's ranking criteria, in recognition of the fact Web users don't want to sit around waiting for pages to download.

What does Google Closure Compiler do?

The Closure Compiler takes JavaScript code and optimizes it. It can be used to check for syntax errors and other “common JavaScript pitfalls,” and to condense the code. There are three levels of compression:

  • WHITESPACE_ONLY will remove whitespace and comments.
  • SIMPLE_OPTIMIZATIONS the default setting, renames local variables as well as whitespace and comments.
  • ADVANCED_OPTIMIZATIONS will further compress code by renaming symbols and global variables, but could break scripts which are spread across multiple files unless all code is compiled together and other steps are taken to ensure calls to third party scripts are protected.

Thus, the Closure Compiler, when used with the Closure Library and Closure Templates are designed to “create and maintain efficient JavaScript code that downloads quickly and works across different browsers.”

In effect the closure tools allows you to piggy back on Google's extensive JavaScript library to develop your own Web applications.

How can I use the Closure Compiler?

It can be run from the User Interface (UI) at http://closure-compiler.appspot.com/home, or by connecting a Web page of your own directly to the application programming interface (API). See Getting Started with the API and Closure Compiler Service API Reference to get started compiling your own code. You can also download the Closure Compiler Application to run on your own computer as a java command line utility.

Issues with Closure Compiler

Although the Closure Compiler turns out cleaner more efficient code, there have been concerns raised among Web developers that it may deter new developers from learning about coding by viewing the source code of web pages and de-constructing the scripts, as discussed on webmonkey.

The problem is where human developers tend to name variables in a self explanatory manner, to make keeping track of them easier, such as “UserName”, the Closure Compiler strips these names out replacing them with a single letter “a” making the output less human friendly. Where the ADVANCED_OPTIMIZATIONS option is used and function names and symbols are replaced with shorter code, the task of deciphering them becomes even harder for the inexperienced but curious coder. It's a trade off. Machine friendly, faster code is inherently less human friendly.

That's where the Closure Library and Closure Templates come in. New developers can download the full suite of closure tools and follow the tutorials to learn how to build applications cleanly. Experienced developers can exploit the extensive library to improve their programming and use the closure compiler to bring their own JavaScripts in line with Google's own coding practices. Perhaps the hardest adjustment will be for intermediate programmers who have already learned some coding skills from viewing other developers' source code, but have yet to really “get” coding from the ground up.