PDA

View Full Version : Design-by-contract in JavaScript


Alex Vincent
05-18-2004, 02:44 AM
If you just want to look at the script itself, ecmaDebug.js (http://weblogs.mozillazine.org/weirdal/ecmaDebug.js)

If you want to look at the rationale,
http://weblogs.mozillazine.org/weirdal/archives/005505.html

This is written mainly for Mozilla-based applications; I don't have an Internet Explorer version (and I don't plan on building one... though you're certainly welcome to. It wouldn't be too hard, I think -- just removing chrome-specific code, like the warn() function). Basically, the idea is to add "sanity checks" for people who write scripts... while at the same time not forcing end-users to suffer ignominously.

joh6nn
05-18-2004, 03:42 AM
that's very impressive

Alex Vincent
05-21-2004, 02:52 AM
Followups:
http://weblogs.mozillazine.org/weirdal/archives/005526.html
http://weblogs.mozillazine.org/weirdal/archives/005533.html

It may be impressive, but really it's almost entirely core ECMAScript...

One piece of advice for users of the script: whenever possible, put your assert() calls into preconditions and/or postconditions. Every function call (including assert()) has a price, and with the ecmaDebug flag disabled, 90% of assertions in your main function are a waste of time.