Monthly Archives: January 2009

Compressing JavaScript and CSS files with YUI Compressor

Performance is always something to be looking for when developing web applications. One possible approach to improve performance of such applications is to compress our JavaScript and CSS files. This will make them smaller and thus faster to download.

A JavaScript compressor could for example turn this:


function helloWorld() {

    alert("Hello World");

}

into something like this:


function helloWorld() {alert("Hello World");}

It gets unreadable, but smaller. When applied to several, potentially large files, this type of compression can help us save a lot of kbytes of download from the server to a client, thus making the page load faster.

Since this is a simple strategy, it’s tempting to write our small routine to apply this compression to our files; but such a trouble is not necessary. There are libraries out there do this for us. We will use the YUI Compressor, from Yahoo, here.

The usage pattern I like is to compress all JavaScript and CSS files right before packaging the application. You can do this manually, and the official YUI Compressor site has instructions on how to do it. But this way you will probably end up forgetting to do so. Since we already use Ant in our projects at IPTI, we also use Ant to automate this compression step.

The first step to use YUI inside Ant is to get the ant task jar file here. Put the jar file in Ant’s classpath and you are ready to continue. PS.: The last time I tried, the YUIAnt site was unreachable. If that is still case for you when you read this, please let me know and I can make the version I have available.

Now, inside your Ant file, declare the yuicompressor task. Something like this:

<taskdef name="yuicompress"
  classname="com.yahoo.platform.yui.compressor.YUICompressTask"
  classpathref="lib.path"/>

and then use it like this:

<target name="minify-js" description="minify the project's JS files">
  <echo message="minifying JS files" level="info"/>
  <yuicompress charset="UTF-8" outputfolder="distdir">
    <fileset dir="srcdir" includes="**/*.js"/>
  </yuicompress>
</target>

Replace all the js by css‘s and you are set for CSS files as well.

Now, you probably have an Ant target to build your application war file – let’s call it dist. Just put minify-js and minify-css (supposing you created this second one) in dist‘s depends attribute. Now, each time you build your application, its JavaScript and CSS files will get compressed!

The last problem is that it doesn’t make sense to do this compression during normal development. The files would be impossible to read and debug properly. The solution again is very simple: add a property to the ant file, that says whether the compression should be executed or not.

This should suffice:

<property name="minify.js" value="true"/>

and add this to the minify-js (and css) targets:

<target name="minify-js" if="minify.js"
    description="minify the project's JS files">

An important detail to notice is that, to disable the compression you must comment the property altogether. Changing its value from “true” to “false” will do no good – actually, the value can be anything, it just need to be there.


Software Quality and Agile

Often when we talk about agile software development, we mention how important quality is. In this post, I’ll run quickly through some thoughts and practices on how agile tries to guarantee this so-called higher quality.

When I first started using agile (eXtreme Programming in my case), one of the things that called for my attention the most was its focus on Accuracy and in avoiding Waste. This means that when developing software using an agile methodology, we always try to build exactly what the client/user want. Nothing less, nothing more. Having developed some things before using methodologies closer to traditional ones, I was urging for someway to stop doing useless work and wasting time (diagrams that would never be touched again anyone?). Agile seemed to be a good bet.

But this all is a little bit abstract. Like I said here, this is just saying that we will try to bring the most value to the client. Now looking into something more practical, there are some interesting things that we can use in a daily basis that also help us improve software quality, among them:

  • Pair Programming: two developers, instead of one, work together to solve problems. I talked previously about this here.
  • Pair Rotation: to avoid that the pairs get “accustomed” to its pair, we can constantly change the pairs that works together. This also helps leveling up the team knowledge and experience.
  • Tests: this is another invaluable practice that should be done in a daily basis. There is too much to sum up in a few lines. I’ll just mention two types of tests: unit and acceptance. We use unit tests to make sure the each individual feature of a system is working, and acceptance tests to guarantee that the whole system is ok, from a perspective similar to what the actual users have.

That’s it for now. I have a few other topics to write about in the pipeline, but if you have any ideas / suggestions for things you would like to see here, please don’t exitate to post a comment!

Bye!


Follow

Get every new post delivered to your Inbox.