Tag-Oriented Programming (TOP)
Tuesday, 26. May 2009, 05:13:47
Object-Oriented Programming (OOP) has one main and big disadvantage. Application object hierarchy is mostly static and must be (well) developed at design time. Even though it reflects the initial problem very well, it's inflexible for future problem changes. In other words OO isn't change-oriented. In real world not many things work that way. That's why I prefer procedural or functional over OOP.
But if we add some sugar to OOP and make objects somehow not strictly connected to the program logic flow, we may overcome that OOP limitation. Let's call it TOP (Tag-Oriented Programming). I found only one mention of this term on Internet, and it was in the project called AJAXTOP. The idea is similar, but not the same. Please forgive me for using this term for my needs.
As it's known, procedural is about Verbs: i.e. doSomething(noun, noun), and OOP is about Nouns, i.e. Something.do(Noun). Now we will try to take advantage of both these camps.
Suppose we have to create an Invoice. Let's define the process (or task) of invoice creation as 'createInvoice'. In p/f (procedural/functional) world there would be a long process, consisting of 100s lines of code and a number of function calls within. Suppose we have:
function createInvoice(var, var, var, ...) {
prepareForm(var);
...
importOrder(var);
...
checkWithInventory(var);
...
addClient(var);
...
sumTotals(var);
...
printInvoice(var);
}
There we have 5 different actions, which may or may not be useful in other processes.
In OOP world there would be 5 objects probably created to supply inteface for these 5 actions:
class Form; class Order; class Inventory; class Invoice; class Client;
These objects may also be used for other tasks. Then the process of invoice creation could be programmed as follows:
...
Form.prepare(var);
...
Order.import(var);
...
lambda = Order.checkInventory(var) {
Inventory.parse(var);
...
}();
...
Client.add(var);
...
Invoice.sumTotals(var);
...
Invoice.print(var);
...
Everything is fine until it must be changed. In both cases changes must be done within the process. Could it be some way to make changes without altering the process code? Yes. Tagging.
In the world of TOP the code of createInvoice could look like follows (in pseudo-Javascript semantics):
object createInvoice(config) {
tags: new Array();
addTag: function() {....},
removeTag: function() {....},
replaceTag: function() {....}
excecute: function(param) {....};
}
Now we add tome tags to the process:
addTag('INVOICE', 'create');
addTag('FORM', 'default');
addTag('ORDER', 'import');
addTag('INVETORY', 'checkOrder');
addTag('CLIENT', 'add');
addTag('INVOICE', 'sumTotals');
addTag('INVOICE', 'print');
....
createInvoice.execute(param);
There also can be an alternative set of tags created, for example:
addTag('INVOICEcreate');
addTag('FORMdefault');
addTag('ORDERimport');
addTag('INVETORYcheckOrder');
addTag('CLIENTadd');
addTag('INVOICEsumTotals');
addTag('INVOICEprint');
We don't mind which set of tags we use and what form of tag logic we would execute. It can be on the client-side, server-side, multiply servers/processors, OPUs (see my previous articles), etc...
Now what about Tags? It might be regular objects, regular functions or whatever regular. These may be either preprogrammed, or created on the fly. It could be even non-existing tag object at execution time, and default or testing/debugging action would be performed in such case. Tags may be freely added or changed at any time, could be read from DB, read from special tag-filesystem and so on. The execution flow order could be any: ordered in some way, semi-parallel, fully parallel.
Now, when we need to change our program logic, we just change the set of tags! However, in case we need to perform some new operation, we'll always have to create a new tag handling object/procedure. But this tag's object logic would be no more so statically connected to an existing object class and our process, as it is in regular OOP.
Tag-oriented programming looks somehow similar to the actors parallel programming model. Maybe I'm not the first coming with this. However, I see not too much speaking about this programming model in a meantime.








