In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what are the reasons for preferring declarative API design rather than imperative design". In daily operation, I believe many people have doubts about the reasons why declarative API design is preferred over imperative design. The editor consulted all kinds of materials and sorted out simple and useful methods of operation. I hope it will be helpful to answer the question of "what are the reasons for preferring declarative API design to imperative design?" Next, please follow the editor to study!
React is declarative. SQL is also declarative. Do you have anything in common?
> Provided by author
We now have many applications around API. For example, many of the cores of our application code communicate through API. We build applications by layering with understandable abstractions. This layer of abstraction is critical because it helps us solve how our software is written and its problems.
The two popular ways to define API are imperative and declarative. SQL is a declarative query language, while IMS and CODASYL use imperative code to query databases. The most commonly used programming language is required.
However, in this article, one of the main reasons for the wider use of declarative API is that declarative API is used more succinctly and provides better abstraction than imperative API.
Imperative declaration in data model
Before SQL, in 1960, IBM designed the Information Management system (IMS), which has a hierarchical model called CODASYL, similar to the JSON model used in document databases.
This model is called a network model, and it helps to create a data model for many-to-many relationships. Each record will have multiple parents, and the way we mark one record relative to another is to use pointers in the programming language instead of foreign keys.
> Source: a Codasyl-type Schela Forp Natural La;g'uage Miedical Records
If you want to get the value of a specific link, you must traverse from the beginning of the list, viewing one record at a time until you find the one you want.
This way of retrieving data makes it difficult for developers to make any updates or changes because they must track the parents of all relationships and links, or they will not find the data they need.
What is the difference between relational model data queries
The relational model uses another way of querying data. As developers, we will tell the program what to do, not how to do it. Therefore, the query optimizer automatically decides in which order which parts of the query are executed and which index to use.
Tell the program what to do, not how to do it. "
One benefit of the declarative approach is that the query language uses API to abstract the customer's implementation details. This leaves room for optimizing the query optimizer to produce better performance and introduce updated functionality without any query changes.
As more and more computing logic shifts to software rather than hardware, SQL is becoming more and more popular and widely used because of its flexibility.
Let's look at another analogy and illustration of declarative API and imperative API in web browsers.
Examples of Web services
Let's give an example of using declarative and imperative methods to process DOM elements in Web browsers.
In general, using CSS is declarative, while using JavaScript to manipulate DOM elements is imperative. In the following example, you can see why it is better for CSS to query data on Web than JavaScript.
Change the color of the text in the "The one who got away" paragraph on the HTML tag, as follows:
The one who got away
Article1 article2 article3
What I learn from my mistakes
SubArticle subArticle2
With CSS, your code would look like this:
Li.topClass > p {color: blue}
It is relatively simple because it declares the pattern in which we want to apply the blue of the text to the elements in it. Not under the class name topClass
The tag segment does not change the color because it does not match the declaration.
On the other hand, using JavaScript to manipulate the DOM element would look like this:
Let liElements = document.getElementsByTagName ("li"); for (let I = 0; I < liElements.length; iTunes +) {if (liElements [I] .className = = "topClass") {const children = liElements [I] .cildNodes; for (let j = 0; j < children.length; jungle +) {if (children.nodeType = NODE.ELEMENT_NODE & child.tagName = ='P') {child.setAttribute ('style',' color:blue') }
Using JavaScript to manipulate DOM elements, we have to tell the program how to do this. It does not tell the program the abstract concept of what we want to achieve. This code is not only much longer than CSS, but also difficult to understand. Novice developers will need to fully follow the instructions on the HTML page to understand their functional intentions.
In addition, we must keep track of what is set on the DOM node and what is not placed on the DOM node. For example, if the user goes to the next page, we need to know manually how to set color:blue back to black. Also, if the API changes, such as the new feature getElementByTagNameV2 (I'm just doing something), the client will need to rewrite the function because API is closely related to the client's implementation.
On the other hand, defining API- declaratively allows us to optimize browser performance without requiring clients to change any CSS tags they write.
We know that querying data in Web browsers is more flexible and more backward compatible than using SQL in CODASYL.
In practice
In the end, the more abstract the API, the more declarative the API. All low-level API exposes the verbs you want to call, and without the imperative API, we cannot encapsulate the high-level abstract API. In other words, an imperative API must exist to encapsulate an API as a declarative API.
If you want to make API more declarative, put the configuration on API for further abstraction. In general, declarative API will provide you with some fault tolerance, so you don't have to think about certain aspects.
Suppose you want to create an abstraction of the polling value in the queue. You can refer to the polling mechanism by providing the function poll ().
By declaring the command poll (), it only does one thing-poll. If a network problem or failure occurs during this process, it will only throw an exception.
One way to write a declaration for API is to mention the configuration of the function intent. For example, if you want the retry count to be 2 and perform a specific action if it fails. Any specific error handling or functionality that the client needs to resolve can be encapsulated in the configvalue, which allows API to do all the hard work for you.
Close the curtain
We'll see why declarative API is easier to understand and use than imperative API. First of all, we understand the currently widely used query language-SQL, and the query language compared to the previous CODASYL. Then we see how it is easier to query the declaration of a web browser than in the form of a command. Finally, understanding the user's intentions and using configuration to abstract all the logic they need to implement is a great way to transition to declarative API.
At this point, the study on "what are the reasons for preferring declarative API design to imperative design" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.