Understanding the importance of declarative-first programming

Programming is mainly centered on giving instructions. Most of the time developers exert effort and spend time on giving instructions to a computer using input logic. This is often reflected in the form of page requests, pulling content from databases, and sending the said content to the browser for the user to see or read.

This process, where you basically tell a computer how to do a task is generally known as programming but don’t be mistaken. It’s just the head-side of the coin.

Giving computers instructions is just a facet of programming called imperative programming. There is another facet called declarative programming and some web developers don’t really recognize it as programming… it is.

The maxim of declarative programming revolves around telling a computer what and not how. To be specific, we tell the computer what we want, and we leave the language interpreter to determine the steps or process to be undertaken to accomplish the task.

Now this may sound fictional but this approach can help in building the web of the future that we are all hoping for.

It’s not seen easily

The subtlety of declarative programming is largely a result of how it is. Let’s put this into an example. Say, you asked your mom to make you some lemonade. Of course, if you programmed your mom, you’ll be telling her the process to make a lemonade step-by-step. But you can’t program your mom (you’ll be in big trouble if you try) so what you can do is ask her for the end result and that is to make you lemonade (please). And if your mom wanted to, she would process “I want some lemonade” into steps like getting a glass of water, getting ingredients and so on.

This is what declarative programming is. You basically declare what result you want to accomplish and leave out the logic of the process needed to arrive to the result.

In HTML, if we want to show an image, we’ll just use and <img> tag and the system will be able to understand that and create a process (i.e. pull out the url of the image from the server, determine the location of the image to render, scale it into the screen, and show it to the user).

Of course, we may not see this as programming, but it is. Realizing that using the <img> tag is basically saying to the browser that you need an image and the user agent will takes the steps for you.

One of the other factors that pulls the strings behind declarative programming on the Web is that somehow magically, it just works out of its own. We know HTML and CSS involved a lot of work to implement, and we somehow fail to see them as declarative programming because we see them as simple output instructions.

One of the real world applications of declarative programming is API development. APIs are implemented using declarative programming and so, developers have less stressful experiences using them.

APIs help in such a way that it saves the developer, and the website, from lots of step-by-step instructions in pulling out a result they want. Rather than telling the website the specific steps to yield to an end result, we could just use an API that does the explaining for us. 

Better and more complex

Once we see the real use of declarative programming, it’s difficult and nearing impossible to un-see it. Declarative programming makes the Web work easier and more efficiently.

However, since JavaScript has become more popular, imperative programming has become more and more prevalent. Now, we can use JavaScript to: substitute HTML and CSS, replace SQL for JavaScript-native database(s), and replace APIs with imperative calls via JavaScript functions.

Now this can and might result to the end of declarative languages on the Web as we know them, and their demise might even occur before we see the rise of more developed technologies in the future. As JavaScript becomes popular, so too are its applications and the number of languages it substitutes.

For now, it is easy to ignore the importance of declarative programming. With the popularization of imperative-only JavaScript, it becomes convincing, especially if more and more programmers see the Web as more of a system of connected documents. However, that is not the case. The Web, as we know it, is a collection and connection of systems and here is where declarative programming becomes an important facet of programming.

Realizing this importance will lead us to a better future, computer-wise. Instead of replacing declarative programming languages with easy-to-use imperative programming, we can resort to creating better declarative languages that can be used by imperative programmers. In this process, we will be able to create better systems.

Instead of using JavaScript and visualizing it as the replacement for Java or C++, it’s better to see the language as more of a modern tool that connects the other tools we have.

In the process of putting lines between the details of implementation in declarative programming, programmers will now be able to use imperative languages such as JavaScript, PHP, and Ruby to build better systems and more complex behaviours.

First and foremost

The first step in building a web-based tool is often programming the tool and make it do whatever it is that you want it to do. However, in the philosophy of declarative programming, you can start by defining the language suited for the end results you desire.

Now, you have a few choices to start with. You can start initially with high level format choices or end up with the details of the data structure. You need to make these choices as early as possible for you to improve the way you organize your work, and later, your imperative implementation.

To conclude

Finding success using declarative programming will be a long piece of land to till. There are challenges that await you, roadblocks that are even more difficult to hurdle than those you encounter in imperative programming. You need to be clear and concise with what you do, but you also need to be knowledgeable about imperative programming to easily understand what needs to be declared. It’s a difficult journey, but it’s also rewarding because declarative programming tends to live longer than imperative programming.

So, next time, start adapting a declarative-first mindset and see what languages are available for you to describe your end result. And when worse comes to worst, write your own simple languages. And the moment you understand what you want to create, start it alongside with a language interpreter. And who knows what you’ll end up creating in the long run.

Rudolph is a geek. He loves reading: books, blogs and even nutrition facts found at the back of products. He loves to read interesting internet stuff. Rudolph has written several articles that concern Typography, Wordpress, Freelance Lifehacks, Graphic Design and Showcase of Beautiful Web Designs. He also writes poems, movie reviews and he puts them in his blog together with rants and some daily life updates. More articles by Rudolph Musngi
Home CSS Deals HTML HTML5 Java JavaScript jQuery Miscellaneous Mobile MySQL News PHP Resources Security Snippet Tools Tutorial Web Development Web Services WordPress