I agree with what David said; it usually makes the code harder to read, but 
that also greatly depends on the exposed API of the library.

Assertions are a widely-known, commonly accepted way to do in-method validation.

I quite recently wrote Assert.jsm 
(http://dxr.mozilla.org/mozilla-central/source/testing/modules/Assert.jsm), 
which you can include like this:

```js
Components.utils.import("resource://testing-common/Assert.jsm”);
```

You can also control how assertion errors are reported, so you can make it do 
the things you listed. By default, it’ll throw an AssertionError object, which 
inherits from Error.prototype.

I hope you’ll find it useful!

Have fun,

Mike.

On 15 May 2014, at 11:17, David Rajchenbach-Teller <dtel...@mozilla.com> wrote:

> I remember opening a bug for this ~2.5 years ago. The answer was that
> most of these assertions are so trivial to write that putting them in a
> module would actually make reading the code harder than simpler. I don't
> think the situation has changed since then.
> 
> Cheers,
> David
> 
> On 15/05/14 08:04, ajvinc...@gmail.com wrote:
>> I've been thinking a bit about writing a JSM for validating arguments coming 
>> into a function.  
>> 
>> The first part would be the set of assertion functions: 
>>  * a non-empty string
>>  * a whole number
>>  * a positive number
>>  * a non-negative number
>>  * a function
>>  * instanceof
>>  * typeof
>>  * member of a set
>>  * Custom validation function returning true or false
>>  * etc.
>> 
>> The second part would be how to respond to an assertion failure:
>>  * Throw an Error
>>  * Throw Components.Exception
>>  * Report to JS console service
>>  * Report to a provided Log.jsm logger
>>  * Trigger a debugger keyword
>>  * Custom failure function
>>  * Ordered combinations of the above
>> 
>> Above all else, the module should have a simple API.  Something like this 
>> could be useful throughout the Mozilla codebase, and for addons or custom 
>> XULRunner applications.
>> 
>> Suggestions?  If there's an existing module out there, I'd like to hear of 
>> it before reinventing the wheel.
>> _______________________________________________
>> dev-platform mailing list
>> dev-platform@lists.mozilla.org
>> https://lists.mozilla.org/listinfo/dev-platform
>> 
> 
> 
> -- 
> David Rajchenbach-Teller, PhD
> Performance Team, Mozilla
> 
> _______________________________________________
> dev-platform mailing list
> dev-platform@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-platform

_______________________________________________
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform

Reply via email to