unassert - Encourage Design by Contract (DbC) by writing assertions in production code, and compiling them away from release.
Takuto Wada
2015/11/07 @nodefest Tokkyo 2015
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
unassert - encourage reliable programming by writing assertions in production
1. unassertEncourage Design by Contract (DbC) by writing assertions
in production code, and compiling them away from release
Takuto Wada
Nov 7, 2015 @nodefest Tokyo 2015
8. 'use strict';
var assert = require('assert');
function add (a, b) {
console.assert(typeof a === 'number');
assert(!isNaN(a));
assert.equal(typeof b, 'number');
assert.ok(!isNaN(b));
return a + b;
}
before
src.js
10. 'use strict';
var assert = require('assert');
function add (a, b) {
console.assert(typeof a === 'number');
assert(!isNaN(a));
assert.equal(typeof b, 'number');
assert.ok(!isNaN(b));
return a + b;
}
after
dest.js
13. In internal / private methods, we
check preconditions with assert,
helping us catch mistakes within the
library.
For performance, these checks are
removed from the production build
with unassertify.
CONTRIBUTING.md
where / when unassert
14. I don't think we should use this
primarily for type checking, but for
preconditions -- parts of API
contracts that cannot be statically
checked.
@jfirebaugh
why unassert
15. Flow & TypeScript are compile-time,
and preconditions could be runtime.
Mapbox GL JS is pretty minimal in
terms of transforms - envify, brfs, glify
- such that all JS is vanilla.
@tmcw
why not flow / typescript
19. If you promise to call routine with
precondition satisfied then I, in
return, promise to deliver a final
state in which postcondition is
satisfied.
Design by Contract (DbC)
20. •redundant checks can and indeed will hurt
•simplicity becomes a crucial criterion
•complexity is the major enemy of quality
•whose responsibility it is to enforce it: the
client's, or the supplier's.
Design by Contract (DbC)
21.
22. switch (name) {
case 'foo':
doSomething();
break;
case 'bar':
doAnother();
break;
default:
assert(false, 'Cannot be here');
}
Not only preconditions
23. add(path) {
assert(path instanceof PathElement,
`path should be PathElement but was: ${ typeName(path) }`);
this.paths.push(path);
assert.deepEqual(this.paths, sortedPathElements(this.paths),
'this.paths should always be sorted');
}
Not only preconditions