Hacker News new | ask | show | jobs
by kls 5777 days ago
It is really not more complex and done right it is significantly simpler. If you roll your own toolkit it is a pain, but if you utilize a framework like Dojo, it is far easier than Java(JSP, Struts, JSF, et al.) or ASP.NET or even PHP. Without the contortions of pumping everything to the server and then getting a response and trying to figure out that context, you get a far less fragmented memory model.

For example lets take a shopping cart, in the page post model you would submit the page to the server, create a cart in the session (bad, bad, bad) and then respond to the client with a new UI, the client would select an item, you would form post that item and the server would update the cart with the item. Back comes the UI and we do it over again with another item, ad nauseum. Eventually the user selects check out, we form post and hit a routine that tallies everything up and spits back another UI. We do this until all the data is collected to complete the transaction.

With the new model, The UI is the sole domain of the client and we speak to the server in complete representative state when we have the whole communication. Not only that but data definitions have very ridged walls that define what that data is, therefore making the server side code far more reusable (more on that in a minute).

So for this example, done the new way, JavaScript creates an order object, it then displays the UI for products after making an asynchronous call to get the product information (given that this is a defined call to the /products URL we can set a cache expiration in the future and therefore any subsequent calls have very little cost associated with them).

So now the server is acting as a data and business logic layer, while the client is providing the work-flow and the screens.

Back to the example, on the client side we have an order object, loaded with products that we have not had to make transitions to the server to create and update, we can then push this object to the server via a POST to the /orders service.

As you can see your data and business logic are becoming very defined and resource-able. If you decide to provide a mobile interface you already have the services available to support it, your data is no longer intertwined with UI work-flow.

The benefits of this model are vast but at a high level here are the big ones:

The client side becomes responsible for the work flow. So different UI's can provide optimized work-flows for their format. Web, Mobile, voice, do not have to rely on the same work-flows to reuse existing code and not start over.

Front end developers work in pure HTML, CSS and JavaScript there is no reliance on back end technologies to be able to perform their tasks.

Back end developers work in pure platforms, a Java developer works in Java, a .NET developer works in C#.

The front end and back end are loosely coupled through service calls, either can be swapped out without ramifications to the opposing side.

You data and business logic becomes addressable, a natural byproduct is that you can expose your system to third party consumers and alternative UI's.

You are working with a non-fragmented object model, one party is responsible for state and that is the client.

The front end is far more responsive to user input. You have far more opportunity to pre-fetch data based on user patterns and expectations. You have fine grain control over performance.

UI's are best programmed via an event based model. It is impossible to achieve this within the old page post model. (See the Node.js talk on blocking vs no-blocking, this is a relative of that argument).

Session management is offloaded to the client, reducing large amounts of memory and resource requirements on the server side. No longer does the server have to approximate what is happening on the client side.

A byproduct of the client holding session is that any disruption in communication does not reflect a total failure of the transaction. The client holds state and therefore can submit to the server once it become available again, no matter the point in work flow.

The decoupling of the server side allows the UI teams to develop the front end in a far more agile and rapid fashion. They can hold closer to the stake holder and rapidly modify the application to meet user needs.

If a top down approach is used, the entire front end can be prototyped while creating stub service files, allowing the stakeholder the ability to touch and feel the application before back end development begins. This significantly reduces the costs associated with development to get to the point where end users touch and request rework to the application. Further, the stubbed services provide a clean definition of the services required to the back end team.

I could write a book on the pros of this development methodology but sufficient to say, with JavaScript frameworks and proper architecture, writing new style web apps is far less cumbersome and a lot less convoluted. I was one of the nay-sayers until I tried it and actually found that it was easier and produced a superior user experience. The benefits to building apps this way are numerous.

3 comments

In your post you say:

>> create a cart in the session (bad, bad, bad)

Sorry to be a total n00b, but why is it bad to keep the cart in a session? What do you suggest as an alternative?

Server side session is a outcrop of the idea that the client is dumb philosophy, as such, the server has to "approximate" the client, this leads to many counter intuitive patterns like session. By far the worst evil is the fact that you have no guaranteed destructor because you have an approximation. So if the client wanders off, you have no way to clean up based on that event other than a brute force timeout. Further session by nature has no way of self governance. For example, I cannot wire an object in session to be cleaned by an observer once an action happen, so a natural byproduct of this is that you get "junk" in the session that all live processes that have reference to it have terminated thereby leaving a zombie.
Sorry for replying to my own post but I did want to touch a little further on this subject. above I stated:

So if the client wanders off, you have no way to clean up based on that event other than a brute force timeout.

A common rebuttal to this is well just sprinkle in an AJAX call. Which in my opinion is the worst decision one can make. Now not only are you supporting a server model but you also have client mode sprinkled in which compounds the complexity of you application significantly, in essence doubling your technology stack. This is the choice a lot of developers make when trying to dabble in RIA and it is my held belief that this is a fatal mistake. It doubles the required skill set and creates convolution in the sequences of application communication.

By 'an AJAX call', do you mean having the client periodically ping the server, and then cleaning up the session if the ping times out?
No I mean wire an event to the unload of the page, that notifies the server that the client has abandoned the app.
Well, nowadays you can encrypt and sign sessions and store the signed/encrypted data on your client side (or non-encrypted cookies if you want them to be modifiable from the client side). As long as it's more difficult to fake session data than to buy working credit card numbers, you're fine (at least once you've taken care of XSS attacks, which I take to be no less of a problem in a single-page site).

Wow. Did I just point out that cookies have legitimate and valid uses? My self from 10 years ago would run after me with a shovel and yell that cookies are evil. (Incidentally, the opinion of my self from 10 years ago about Javascript would be exactly the same).

He suggests keeping the cart clientside in (e.g.) a javascript array.
Can you link to some examples of this? Do you use only dojo on the client side or is there something more?
For large applications I use Dojo, for quick small apps and web pages I use jQuery. I focus my time on these two because I feel that they represent the best in their respective classes. But yes, I use Dojo, CSS and HTML that is pretty much it. For the back end I use Java, mainly because there is a wealth of middle-ware technology available for Java. I use JAX-RS to provide all of my services as RESTFull services.

http://www.ibm.com/social/impact/

The techniques you're describing aren't benefits of your system. They're just modern web programming techniques and certainly don't need to be used in a single page programming model.

For example the majority of what you're describing as benefits has been known to anyone using MVC for a while, MVC is available in any of the languages you mentioned as your system being easier than.

MVC was conceived to account for the limitations of a view oriented development philosophy. MVC is far from modern as it is not an evented model. Modern are platforms like Node.js and the RIA toolkits who eschew MVC in favor of an evented and message oriented architecture.

http://www.appcelerant.com/mvc_is_dead.html

http://apsblog.burtongroup.com/2007/11/mvc-matrix-and-.html

http://broadcast.oreilly.com/2008/10/mvc-as-anti-pattern.htm...

http://www.sitepoint.com/blogs/2005/12/22/mvc-and-web-apps-o...