What if all methods were objects?

What would happen if you wrote code where every method of an object simply delegated to a field? Where every method was essentially a ‘Method Object’ as described in the Refactoring book. What would that be like? Objects would become like mini-packages. Loose aggregations of behaviour collaborating to form a whole like cells in an organism. What would be the advantages and flaws? I haven’t tried it so I don’t know for sure. One advantage I can see is that of testability. If all your methods are objects, testing a private method becomes simply a matter of instantiating its object and invoking it. It would also make it harder to write code with side effects. Multiple return values (ie. methods that modify several fields) might be tricky though. On the other hand this might force more careful thought about where behaviour should be.

Food for thought on a friday…

3 thoughts on “What if all methods were objects?

  1. You should have a look at the Self language! It has named “slots” that can be filled with field accessors, methods or whatever you want. The slots look like methods from the outside of the object.

  2. That’s Smalltalk for you. In Smalltalk, even the basic constructs (if, while, etc…) are objects receiving messages. Interesting concept, but it doesn’t scale in my experience. Objective C kept the same ideas and syntax (messages, slots) but retained the C syntax that we all use and love 🙂

    Also, check out CLOS and Scheme, which present some very interesting variations on MOP’s.


    Cedric

Comments are closed.