官术网_书友最值得收藏!

Declarative programming

Reading the React documentation or blog posts about React, you have undoubtedly come across the term declarative. One of the reasons why React is so powerful is because it enforces a declarative programming paradigm.

Consequently, to master React, it is essential to understand what declarative programming means and what the main differences between imperative and declarative programming are. The easiest way to approach the problem is to think about imperative programming as a way of describing how things work, and declarative programming as a way of describing what you want to achieve.

A real-life parallel in the imperative world would be entering a bar for a beer, and giving the following instructions to the bartender:

  1. Take a glass from the shelf.
  2. Put the glass in front of the draft.
  3. Pull down the handle until the glass is full.
  4. Pass me the glass.

In the declarative world, you would just say: Beer, please.

The declarative approach of asking for a beer assumes that the bartender knows how to serve one, and that is an important aspect of the way declarative programming works.

Let's move into a JavaScript example, writing a simple function that, given an array of uppercase strings, returns an array with the same strings in lowercase:

  toLowerCase(['FOO', 'BAR']) // ['foo', 'bar']

An imperative function to solve the problem would be implemented as follows:

  const toLowerCase = input => { 
const output = [];

for (let i = 0; i < input.length; i++) {
output.push(input[i].toLowerCase());
}

return output;
};

First of all, an empty array to contain the result gets created. Then, the function loops through all the elements of the input array and pushes the lowercase values into the empty array. Finally, the output array gets returned.

A declarative solution would be as follows:

  const toLowerCase = input => input.map(value => value.toLowerCase());

The items of the input array are passed to a map function that returns a new array containing the lowercase values. There are some significant differences to note: the former example is less elegant and it requires more effort to be understood. The latter is terser and easier to read, which makes a huge difference in big code bases, where maintainability is crucial.

Another aspect worth mentioning is that, in the declarative example, there is no need to use variables nor to keep their values updated during the execution. Declarative programming tends to avoid creating and mutating a state.

As a final example, let's see what it means for React to be declarative.

The problem we will try to solve is a common task in web development: showing a map with a marker.

The JavaScript implementation (using the Google Maps SDK) is as follows:

  const map = new google.maps.Map(document.getElementById('map'), { 
zoom: 4,
center: myLatLng
});

const marker = new google.maps.Marker({
position: myLatLng,
title: 'Hello World!'
});

marker.setMap(map);

It is imperative because all the instructions needed to create the map, and create the marker and attach it to the map, are described inside the code one after the other.

A React component to show a map on a page would look like this instead:

  <Gmaps zoom={4} center={myLatLng}> 
<Marker position={myLatLng} title="Hello world!" />
</Gmaps>

In declarative programming, developers only describe what they want to achieve, and there's no need to list all the steps to make it work.

The fact that React offers a declarative approach makes it easy to use, and consequently, the resulting code is simple, which often leads to fewer bugs and more maintainability.

主站蜘蛛池模板: 西华县| 仪征市| 凤台县| 南皮县| 莱西市| 黎川县| 新宁县| 许昌县| 南召县| 玉林市| 永寿县| 新竹市| 乳源| 杭锦后旗| 读书| 信宜市| 永靖县| 诸城市| 莆田市| 阳泉市| 繁峙县| 吉隆县| 临桂县| 章丘市| 福贡县| 宜阳县| 绥德县| 济源市| 西安市| 新余市| 涿鹿县| 方正县| 芦山县| 长汀县| 荔波县| 南汇区| 玉林市| 靖州| 河津市| 大方县| 齐齐哈尔市|