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: