react characteristics - declarative programming

The network has a lot of comparison and description of the declarative programming and imperative programming, but most of them are similar, the general is saying, "I tell the computer what to do, but let the computer decide for themselves how to do."

1. imperative programming.

   This programming model is more common, is that we better understand the way it needs to complete a full show every step out, step by step, to direct the computer to do something.

each array = [1,2,3,4,5 ]

was newArray = []

for(var i = 0; i < array.length; i++) {

  var newNumber = array[i] -1
  newArray.push(newNumber)

}
console.log(newArray) //=> [0,1,2,3,4]

Features: Before executing the computer does not know what I will do, why do it just carried out step by step.

2. declarative programming.

  Different from imperative programming, which is characterized by what I tell the computer what to do, but does not tell you how to do it.

each array = [1,2,3,4,5 ]

var newArray = array.map(function(n) {

  return n -1
})
console.log(array) //=> [0,1,2,3,4]

 I just tell the computer that you perform this function like a map, as I want to tell you why I do not, do not tell you specifically how.

3. The idea of ​​contrast.

If you still do not know, let's use it to analyze my cases.

Imperative programming ---------------
var
Array = [1,2,3,4,5 ] was total = 0 for(var i = 0; i < array.length; i++) { total *= numbers[i] } The console.log (Total)//=> 120

declarative programming -----------------------
var array = [1,2,3,4,5] var total = array.reduce(function(sum, n) { return sum * n }); console.log(total) //=> 120
 

Description: Compared to imperative programming, the biggest feature declarative programming is not "if", "for" this kind of control statement, but directly to a function and declare a variable that receives the execution result of the function, let the computer data as a parameter into them perform again on the line.

 

4. Unity

So far, many people say these two programming paradigm is the opposite. I think this is not accurate, not to say that the idea is declarative programming code to achieve as little as possible, it is imperative that as much as possible. This does not rigorous, and that they understand how to do with it?

Look carefully compare the code above, you notice the difference is in red font at if:

Declare a function: 
function the reduce (Array, Total) {
for(var i = 0; i < array.length; i++) {

   total *= numbers[i]
}
return total
}

Imperative programming --------------- var Array = [1,2,3,4,5 ] var Total = 0
var = the reduce Total (Array, Total); the console.log ( Total) // => 120

If you do not see the function declaration, it is not like declarative programming? If we reduce the function prototype to the array, from the code more like a form. Of course, we are not here to discuss the specific function array.reduce () in. Just from features on the code for: declarative programming is a kind of functional programming idea is actually highlights the declarative functional programming.

 

Conclusion: declarative programming is ism, take pure function has been packaged to achieve the purpose, but in fact the number of steps executed not necessarily less than imperative code, sometimes even more, however, because these pure. function has strong stability, so programmers do not have to consider the specific implementation process within. greatly reduces the physical and mental burden.

 

Guess you like

Origin www.cnblogs.com/Shyno/p/11083517.html