Declarative programming

As a beginner in functional programming I had trouble in understanding declarative style of programming and how it is different from imperative.

In my eyes they both looked same. My aha moment was when i was reading some comments in reddit and someone said that excel is a declarative programming.

In excel you describe what a cell represents.

=SUM(A1,B2)

In above example we are saying, a cell represents a sum of value in A1 cell and value in B2 cell.

So, in declarative programming we declare what things are instead of saying what things do and how to do it.

For example in javascript,

1. `function x(){}`
2. `var x = function(){}`

Example 1 could be thought of as imperative while Example 2 as declarative style.

At first glance, it looks same and  there is no advantage. You might be right in some situations.These are just a style of programming.

Declarative programming helps us to think about the problem in a different way. Instead of ‘how’ to solve, we can think of it as ‘what’ can we use to solve a problem like a set of tools.

Leave a comment

Design a site like this with WordPress.com
Get started