简体   繁体   English

我应该多彻底地检查 JavaScript 中可能出现的错误?

[英]How thoroughly should I check for possible errors in JavaScript?

I've difficulties to understand how far should I normally go with checking and validating data I operate in my code.我很难理解在检查和验证我在代码中操作的数据时,我通常应该走多远 go I'm not even saying about user-input data here, I'm just saying about my own data, the data I know the structure of.这里我什至没有说用户输入数据,我只是说我自己的数据,我知道其结构的数据。 For example, I might have some basic function like this:例如,我可能有一些基本的 function 像这样:

let someData = [object];

someFunction(newData) {
  let newSet = newData.frequency[0];
  someData.dataset.chart.data[0].frequency = newSet;
}

Let's say I have someData variable that is used by a chart, and then I also have a function that simply updates the chart data with a new one.假设我有一个图表使用的 someData 变量,然后我还有一个 function 只需用新数据更新图表数据。 newData comes from my database (when user adjust filters and apply them) and someData is the default, initial data everyone sees. newData 来自我的数据库(当用户调整过滤器并应用它们时), someData 是每个人都看到的默认初始数据。 Now, my problem is... I foresee the following events:现在,我的问题是......我预见到以下事件:

  1. Someone might change someData using Developers Console, so this variable will no longer hold an object, or won't have properties I address in my code, so it will lead to errors and break my logic entirely.有人可能会使用 Developers Console 更改 someData,因此该变量将不再包含 object,或者将没有我在代码中处理的属性,因此它会导致错误并完全破坏我的逻辑。

  2. Someone might call someFunction directly (using console again) with very random data as argument, and this, again, will lead to errors.有人可能会使用非常随机的数据作为参数直接调用 someFunction(再次使用控制台),这同样会导致错误。

  3. Maybe newData received from DB will be somewhat wrong due to some errors on my side or anything, or maybe initial someData will fail initialising (cause it's initialised through a function as well and relies on third party.JS file that also might fail to load one day).由于我这边的一些错误或任何事情,可能从 DB 接收到的 newData 会有些错误,或者初始 someData 初始化失败(因为它也是通过 function 初始化的,并且依赖于也可能无法加载的第三方.JS 文件天)。

And I'm not even sure I've foreseen all possible events.而且我什至不确定我是否已经预见到所有可能的事件。 So my code turns from what you saw above to something "tedious" (in my opinion) like this:所以我的代码从你上面看到的变成了这样的“乏味”(在我看来):

let someData = [object];

someFunction(newData) {
  let newSet = typeof newData === "object" &&
               newData.frequency ?
               newData.frequency[0] : undefined;
  let oldSet = typeof someData === "object" &&
               someData.dataset &&
               someData.dataset.chart &&
               someData.dataset.chart.data && 
               someData.dataset.chart.data[0] ?
               someData.dataset.chart.data[0].frequency : undefined;
  // Since using length on undefined will lead to an error, I've to check the type too
  if (Array.isArray(newSet) && newSet.length === 5 && oldSet) {
    oldSet = newSet;
  } else {
    throw Error("Dataset update has failed.");
  }
}

And even this doesn't guarantee that the newSet of data is the data I expect to see, because maybe I was looking for [1,2,3,4,5] and user managed to insert ["a","b","c","d","e"] via his console and so on.即使这样也不能保证新的数据集是我希望看到的数据,因为也许我正在寻找 [1,2,3,4,5] 并且用户设法插入了 ["a","b" ,"c","d","e"] 通过他的控制台等等。 It feels like I can refine this endlessly and it will never be bulletproof plus eventually it's starting to get complicated to understand my own code, that the only thing I wanted to do is to changed old data with the new data.感觉就像我可以无休止地改进它,它永远不会是防弹的,而且最终理解我自己的代码开始变得复杂,我唯一想做的就是用新数据更改旧数据。 At this point I'm feeling like I'm doing it all wrong.在这一点上,我觉得我做错了。 Coding takes way more time and I'm not even sure I'm doing it for good, but at the same time I can't feel the limit when to stop over-validating my code.编码需要更多时间,我什至不确定我是否做得很好,但同时我感觉不到何时停止过度验证我的代码的限制。 Please advise.请指教。

I would just stick with user input validation.我只会坚持用户输入验证。 Out of that, it's their problem if they want to screw you things with developper tools.除此之外,如果他们想用开发工具来搞砸你,那是他们的问题。 Anyway, those would only stay on their side.不管怎样,那些人只会站在他们这边。

What's important is the validation on your server.重要的是在您的服务器上进行验证。 The client side input validation is just to make sure everything put by regular user is error free before processing.客户端输入验证只是为了确保普通用户在处理之前输入的所有内容都没有错误。 It also save useless send to server.它还节省了无用的发送到服务器。 The server must redo the validation and more because of those screwed people.由于那些搞砸的人,服务器必须重做验证等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在运行时检查javascript或html错误? - How do i check javascript or html errors at runtime? 如何使用capybara和poltergeist检查Javascript错误? - How do I check for Javascript errors using capybara and poltergeist? 我应该如何检查jasmine或javascript中的已排序语句 - How should I check the sorted statement in jasmine or javascript 如何彻底删除JQPlot图表 - how to thoroughly remove JQPlot Chart 缺少分号时,是否可以检查带有节点的JavaScript文件并获得错误? - Is it possible to check JavaScript files with node and get errors when a semicolon is missing? 我应该如何处理Firebase登录错误? - How should I handle firebase login errors? 如何仅从命令行检查 JavaScript 代码的语法错误? - How can I check JavaScript code for syntax errors ONLY from the command line? Javascript如何检查多个等待异步mssql DB请求结果中的错误? - Javascript how do I check multiple await async mssql DB request results for errors? 如何在JavaScript中制作最快的自底向上树形转换器? 我应该自己管理内存吗? - How to make the fastest possible bottom-up tree transformer in JavaScript? Should I manage memory on my own? 我无法处理JavaScript冲突。 您应该如何检查它们? - I can't get my JavaScript collisions to work. How should you check for them?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM