Tuesday, May 15, 2012

Occamsrazor.js: A Javascript component registry


Occam's Razor
This principle is often summarized as "other things being equal, a simpler explanation is better than a more complex one."
http://en.wikipedia.org/wiki/Occam%27s_razor


I am a Plone developer. For those who do not know, Plone is a powerful CMS based on the Zope application server (written in Python).

One of the best features of this CMS is the Zope Component Architecture (ZCA). It borns as a part of the Zope 3 project but It's a quite independent set of modules. ZCA allows you to build component based application.


I really missed a Javascript implementation of the ZCA or something similar. But I haven't found anything !!! Maybe, building complex and pluggable CMS in Javascript is not yet so common (no, I'm not surprised).

ZCA in Javascript ?

So I started to think to port the ZCA in Javascript, unfortunately It's not easily portable (It's written in Python and rely on Python inheritance implementation).
Furthermore some aspects of the ZCA perplexed me. These are the dark spots of the ZCA (all in my own opinion):



  • Interfaces

Interfaces are an unusual concept for dynamic languages like Python and Javascript. For example the concept of "interface" (design-by-contract programming) doesn't fit perfectly in an environment where you can create, delete or modify any attribute of an object at run time.
Python and Javascript coding style privilege attribute checking over type checking. For this reason in the day-to-day work the main use of Zope interfaces is often to "mark" an object without enforcing a specific interface (intended as a subset of attributes/methods).

  • Interface inheritance

Inheritance is a tricky matter.
Zope interface implementation is tightly coupled with the Python inheritance implementation. While it's powerful, it is also quite complex to figure out in complex inheritance chains.
Furthermore it's quite difficult to port the library in a different environment (for example Javascript).

  • Unnecessary functions and component types

ZCA API could be simpler furthermore it uses an unusual and rich terminology (components, adapters, utilities etc.).

occamsrazor.js

I eventually decided to work around some ideas and try to improve and simplify the ZCA.
The result is Occamsrazor.js

Occamsrazor.js is a library useful to decouple a functionality from the code that provide it.

Documentation and source code is here.

I'm very curious to have some feedback ...