Monday, November 3, 2014

What do SQL and XSLT Have in Common?

Besides both making me angry and frustrated, what do SQL and XSLT have in common? They're both declarative programming languages.

Declarative languages express the logic of a computation without describing its control flow. This simply means that you tell the computer what you want the end result to look like instead of telling it how to perform the steps.

As I said, both SQL and XSLT are declarative languages. But so is HTML. I usually don't think about HTML as a program, but it is a set of instructions which tell the browser what to display. There's no way to express any kind of interaction with those components in pure HTML, which is why web development almost always includes JavaScript in addition to the HTML.

Imperative languages, by contrast, are concerned with the "how." In languages like C, C++, and Java, you are typically creating algorithms. You give the computer a series of steps that it needs to perform. If you want to display some text on the screen, those steps might be very difficult depending on a variety of factors. Compare that with "<p>Here is some text</p>" in HTML.

I won't tell you that one is better than the other. You should always pick the right tool for the job. But I will admit that I started programming in imperative languages long before I ever tried to use a declarative one, and the difference was often frustrating. Especially because languages such as SQL actually allow some imperative-style structure to sneak in. That enabled me to do things "wrong" because the wrong way was more familiar, and just set me up for total failure later when I tried to use XSLT.

Regular expressions are another example of a declarative language. Also another example of a language that I can never seem to get right, but I did find this really awesome tool for creating and testing them. I would highly recommend it next time you need to come up with one. comic for 3 November 2014