Monday, May 9, 2011

Scrappy code sample

Here's a very short and simple code sample that I feel hi-lights my coding style and design philosophies. It's taken from scrappy, an IRC bot that a friend and I have hacked around on for several years.

It's part of the event handling framework for the core bot. When one of the bot's modules wants to register with a new event (like registering an action to be taken when someone types a bot !command in an IRC channel), it simply calls bot.register_event() and passes the type of event to hook on to (msg, CTCP, join, etc), the name of the function(s) to be called for that event, and a reference back to the calling module.

Whenever the bot's IRC socket handles an event, the appropriate function for that event type is called, and a new thread is spawned for each module-registered event function. You may recognize this as being very similar to the observer design pattern.

(To view this without scrolling, visit

Here's why I chose this sample:

  • It's simple. The code is short and sweet, and it's both easy to understand and simple to write.

  • It's intuitive. This is the most logical and "common sense" way to call a series of event-based functions. It's similar to the way your brain would handle such a task.

  • It's elegant. The code is formatted nicely, and the source is easy to read.

  • It's user-friendly. Any user-written module easily interfaces with this via the register_event() function, and the different parts of the message are passed on to the called module in a nice package (dict) that other programmers can easily use.

  • It's pythonic. No code is reinvented. It uses the "batteries included" thread module to do the "heavy lifting" of spawning multiple threads. The thread module automagically handles all of the necessary locking and signaling associated with threading. It also handles KeyboardInterrupt and sys.exit() for you, ensuring that a thread cleanly finishes before the program terminates. Also, if one of the called functions should fail or raise an error, it is cleanly handled in the "background" and the rest of the bot will continue running without interruption.

So there you have it. A code sample which reflects my design philosophies of being simple, clean, elegant, and taking advantage of any system/language libraries available to the programmer.

No comments: