简体   繁体   English

使用断言在node.js中进行防御性编程

[英]Defensive programming in node.js using asserts

Considering that sometimes error messages in node are particularly unhelpful I like to liberally use asserts in my functions so that programming errors are caught asap and I can get a message which in general pinpoints the problem. 考虑到有时节点中的错误消息特别无用,我喜欢在我的函数中自由地使用断言,以便尽快捕获编程错误,并且我可以得到一条消息,通常可以查明问题。

function doSomething(arg1, arg2){
  assert(!arg1, "arg1 is undefined");
  assert(!arg2, "arg2 is undefined");
  assert(!arg1.expectedFn, "arg1 does not have expectedFn");

  arg1.expectedFn(function(blah){
    ...
  }
}

Is this a particularly bad thing to do in node/javascript programs? 在节点/ javascript程序中这是一件特别糟糕的事吗? Does this have an impact on performance? 这会对性能产生影响吗?

(this is more of an opinion than fact post, so take it for what it's worth) (这更像是一个意见而非事实的帖子,所以把它当作它的价值)

Asserts definitely do nothing good for performance, but used in moderation I doubt they have a serious enough of an impact to matter. 断言绝对没有对性能有任何好处,但是在适度使用中我怀疑它们对物质的影响非常严重。 That said, generally speaking good test coverage is preferable to asserts whenever possible. 也就是说,一般来说,良好的测试覆盖率优于尽可能断言。 Also, what asserts you do write should not be redundant to what errors will naturally be thrown at runtime, unless they are particularly opaque. 另外,你写的断言应该不是多余的,因为在运行时自然会抛出什么错误,除非它们特别不透明。

Let's consider a modified (and somewhat contrived) version of doSomething - 让我们考虑一下doSomething的修改版(并且有点人为) -

function doSomething(arg1, arg2){
  var res = arg1.expectedFn(function(blah){
    ...
  }
  return res + arg2;
}

I would argue there's no point in checking arg1 for existence or it contains a function name expectedFn. 我认为检查arg1是否存在没有意义,或者它包含一个函数名称expectedFn。 If either are undefined, the javascript runtime will throw a fairly understandable TypeError that will say exactly what happened; 如果其中任何一个未定义,javascript运行时将抛出一个相当容易理解的TypeError,它将准确地说出发生了什么; the asserts are redundant. 断言是多余的。

However, you may find it desirable to test arg2 here. 但是,您可能会发现在这里测试arg2是可取的。 Suppose it's undefined, and res is a string or number - you'd then end up with "undefined" appended to the string, or NaN returned. 假设它是未定义的,res是一个字符串或数字 - 然后你最后会在字符串后附加“undefined”,或者返回NaN。 Both are fairly subtle errors that can exist for a long time without anyone noticing - if you want it to fail sooner rather than later for debugging purposes, then you may want to add a few asserts to ensure it's the correct type. 两者都是相当微妙的错误,可以存在很长时间而没有人注意到 - 如果你希望它尽早失败而不是为了调试目的,那么你可能想要添加一些断言以确保它是正确的类型。

In general, if you're concerned about rigor, I believe that energy would be better put to use in writing comprehensive tests: writing tests that fully cover the cases when doSomething is called will likely lead to a better, more robust development process than asserts. 一般来说,如果你担心严谨,我相信在编写综合测试时会更好地使用能量:编写完全覆盖doSomething被调用的情况的测试可能会导致比断言更好,更强大的开发过程。 There are a few cases where asserts are appropriate, but they're limited to cases where malformed results can exist for a long time without any direct errors, but still able to cause undesirable side effects. 在一些情况下,断言是合适的,但它们仅限于畸形结果可以长时间存在而没有任何直接错误但仍然能够导致不良副作用的情况。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM