Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  Luc Franken Feb 1 '13 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 '13 at 11:33

3 Answers 3

up vote 3 down vote accepted

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.

share|improve this answer
    
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 '13 at 11:34

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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