### When declarative programming does not make sense

by Forrest Sheng Bao http://fsbao.net

Pretty much nothing we have created can make sense everywhere. It's very hard to make a universally useful tool.

The term of declarative programming has been out there for many years ago. Its idea is to only tell a computer what you want without how to do it. The compute is supposed to just give you want you want like a black box. Some has become successful, like HTML, SQL, LaTeX or Prolog.

This idea sounds really cool, eh? Hold on one second. If we just need to declare what to do, then why do computer scientists spend so much time on developing algorithms? Why do we spend time on studying Quicksort or FFT?

Oh, yes, HTML, LaTeX or SQL are not designed for these jobs. All right. Let's narrow down.

One subset of declarative programming is called logic programming. Prolog is an example of logic programming languages. I am quite aware that logic programming is not popular at all when we have imperative ways to solve the same problem.

Why haven't a lot of programmers embrace the declarative programming idea?

Reason 1: It's slow.

Logic programs are solved using search algorithms (I guess this is the only way you can do it.) and the search in most cases takes exponential time w.r.t. to the problem size. Just think about traversing a binary tree. Then why would you do that if a problem has a faster algorithm?

For example, sorting. Of course you can use search to find the result. Just prepare all combinations to order given numbers and then check all combinations until you find the sorted one. The time complexity is O(n!). Isn't that much slower than O(nlogn)?

In such a case, using declarative programming does not make sense.

Reason 2: It's hard to use.

A world without loop? Sure, because you don't have to tell the computer how to do and loop is one kind of how-to-do. Instead, you are forced to use recursion because that is declarative. How to recursively do matrix multiplication? I would prefer the for-loop version.

I don't mean a life without loop is sad. But human beings do not think in declarative ways when solving many problems. We prefer step 1, step 2, step 3, .... If programmers don't like that, there is not need to develop a new programming language or paradigm to make them change the way they think. Microsoft Windows sucks, but a lot of people get used to it and therefore Microsoft is still keeping Mac OS or Linux from being mainstream desktop OS. They will give up Microsoft Windows only if [(1 and 3) or (2 and 3)]:
1. Windows sucks too much and they can't tolerate any more
2. Mac OS or Linux is good and they can't resist
3. the cost of transition is affordable or worthy
Then the question is, in what case we shall use declarative programming.
I don't know. I am still thinking. Come back some time later.