Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you synchronize server-side and client-side code?

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?

like image 657
Philippe Beaudoin Avatar asked Dec 15 '09 18:12

Philippe Beaudoin


People also ask

How does client-side and server-side work together?

Client-side and server-side are sometimes referred to as front-end and back-end. The client-side of a website refers to the web browser and the server-side is where the data and source code is stored.

Which programming language can be used for both client-side and server-side of a web application?

JavaScript is both a client and server-side language because it can be used to develop client application in your browser (or even mobile apps using environments like Apache Cordova) and also as backend technology thanks to runtimes like NodeJS which is entirely executed in a server machine.

What is server-side synchronization in Dynamics 365?

Server-side synchronization is the preferred option for organizations with users who run Dynamics 365 Customer Engagement (on-premises) in a web browser or on a mobile device, such as a tablet or smartphone. Server-side synchronization provides direct Customer Engagement (on-premises)-to-email server synchronization.


2 Answers

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.

like image 61
Brian Hasden Avatar answered Oct 11 '22 03:10

Brian Hasden


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.

like image 32
Bialecki Avatar answered Oct 11 '22 01:10

Bialecki