Read my book

I wrote books about Webpack and React. Check them out!

Thursday, September 29, 2011

Invariant Checker for JavaScript

I just dug out something interesting from my archives. It's an invariant checker I developed during my previous, big JavaScript project. It is highly useful for validating arguments provided to a function. Sometimes you are aware of these kind of constraints so why not to enforce them at least during development? This concept may be combined with my chaining assert library (blog post).


Given code speaks louder than words, let's take a look at a concrete example:

There are a few things to note. First of all, note how we check specific arguments, namely from and to. I've defined two invariants for these two. Given the definition is just a list of objects, I could have easily refactored the example so that these invariants are generated by a function. The current solution should give you a good idea of the basic scheme, though.

Each invariant contains some logic (ie. the invariant we want to check) and an error fragment. That fragment is used together with an error prefix string passed to checkArguments. If some invariant fails, it will output a warning based on this information and finally return true value to signify that some invariant failed.

Whether or not this is a good solution is debatable. I guess on retrospect I should have used exceptions instead. That should be pretty easy to change if you favor that kind of solution, however.


I've listed the implementation below. There are a few bits you might want to tweak to suit your tastes. Overall the code is very simple, though.

Array Index Checker

In this specific project I needed to check out array indices specifically. I developed a special function for this. It relies on checkArguments and gives yet another idea how to use it in practice. Here's the implementation:

You could use it like this for instance:


I hope someone finds this little snippet handy. It can be useful to define some extra constraints especially given JavaScript code tends to be pretty loose by definition. I'm not saying this can replace testing altogether. At least this kind of utility should complement it somewhat.