Dynamic method creation in Objective-C
In the book [The Pragmatic Programmer][1], the authors suggest that all method inputs should be validated.  This allows problems with a method to be caught early and their sources traced easily.  

In my Mac application, I accomplished this by creating an `Assert` class.  This class has several class methods.  These methods determine if some precondition is met, and if it is not, then an exception is thrown.  A typical assertion might looks something like this:

    -(void) setWidth: (int) theWidth {
        [Assert integer: width isGreaterThanInteger: 0];
        width = theWidth;
    }

This works really well, and significantly reduced the amount of time I've spend bug hunting.  However, I've noticed lately some of the assertion methods are very useful as predicates.  For example, my `integer:isGreaterThanInteger:andLessThanInteger:` and my `stringIsNotEmpty:` methods are equally useful.  To this end, I created a second class `Predicate`, which I filled with several of my more useful predicate methods.  So I took the logic from the assert methods, and moved it into `Predicate`, and then rewrote my `Assert` methods like the following:

    if ![Predicate predicateMethod]
        throw exception

This has turned into a maintenance nightmare.  If I change the name of a method name in `Predicate`, I must also change it in `Assert` to stay consistent.  If I update the documentation of an `Assert` method, then I must do the same to a `Predicate` method.

Ideally, I would like the reconstruct the `Assert` class so that when any method is called on it, it intercepts the selector.  The `Predicate` class can then be checked to see if it responds to the selector, and if it does, the method is called on `Predicate`with the same arguments that were passed into the `Assert` method.  If the `Predicate` method returns false, then an exception is thrown.

Is there a way to do this in Objective-C?

Thanks.

  [1]: http://pragprog.com/the-pragmatic-programmer
Dynamic method creation in Objective-C
===

In the book [The Pragmatic Programmer][1], the authors suggest that all method inputs should be validated.  This allows problems with a method to be caught early and their sources traced easily.  

In my Mac application, I accomplished this by creating an `Assert` class.  This class has several class methods.  These methods determine if some precondition is met, and if it is not, then an exception is thrown.  A typical assertion might looks something like this:

    -(void) setWidth: (int) theWidth {
        [Assert integer: width isGreaterThanInteger: 0];
        width = theWidth;
    }

This works really well, and significantly reduced the amount of time I've spend bug hunting.  However, I've noticed lately some of the assertion methods are very useful as predicates.  For example, my `integer:isGreaterThanInteger:andLessThanInteger:` and my `stringIsNotEmpty:` methods are equally useful.  To this end, I created a second class `Predicate`, which I filled with several of my more useful predicate methods.  So I took the logic from the assert methods, and moved it into `Predicate`, and then rewrote my `Assert` methods like the following:

    if ![Predicate predicateMethod]
        throw exception

This has turned into a maintenance nightmare.  If I change the name of a method name in `Predicate`, I must also change it in `Assert` to stay consistent.  If I update the documentation of an `Assert` method, then I must do the same to a `Predicate` method.

Ideally, I would like the reconstruct the `Assert` class so that when any method is called on it, it intercepts the selector.  The `Predicate` class can then be checked to see if it responds to the selector, and if it does, the method is called on `Predicate`with the same arguments that were passed into the `Assert` method.  If the `Predicate` method returns false, then an exception is thrown.

Is there a way to do this in Objective-C?

Thanks.

  [1]: http://pragprog.com/the-pragmatic-programmer