Should data be formatted in the backend or front-end?

dchhetri picture dchhetri · Nov 19, 2013 · Viewed 10.9k times · Source

I have a web application, and I'm wondering if its better to format the data in the front-end or the backend? They both get the job done, but can someone help me brainstorm which is the better option between the two.

As an example, say I have a backend that returns the a family tree of names in a certain format, but in the front-end I need to adjust the format to match the format a widget expects, should this adjustment be done in the backend or the front-end?

If its done in the backend, I can straighforwardly push the data into the widget in the front-end else, I would have to parse in the front-end beforehand. Can anyone think of pros/cons for this situation? Thanks.

Answer

Guido Anselmi picture Guido Anselmi · Nov 19, 2013

Great question. I do a layered MVC-inspired architecture.

BACKEND

I model (format) the data on the backend in accordance with it's 'natural' order. In other words I follow the internal organization of the data. This is because my APIs are often used by multiple, changing or evolving clients and rewriting the API multiple times or having multiple versions takes too much time to maintain.

This doesn't mean you should send the contents of the database with every API call. You should definitely pare down the data model with for each call but it should be a pared down version of the backend ('natural') data model not a data structure that is customized to a particular view.

FRONTEND

In the front end I have a tightly coupled controller which receives the data from the server and transforms the data into the model that fits well with a given view. Depending on the technology used on the client side there may be library support for this (e.g. AngularJS for javascript/HTML Swing for Java, WPF for C#, etc. etc.)

I find that this architecture results in clean separation and high productivity.