最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS library best practice: Return undefined or throw error on bad function input? - Stack Overflow

programmeradmin0浏览0评论

When coding a library in JavaScript, what is the most standard (friendliest?) way to handle invalid input to a function? My gut tells me that returning undefined is perfectly fine, but is it actually more helpful to throw an error instead? Or does it really not matter?

I could also see returning false, null or even -1, but I don't think those would be as widely expected.

(If this question is too subjective, I'm happy to make it cw.)

When coding a library in JavaScript, what is the most standard (friendliest?) way to handle invalid input to a function? My gut tells me that returning undefined is perfectly fine, but is it actually more helpful to throw an error instead? Or does it really not matter?

I could also see returning false, null or even -1, but I don't think those would be as widely expected.

(If this question is too subjective, I'm happy to make it cw.)

Share Improve this question asked Dec 31, 2011 at 17:30 ThomasThomas 5,8568 gold badges49 silver badges68 bronze badges 2
  • A library ought to be coded with ments that clearly define what the arguments must or may be and what they must not be, along with the type of any return value. Friendliest is to not call a function with any arguments it is not prepared to deal with. – kennebec Commented Dec 31, 2011 at 17:44
  • So it's on me to document the function and on the user to abide by its rules. That suggest simply returning undefined is good enough. But since it takes 1 extra min for me to throw an error instead of dying silently, won't that save hours in debugging time for those who didn't take the time to read the docs? Or is there something inherently negative about being too verbose and throwing too many errors. – Thomas Commented Dec 31, 2011 at 18:09
Add a ment  | 

3 Answers 3

Reset to default 4

I think undefined is fine but keep in mind that:

JavaScript does not have a void type, so every function must return a value. The default value is undefined, except for constructors, where the default return value is this.

So you don't need to explicitly returns undefined. It will be by default.

see http://javascript.crockford./survey.html

Advantages of exceptions:

  • If an error is a rare occurrence (not expected in normal operation), then it may be easier and faster for the calling code to catch exceptions in one higher-level place rather than test return values after every API call.

Disadvantages of exceptions:

  • Catching an exception is a lot slower than testing a return value so if an error is a mon occurrence, then exceptions will be a lot slower.
  • If the calling code is going to check errors on every API call they make (and not catch exceptions at a higher level), exceptions are more of a pain to write around every single API call than just testing a return value.

maybe obvious If you extending an environment - continue their practice as a minimum

Quick answer. If this is javascript in the browser undefined is OK, if it is javascript in the server throw an error.

Improved Let the library user select the behavior as an option, either library global, or on an object by object basis.

发布评论

评论列表(0)

  1. 暂无评论