Declarative programming is a style of programming where applications are structured in a way that prioritizes
describing what should happen over defining how it should happen.
In order to understand declarative programming, Let's compare it with imperative programming (style of programming that’s only
concerned with how to achieve results with code).
Example: making a string URL-friendly. Typically, this can be accomplished by replacing all of the spaces in a string with hyphens,
since spaces are not URL-friendly. First, An imperative approach to this task:
const string = "difference between declarative and imperative in react.js";
const urlFriendly = "";
for (var i = 0; i < string.length; i++) {
if (string[i] === " ") {
urlFriendly += "-";
} else {
urlFriendly += string[i];
}
}
console.log(urlFriendly); // "difference-between-declarative-and-imperative-in-react-js"
In this example, we loop through every character in the string, replacing spaces as they occur. The structure of this program is only concerned with how such a task can be achieved. We use a for loop and an if statement and set values with an equality operator. Just
looking at the code alone does not tell us much as imperative programs require lots of comments in order to understand what’s going on.
Now let’s look at a declarative approach to the same problem:
const string = "Difference between declarative and imperative in React.js?";
const urlFriendly = string.replace(/ /g, "-");
console.log(urlFriendly);
Here we are using string.replace
along with a regular expression to
replace all instances of spaces with hyphens. Using string.replace
is
a way of describing what’s supposed to happen: spaces in the string
should be replaced. The details of how spaces are dealt with are
abstracted away inside the replace function.
In a declarative program, the syntax itself describes what should happen, and the details of how things happen are abstracted away.
Essentially, declarative programming produces applications that are easier to reason about, and when it’s easier to reason about an application, that application is easier to scale. Additional details about the declarative programming paradigm can be found at the Declarative Programming wiki.
Now, let’s consider the task of building a document object model. An imperative approach would be concerned with how the DOM is constructed:
const target = document.getElementById("target");
const wrapper = document.createElement("div");
const headline = document.createElement("h1");
wrapper.id = "welcome";
headline.innerText = "Hello World";
wrapper.appendChild(headline);
target.appendChild(wrapper);
This code is concerned with creating elements, setting elements, and
adding them to the document. It would be very hard to make changes,
add features, or scale 10,000 lines of code where the DOM is
constructed imperatively.
Now let’s take a look at how we can construct a DOM declaratively
using a React component:
const { render } = ReactDOM;
const Welcome = () => (
<div id="welcome">
<h1>Hello World</h1>
</div>
);
render(<Welcome />, document.getElementById("target"));
React is declarative. Here, the Welcome component describes the DOM
that should be rendered. The render function uses the instructions
declared in the component to build the DOM, abstracting away the
details of how the DOM is to be rendered. We can clearly see that we
want to render our Welcome component into the element with the ID of
target
Source: Modern Patterns for Developing React Apps