Friday, May 27, 2011

The Javascript Bigot

Why am I a Javascript bigot?  Good question and my introspection my adversity led to an evaluation of coding practices and frameworks......

Javascript in and of itself is not bad, but I see a lot of web applications where Javascript has gone way beyond simply adding a rich feel to web applications.  When Javascript is used to control and orchestrate application behavior that's where things go off the tracks.  A lot of this is due to marshalling frameworks.  For example, you have the Order and Order Item data relationship.

I have seen lots of examples where the data is "flattened" to send it back to the client.  Initially this was done to save bandwidth (think old dial up days).  So instead of sending every order item along with the order, you may have sent an order item id and let the client ask for the order item by id later.  The reduction of the payload leads the client to have an intimate understanding of the data relationships and their implementation on the server side.  The client must orchestrate the requests for data in a way that makes sense to the server.  This leads to a distributed set of logic, multiple maintenance points, fragility and difficult testing.

Examples of this are sending data over XML where data relationships that exist on the server are not represented in the data sent to the client.  I've seen similar with JSON payloads.  DTOs used in Swing applications, Smalltalk models, Spring Beans and JSF backing beans.  If you look at web applications as basically taking data from a database and formatting it so it is pretty for the user, allows them to view and interact with the data, and save their changes -- then the closer the client model is to the persistence model, the less confusion and rework there is.   Keeping client and server models as 2 separate things leads to duplicate maintenance and ultimately multiple sets of logic and orchestration.  One of the reasons I really liked JSF and Seam is the close relationship of the server persistence model to the client model.  It leads to bean validation where the client can use the server validation rules.  It leads to expression language use of the server model within the page rendering engine.  It leads to a client that simply formats data, allows the user to interact with it and save their changes.  Perfect.

So maybe I'm not a Javascript bigot after all.

No comments:

Post a Comment