KillerSites Blog

Cleaner code is better than faster code

May 11, 2008

I can’t tell you how many times that my programming experience (in Java, PHP etc) has guided me in my web design work … and strangely, even in other aspects of my life not at all related to topics ‘nerd’.

… Ah, nerd wisdom prevails in all aspects of life.

🙂

Anyway, here yet again, is another example where programming guides me: this time, it’s all about web design and code.

Over the last few years, the consensus in the web design community has been to streamline code. In real terms, that comes down to:

  • collapsing html
  • collapsing css
  • … and consolidating css code into one file, to minimize the number of server hits.

… The idea is to speed up web page load times and to reduce web server loads.

This is an important goal and something all web designers should be concerned about. The problem is that if you are concentrating on optimizing your CSS or HTML to speed things up, you are concentrating on the wrong parts of your websites.

The fact is, that most of the optimization opportunities is actually found in your images, Flash movies and other multimedia content – not the code.

What is HTML and CSS code collapsing?

In a nutshell:

This is simply removing white space in your code. So you can take this:

Web design rocks!

Web design rocks!

And cut it down to this:

Web design rocks!

Web design rocks!

… The idea is that if you cut down 7 lines of code to just 1 line, the file will be a touch smaller. And this is true.

This old-school technique was used back in the 1990’s when everyone was on slow dial-up Internet connections and everyone had much slower computers.

Collapsing code was once useful, but like the transparent spacer gif trick … it is no longer needed. Actually, it’s more trouble than it is worth.

What can web designers learn from programmers?

When it comes to code, the web design community is still (generally) not nearly as experienced as the programming community. There are still lessons to be learned by the average web designer, lessons that have been fundamental in software development for years.

… Programmers have known for years that when it comes to code, it is better make it cleaner than speedier, because clean code is much easier to read, write and maintain.

So in our example above, seven lines of code are better than one.

What is ‘cleaner’ code?

Clean code can be broken down to:

  • Lots of white space so the code is easy to read.
  • Lots of comments so that you can much more easily understand what is going on 6 months down the road.
  • Code that is ‘chunked’ into logical groups so that (again) it is easy to update etc.

The rise of object oriented programming is the best example of this understanding.

Object oriented code will almost always be slower (when running) than old school procedural programming. Nonetheless, object oriented programming now dominates the programming world and all the major languages use it including:

  • Java
  • PHP
  • Ruby
  • Actionscript

… and many others.

After decades of experience, programmers have learned through hard experience that cleaner code is much better than faster code.

How does this apply to web design?

Well, write cleaner css and HTML that is easier to understand and easier to work with. To repeat the list from above:

  • Use lots of white space so the code is easy to read.
  • Use lots of comments (in your html and css) so that you will be more able to understand what is going on 6 months down the road. This becomes more important when you are dealing with tricky CSS layouts that require seemingly strange markup.
  • Organize your css into logical groups (and pages) so that it is easy to update etc.


Won’t cleaner code slow down my pages?

The fact of the matter is, for the vast majority of websites (%99.99999999999999999) you will not see ANY difference in terms of speed or cost in bandwidth.

… The ‘optimize code to save money’ is a wurban (Web + urban) myth.

Stefan Mischook

www.killersites.com