Back to DaveOpinion

Title:Static vs Dynamic Output
Date:July 10th, 2001
Self Righteous:1
Simply true:9

There is a trend lately, especially in web development, towards creating dynamic output.

This trend is silly.

First, some definitions for clarity:

Static output means that it has already been generated before (and separate) from being viewed.
Dynamic output is generated each time it is viewed.

A simple example of this would be an online photo album. The album changes whenever photos are added (or removed..), and the output is viewed when someone goes to the web site with their browser.

Such a photo album can be generated statically, so that sections of the album are regenerated whenever photos are added, or they can be generated dynamically, such as through a CGI that generates the album output everytime it is viewed.

Now, there are many examples of people using dynamic output, but is it a good idea? There are certainly cases where it is necessary, such as when the data is changing on a regular basis, or when it isn't changing under human power, such as those clock web pages that were so popular when the web was an infant.

Fortunately we don't see many silly web clocks anymore.

Generating output takes computing power. Dynamic output makes more sense initially - someone wants to see something, so you create it for them. Static output requires a rethinking of this process, of unrolling the loop, in a sense, so that generation is separate from viewing. And which technique should we use?

It's a pretty simple decision, actually, and it's unfortunately often ignored. Remember:

static means output is generated when it changes
dynamic means output is generated when it is viewed

So you have to ask yourself two questions:

  1. Is the output viewed more often than it changes?
  2. Is it easy to determine when the output needs to change?
In almost all cases, the answer to both questions is yes.

Consider a few examples:

Online Clock
Static: Create a new web page every time the clock changes (every second/minute)
Dynamic: Insert the clock everytime the page is viewed.

In this case, it often does make more sense to use dynamic code (though not if the page is viewed more often than the clock changes!). To use static, you would need to setup some sort of regular job that writes a web page on a regular basis (question #2 above).

More importantly, I would ask you - why do you want a clock on your web page?

Photo album
Static: Create new pages when you add photos
Dynamic: Generate pages every viewing

Adding photos is a process, whether it is a human downloading some of their latest trip photos, or a webcam saving a new image after a delay.

Regardless, you could add the album creation to the end of this process, and then your photo album will be static and up to date.

More importantly, this would generally save CPU time. If not, I have to ask you why you are updating your photo album more often than people are looking at it?

Photo album with user comments
Some photo albums allow users to add comments to photos. This would seem to suddenly require that the album becomes a CGI script. This is not the case. While CGI is certainly required to process the comments, the CGI script could be a trigger that writes a new static page. This does, however, have the tricky problem of dealing with locks, so that viewers don't see partially completed pages, and so that two comment users don't write over each other. But these lock problems are not difficult to solve.

Online calendar
This would seem to be a sure candidate for dynamic output; however, this is not necessarily true. It's possible to do full dynamic or static calendars, or (probably even better), a mixture of the two. For example, you could regenerate the HTML for a single day when an event is added to that day. Then the week/month/day views could be dynamically generated, but they would consist of simply including the HTML for each day included - very little processing power required. (Regular events certainly cause some difficulty with this, but could be treated as an exception). And while this may be more difficult in some ways than a full dynamic calendar, it will save large amounts of processing power every time the calendar is viewed.