kazinix
kazinix

Reputation: 30113

Disadvantages of separating JavaScript Codes?

I have a page which has many event handlers. The code now reached 1000+ lines of codes and I'm beginning to have difficulty reading the codes. I'm now planning to separate the codes to different files. My question is, is there any disadvantages in separating JS codes into different files?

Upvotes: 6

Views: 2893

Answers (6)

dbrin
dbrin

Reputation: 15673

Here is a bolg post about JSminify and similar tools to use with VisualStudio and build automation: http://encosia.com/automatically-minify-and-combine-javascript-in-visual-studio/

Upvotes: 2

ZER0
ZER0

Reputation: 25322

Having code separate in different files helps you for maintenance. I will suggest to you to split it in modules compatible with CommonJS, if you're working in a browser environment you can use RequireJS to load them. You can also use the utility provided to create a single file when you deploy your website / webapp in order to optimize the http requests.

Upvotes: 1

jfriend00
jfriend00

Reputation: 707436

The best practice is to create relatively small files for development purposes where each file contains a module of functionality that is all related (whatever is most efficient for development/debugging/editing/source control. You can give each file a meaningful name that describes what is in it. Each of these files can be managed separately (their own version history in your source control system, check in/check out, etc...). It's often easier to have multiple tabs open with separate files in your editor than trying to use bookmarks to jump around between different places in one large file, etc...

Then, when you go to deploy your app, you use a tool (like Google closure or YUI Compressor) to minimize and combine all your smaller files into one deployment file. This preserves the development advantages of smaller files that contain related code while preserving the deployment advantage of having most/all the code in one larger external javascript file that can be downloaded in one http request and can be cached very effectively.

Upvotes: 4

pylover
pylover

Reputation: 8065

In addition to my comment.

Use t4 templates from here, or you can use C# to write javascript by script#

in modern platforms, developers does not write javascript directly, for example

  • in Rails, using CoffeScript
  • in python, using one of (py2js,pyjamas,google V8, skulpt)
  • in C#, using script#

Upvotes: 2

Caffeinated
Caffeinated

Reputation: 12484

The disadvantage would be the added complexity of making sure you're including everything correctly. But 1000 lines is getting unwieldy. Here's a related page about calling JavaScript objects in separate files

Upvotes: 3

Joseph
Joseph

Reputation: 119847

disdvantages?

  • another HTTP request. developers advise that files be compressed and be in as few files as possible for optimization. the less files you request, the faster (since there is less round trips)

  • if you use a text-editor with an auto-complete, some of these editors won't pick-up the stuff from the other file for auto-complete. this can be a problem if you don't memoize the functions you have.

  • if one of these inter-dependent files failed to load, your app will break.

Upvotes: 3

Related Questions