views:

342

answers:

3

Something I've been learning (and teaching) in Software Engineering is that code duplication is the root of all evil. On the other hand, I find it quite hard to explain how this concept should be applied to the development of web apps.

Allow me to clarify... Input & data validation can be an important part of a web app. Sometimes this validation can be quite complex. For example, I worked on a puzzle editor and the validation consisted of checking whether an operation or a move was valid. Non-trivial rules then had to be checked.

Naturally, validation must be done server-side in order to ensure the consistency and quality of the stored data. However, it's a must to do validation client-side to ensure a smooth user experience.

In most instances, client-side and server-side code are written in different languages (i.e. javascript/Python), so validation code has to be written twice. However, in my only experience with GWT/Java (Java on both sides), I found that a large portion of the validation code could be reused. This seemed to make everything easier: maintenance, refactoring, debugging...

So my question to you is: how do you manage issues related to code duplication in projects where the client-side and server-side languages are different?

+4  A: 

The way I usually handle this is to write the validation code on the server side and expose it via a web method (in .NET, similar functionality exists in most other languages) so that it can be called from javascript. As a result, you have a single method that can be called both synchronously and asynchronously from the client side and also called from the server side. This isn't applicable in every case but it's worked very well for me so far.

Brian Hasden
+1: makes practical sense.
jldupont
A validation service? Makes sense, but network delays may make it impractical when the goal is to have an interactive UI.
Philippe Beaudoin
It depends on how you handle it. If you're making async calls in the background you can very nicely highlight the area which is invalid. If you're looking to make sync calls just make sure they're lightweight and it shouldn't be a problem. It's amazing how quick HTTP calls can be if you keep the payload down.
Brian Hasden
This is true, except when you have a crappy connection for whatever reason (e.g. cell phone edge network). There will always be high latency situations. It feels like if you want it to be really responsive, you have to have client side code.
Bialecki
Agreed. When I was speaking of "isn't applicable in every case" spotty network connection and low-bandwidth connections would be some of those cases where you'd need to come up with a different solution. In the general case though, this is usually the approach I take until there's a reason I should deviate.
Brian Hasden
A: 

A possible solution is that you abstract the actual validation in a validation description file (in XML or any other means) using a DSL. This way you only need to implement the validation engine in both client and server language and base the validations on the same description file.

David Nouls
I get the impression that "validation" is typically taken to mean something more specific than what I thought. Sure, validation can be about checking the content of a text field. More generally, though, it's about making sure that the way in which the model is being modified is legal given its current state. This definition of "validation" makes it hard, in my opinion, to do it using anything less than a programming language.
Philippe Beaudoin
+2  A: 

Typically, it's really hard to avoid duplicating the generated code, but a common approach is to use a code generator to build either the server or client side code so you only code one half of it. The most popular approach is writing the server-side common and then having the code generator build the JavaScript code for you. For instance, the language we use at my company is Coldfusion and Form-o-matic solves that problem for us. People have also approach the problem from the opposite direction by writing JavaScript which can be executed server side. I'd look for a framework that will do this for you.

Bialecki
Thanks. This goes a long way towards answering my question. So GWT, seen as a code generator, essentially goes in the same direction? Is there any of the code generators you are referring to that has the flexibility and power of a full-fledged programming language? (In order to do non-form based validation, like what I described.)
Philippe Beaudoin
I think GWT is one of the furthest along I've seen because you write next to zero client-side code. I haven't experimented with it at all, but I think the only thing that would provide a fully fledged programming language that can have code that is executed both client and server side are the frameworks that allow JavaScript to run on the server. I'm sure there are still limitations there, but the goal is to write nothing but JavaScipt and it can be executed anywhere. Personally, I don't love JavaScript enough to do this, but if you wanted to, you could go this route.
Bialecki
Check out the wikipedia page here for same projects that have gone in this direction: http://en.wikipedia.org/wiki/Server-side_JavaScript.
Bialecki
Thanks for the wikipedia page. Very useful. You do write client-side code in GWT, simply, you write it in Java and the javascript is generated from there. So you can take some parts of your code and run them both client-side (after javascript generation) and server-side (inside a servlet).
Philippe Beaudoin