Recently, I was involved in a discussion about developing a framework. Like a child getting attracted towards fire, we programmers tend to get attracted towards developing frameworks. Unfortunately, in both cases the end result is getting burnt. The difference seems to be that a child learns not to touch the fire once it hurts.
What is in a framework development that makes us tempted to say – we need it? I suppose that when we develop a framework we are trying to second guess the people who uses the framework. We might be feeling a little like God telling others what they should be doing and how. This is not unlike being a PHB.
Do not misunderstand me. I am not saying that we should not develop frameworks. I am trying to convince you that the traditional way of framework development might not be the right way. Read on for few more thoughts.
My first experience with framework development is around 15 years back. Back then I was involved in a banking application – the UI is curses based. Most of the application screens were tedious to develop. I suggested that we develop a framework. The project manager assigned one of the UI screens to me and off I went developing the framework. Looking back, the design of the framework is not bad – just that it is not used in production code. What happened was that the assigned program is a master maintenance function. That is supposed to be used while the initial database is created and then more or less discarded. Each DB (actually it was c-isam) table has a corresponding master program and the framework helped in creating those. But no developer used this framework to develop the actual end user programs.
I followed up this program with a generic menu manager program. I used a beautiful implementation of circularly-linked list to display the menu items. The user can use the arrow keys to navigate the menu. Suppose the user is at the last item in the menu and he presses the down arrow – bingo, he lands up at the first item in the menu. Not only that, the menu handler has options to select a menu item by pressing a highlighted key in the items description (these are called accelerator keys now a days – at that time I did not know the name). I must have spent most of the time developing the cute navigation with arrow and accelerator keys – since the actual program did not take too long to develop. There is only a small catch – it seems most of the time the end users keep the numlock on and hence the arrow keys were not available for use. Since the numpad is used so frequently the users prefer to use menu-numbers to select the items. Fortunately, each menu item is prefixed with a sequential number (00 – 99) and the users are able to use these to select the items. I confess, it took a lot of cajoling and pleading from the project manager for me to spoil the beauty of my design to add this trivial and often used feature.
Moving on, I landed up using MFC for a project. We wanted to implement tabs like the tabs in FireFox. By this time I was managing the project – so I can tell our developers that they should develop such a look and feel. (Incidentally, it seems OWL supports developing such an interface. After seeing a demo program, I am convinced that this is what we want in our application). We struggled and struggled. In the end we implemented such an interface, but MFC actively obstructed every pathway that we pursued in this goal.
I am fortunate to get involved in cygwin project – specifically the X server for windows. At that time I wanted to use a X-server on windows to connect to a linux box, but could not find a free one. Hence, I thought to give a try to develop a X-server. Fortunately, cygwin/X has most of the XFree86 code ported already. It already has Xvfb (X virtual framebuffer server) running on Windows. So, I started looking into the code and see how we can make a Windows X server. It took me around two days to make it work. I believe the effort is reduced because of the clean structure of the code base.
More recently I am looking at Ruby on Rails. It is an excellent framework that is a pleasure to work with. The thing I like most with RoR is its philosophy – rather its consistent application of a philosophy.
With all these (and more) experiences I have developed my own version of how to develop a framework.
Do not develop one
Rather do not start with the development of a framework. The success of RoR is due to the fact that it is extracted out of a working program. If you look at frameworks like MFC/OWL/XLib you can see that they are developed before the application of those frameworks and it shows.
Use common idioms
One of the best frameworks is Unix system call library. A thread runs through most of the calls and it is very easy to memorize them. Look at open-read-write-close idiom – and the number of times it is used through out the library. If at all you need to develop a framework look around for similar framework/libraries and use the common idioms from them.
Use at least in two projects
If you are not using a framework in more than one project, it is not worthwhile to develop it in the first place. Application development is not easy. You do not need to complicate it by additional burden of making it generic also.
Create a framework out of working code
Agile practitioners say not to invest upfront in framework development. You can take it a little further. Do not invest in framework at all till your first application is ready. Then put in the efforts and your best resources to extract the framework.
Make the framework for a niche
Enough frameworks exist that try to please everyone and please none. You do not add one more to that list.
Recently I was reading a blog entry about ruby vs. lisp. One of the active lispers is so railed (pun intended) by the success of RoR that he even started a project “lisp on ladder” or some such silly name. (I am happy with this discussion as it made me look at lisp and enjoy it). IMO the basic point missed out is that Rails is not developed to create a 15 minute demo of creating a blog application. That is the effect of extracting a framework from a working project.