views:

70

answers:

4

When dealing with websites with large amount of javascript, i see that these are still usually served to the client as one large javascript file.

In the development phase, are the javascript files usually split up (say there are >300 lines of js) to make things abit more manageable, and then merged when the website is 'put live'? Or do the developers just put up with working in one long large file?

+3  A: 

In my experience — having separate files in development is the norm. It certainly makes life easier when you need to hunt for code or have multiple people working on different parts of the system.

David Dorward
+5  A: 

We place different modules/classes/parts in separate files and use a proper build process to

  • validate the code using eg. jslint
  • concatenate
  • instrument (replace, wrap etc)
  • minify

An example of how to use Ant for this can be found in one of my projects here http://github.com/oyvindkinsey/easyXDM/blob/master/build.xml.

I also have projects where the webserver automatically merges the files, localizes and then minifies them before serving the client.

So stick with whats manageable, using separate files, but do remember that if you use error reporting then the line numbers will point to the concatenated version.

Sean Kinsey
We do the same thing at my company, but even on my production environment, we can append "debug=true" to the URL query string and see the page loaded with all of the original JS files un-minified. It makes debugging very easy. I take that technique everywhere I go now. Such a great idea (not mine!)As always, you have to balance manageability with user experience. By default, most browsers only download 4 files at a time. And because JS files block page rendering, if you have 10 JS files mixed into your code, your page will always take longer to render than if you have just one or two.
Andrew
Our tool localizes, if debug it minifies and if not debug then it names all anonymous functions. This tool also handle 304 requests and serves using aggressive caching headers. To avoid stale cache we then render urls to the handler using the timestamp of the files.|
Sean Kinsey
+1  A: 

It would be ideal to have multiple javascript files depending on the class and functionality (like you have them for java project ) in development environment.

However when you are deploying the js file in production, you should concatenate all js file in a single file and have them referred by your web application. That will make thing easy

Note: It would also be advisable to use javascript compressor to reduce the actual size and hence saving bandwidth.

Kunal
+1  A: 

Developement differs from company to company and from developer team to developer team.

I for myself am used to the approach of implementing functionality step by step, storing those functionalities in seperate files and merging everything together in most cases - at lease when i am not the only one working on a given project.

Thariama