1

I have almost a 'best-practice' question that's been nagging at me for a while.

When I use JavaScript libraries and APIs such as JQGrid or Google Maps, I tend to find myself creating server side libraries to render the JavaScript for me.

For example I might have:

$map->setZoom(10);
$map->setDimensions(200,250);

in PHP, which would internally render the relevant JS when I call $map->render();, for example.

This means I can easily generate JavaScript based on the state of my applications. I also find it easier to work with the data I already have in my controllers etc.

Is this a suitable way of using common functionality within a JavaScript library or would it be better to write JS as standard?

2
  • It depends on how and where you do it. An MVC helper might be a quite useful solution for this kind of issues. Though it should be not overdone. I also like data- attributes so Javascript can convert it. So you create HTML with data tags and Javascript attaches behaviour to it. Feb 1, 2013 at 19:53
  • I quite like the data attributes too, but I found in some situations I was almost over-using them which became hard to maintain with so much data.
    – James
    Feb 4, 2013 at 11:33

3 Answers 3

3

First issue: code quality

How do you:

  • unit test,

  • debug,

  • review for security issues,

this generated code?

The issue is the same for any generated code.That's why code generation tools are used only for simplistic tasks where the code is mostly boilerplate.

For example in .NET world, Visual Studio generates code for Windows Forms and is limited to positioning and customizing controls, but nothing technically challenging. In the same way, Entity Framework generates the mappings for the database, which means lines and lines of boilerplate, uninteresting, monotonous code.

Second issue: performance

If the code is generated, how do you cache it? Is it at least cached? If not, have you measured the precise impact on the performance, compared to the correctly-cached static JavaScript code? What about the impact on the server which needs to generate this code and how does it scale?

This issue may be non-existent in some cases (or at least the slight performance impact is very limited by severe caching, and a few milliseconds spent by the server generating the files is outweighed by the gains in terms of time you spend writing code), but you still need to measure the impact to know exactly how is it affecting your application.

Third issue: lower interoperability

JavaScript code written in JavaScript can be used no matter which framework is used server-side. JavaScript code I've written for an ASP.NET MVC website two years ago can still be used for a new website in Python.

If the code is generated server-side using server side programming language, you won't be able to reuse the same code in websites powered by other programming languages. Moreover, even migrating to newer versions of the same framework may be painful.

1
  • Thanks for your detailed answer; you've raised some points that I hadn't originally considered. I think I need to find the balance between eradicating monotony and keeping flexibility, although code review and maintainability are potentially an interesting concept for me in this case.
    – James
    Feb 4, 2013 at 11:34
0

I convert data to JSON on the server by using a series of "plug-ins" that convert standard data sets to multiple styles of JSON. This allows me to convert data to objects, array of arrays or custom formats (like that used by DataTables). My data layer doesn't have to change, I just switch out the JSON formatter when needed.

0

The latency of network must be the biggest concern of web development. So it is not wise to change the js files which cached by the browser according to user inputs .

The data and the logic comprise of the world. The state of your application is actually the data, let the data changes over time instructing your logic to do the job. That is not telling you to push all the logic to clients, but push what they need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.