Building simplicidade.org: notes, projects, and occasional rants

SAF

SAF is a temporary name for one of my projects.

I’m a big fan of event-driven programming, usually found in GUI programming. I always wanted to use it in systems or application programming.

The idea behind SAF is this: you take a system or application and split into several modules. Each module exports a specific API. The API is then implemented by each module as a event thats sent to a channel. All the modules hook up into that module, and can get involved in the processing of the event.

Let’s see a simple example: imagine a user registration module and a comment system module.

Each user has a username, password and karma level. Karma starts at 0. It increases with good comments, and decreases with bad ones. The karma level of each comment is taken from a average of ratings done by other users of the comments.

You’ve seen this in a lot of places, slashdot probably being the most famous one.

To implement this with SAF, we would build a user registration module, with common methods like create user, login and change password. This module would also listen to “karma changed” events.

The comment module, would accept new comments, either fresh ones, or replies to other comments. It’s only logical that it would generate events for each of those operations. It would also allow one to rate other peoples comments. That API, rate_comment, would send a event “karma changed” whenever a rate_comment event is completed successfully. The event would include previous and the new karma levels, the author of the comment, and the user that did the rating.

The system would work very well. One important aspect of this is the possibility of evolution without the need of complex changes everywhere. If we decide to give a karma reward for people who rate a lot, we don’t need to change the comment module, only the user module.

Also, if you decide to allow the possibility to request that reply’s to my comments are sent to me via email, or a notification of that reply is sent to me via Jabber, the only thing I really need is to add a new module, comment notification, with an API “request notification”. This module would listen to events, and trigger the appropriate notifications whenever a proper reply is seen to a post from a user who requested notification.

This is possible because the modules are not tightly integrated with each other. The SAF architecture can grow as we evolve our system. Sometimes we need to go back to a previous module and add another event generated under certain conditions, but those changes are typically very simple, and unlikely to introduce many bugs.

The system “forces” you to split your problem in manageable pieces, and to take a systematic approach to the API each one will provide.

I already used this system in some applications. It works pretty well. I’m now cleaning the code, adding some documentation, and uploading to CPAN. I expect this to happen in the next few days. Expect a 0.1 release sometime this week. The full version 1.0 will follow in June probably, with full documentation and tests.

Right now, I don’t have any plans post-1.0.

A arch archive will be available sometime tomorrow. I’ll post details then.