Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
148
rated 0 times [  150] [ 2]  / answers: 1 / hits: 77298  / 9 Years ago, wed, november 11, 2015, 12:00:00

Recently I've been studying a lot about the functionality and the ways to use the Facebook JavaScript library React.js. When speaking of its differences to the rest of the JavaScript world often the two programming styles declarative and imperative are mentioned.


What's the difference between both?


More From » reactjs

 Answers
8

A declarative style, like what react has, allows you to control flow and state in your application by saying It should look like this. An imperative style turns that around and allows you to control your application by saying This is what you should do.



The benefit of declarative is that you don't get bogged down in the implementation details of representing the state. You're delegating the organizational component of keeping your application views consistent so you just have to worry about state.



Imagine you have a butler, who is kind of a metaphor for a framework. And you would like to make dinner. In an imperative world, you would tell them step by step how to make dinner. You have to provide these instructions:



Go to the kitchen
Open fridge
Remove chicken from fridge
...
Bring food to the table


In a declarative world, you would simply describe what you want



I want dinner with chicken.


If your butler doesn't know how to make chicken, then you cannot operate in a declarative style. Just like if Backbone doesn't know how to mutate itself to do a certain task, you can't just tell it to do that task. React is able to be declarative because it knows how to make chicken, for example. Compared to Backbone, which only knows how to interface with the kitchen.



Being able to describe the state reduces the surface area for bugs dramatically, which is a benefit. On the other hand, you might have less flexibility in how things occur because you're delegating or abstracting away how you implement the state.


[#64435] Monday, November 9, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
byron

Total Points: 616
Total Questions: 101
Total Answers: 91

Location: Reunion
Member since Wed, Apr 14, 2021
3 Years ago
;