Declarative programming--more abstract, focus on what, rather than how

Source: Internet
Author: User

CSDN : AngularJS What is the design concept? What does inspiration come from?

Misko : The design philosophy that Angularjs follows is that the build UI should be declarative. This is also the source of inspiration for the Angularjs identifier (directives) idea.

Hence the excerpt from the following: A comparison of declarative programming and imperative programming

To unify the concept first, we have two ways of programming: imperative and declarative.

We can define the difference between them as follows:

    • Imperative Programming : Command "machine" how to do things, so whatever you want, it will follow your command.
    • declarative Programming : Tell the machine what you want (what), and let the machine figure out how to do it .
Code examples for declarative programming and imperative programming

As a simple example, let's say we want to double the values in an array.

We implement it in an imperative programming style, like this:

VarNumbers=[1,2,3,4,5]VarDoubled=[]For(VarI= 0i < numbers. Lengthi++) {var  Newnumber = numbers[i * 2 doubled. Push (newnumber) }console log (doubled) //=> [ 2,4,6,8,10]                 

We iterate through the entire array, take each element, multiply it by two, and then put the doubled value into the new array, manipulating the double array every time until all the elements have been computed.

Using declarative programming methods, we can use Array.map functions like this:

Varnumbers = [1, 2,3,4,5< Span class= "p" >]var doubled = numbers map (function (n ) {return n * 2< Span class= "P" >}) console. Log (doubled) //=> [2,4,6,8,10]    

mapA new array is created with the current array, and each element in the new array is processed by the incoming map function (here function(n) { return n*2 } ).

mapThe thing about the function is to draw out the process of traversing the entire array directly, so that we focus on whatwe want. Note that we have a pure function in the map, it does not have any side effects (does not change the external state), it just receives a number and returns the value multiplied by two.

Is declarative programming strange?

If you have not heard before map and reduce function, your first feeling, I believe, will be like this. As programmers, we are very accustomed to pointing out how things should work. "Go through the list", "If this happens then," and "assign this new value to this variable". When we already know how to tell a machine how to do things, why do we need to learn this seemingly weird inductive pull-out function tool?

In many cases, imperative programming is useful. When we write business logic, we usually have to write imperative code, there is no possibility in our special business There is also a can be summed out the implementation of the extraction.

However, if we take the time to learn (or find) declarative parts that can be summed up, they can bring great convenience to our programming. First, I can write less code, which is a shortcut to success. And they allow us to stand on a higher level to think, to stand in the clouds and think about what we want, instead of standing in the mud and thinking about how things should be done.

Declarative programming Language: SQL

Maybe you don't understand, but there's a place where you might have used declarative programming, which is SQL.

You can think of SQL as a declarative query language for processing data. Write an application entirely in SQL? That's impossible. But it's incredibly powerful if you're dealing with an interconnected set of data.

Query statements such as the following:

SELECT * from dogsINNER JOIN ownersWHERE dogs.owner_id = owners.id

I did not say that SQL is a very easy to understand language, and did not say a glance can see them, but basically still very neat.

SQL code is not only very short, not only easy to read, it also has a greater advantage. Because we're inductive, we can focus on what, and let the database help us optimize how.

Our imperative programming code will run very slowly because we need to traverse the owner of every dog in the list.

In the case of SQL, we can let the database handle How to find the data we want. If you need to use an index (assuming we've built an index), the database knows how to use the index, so there's a big improvement in performance. If it has executed the same query before this time, it may be found immediately in the cache. By letting go of how, let the machine to do these difficult things, we do not need to master the principle of the database can easily complete the task.

Summary of declarative programming

Declarative programming lets us describe what we want, let the underlying software/computer/And so on to solve how to implement them.

In many cases, as we've seen, declarative programming can bring real improvements to our programming, and by standing at a higher level, we can focus more on what, and that's exactly what we 're developing software for.

Transferred from: http://www.vaikan.com/imperative-vs-declarative/

Reference: https://llh911001.gitbooks.io/mostly-adequate-guide-chinese/content/ch6.html# Declarative Code

Declarative programming--more abstract, focus on what, rather than how

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.