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.
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.
|Amphibian.com comic for 3 November 2014|