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

Analyzing code for hidden errors

Let's suppose that you have the following JavaScript:

// src/Ch01/Ch01_Demo.js
function makePerson(id, name) { return {id, name}; }

A lot of things can go wrong with the preceding code; they are as follows:

  • The caller can pass in nulls or undefined values as arguments
  • The caller can pass in unintended types of arguments
  • The caller can manipulate the returned person object any way they like, for example, they can add or remove properties

In other words, this code doesn't prevent a number of potential errors. In JavaScript, we have linters, such as ESLint (https://eslint.org/), that check for a lot of possible errors, but you have to remember to find them, enable them, and then work around their limitations. A linter can be helpful in various other ways, such as by pointing out the recommended best practices in a coding style. However, linters in JavaScript are often re-purposed to perform static type checking tasks as well; because they offer so much flexibility and need to be configured (in fact, people usually upload their preferred sets of configuration for different styles of programming), there may be large differences in what exactly gets checked across different codebases.

主站蜘蛛池模板: 正镶白旗| 宜宾县| 晋宁县| 泰州市| 凯里市| 新田县| 沙坪坝区| 当阳市| 惠州市| 陆河县| 岐山县| 株洲县| 安徽省| 毕节市| 阳东县| 和田市| 密山市| 巩留县| 平果县| 且末县| 丰台区| 利川市| 竹溪县| 临猗县| 永定县| 固原市| 安平县| 靖安县| 平昌县| 东乡族自治县| 唐河县| 泸定县| 府谷县| 陕西省| 陆良县| 桂阳县| 昌邑市| 墨竹工卡县| 四子王旗| 西乌珠穆沁旗| 衡山县|