jn025
jn025

Reputation: 2895

External files - good programming practices

I was just wondering about some good practices when it comes to external files specifically with javascript and CSS.
For javascript, should you make an external file for every module/added feature so that it's easy to locate and find the code in one spot for the module?

Also is it bad to have too MANY external files connected to one page, to the point where it affects load time and general bad practice?

Same question appllies for css..

Thanks

Upvotes: 4

Views: 333

Answers (3)

dlock
dlock

Reputation: 9577

You always want to your files to be organized and well structured in a project like manner during development time. It's okay to have many css,js files during development, but that's very costy and expensive in production or real-life phase due to the fact that each file load adds an overhead to the exact file size making the file fetching slower and increasing your page load times. So, when you are ready to deploy your application, it's a good idea to merge and minify all your css files into 1 css file, the same goes with your javascript files too. Just remember that if you are doing CSS overrides, you will have to maintain the file order while merging not to mess up your css logic.

You can merge and minify using that tool http://www.shrinker.ch/ ;)

Upvotes: 1

Melbourne2991
Melbourne2991

Reputation: 11797

Best case scenario is to have a master css file for the high level layout, and then different css files for the sub layouts that are only called when the module is loaded (not sure what framework your using, but I suppose your modules can have independent layouts). However you would want to use a minifier:

code.google.com/p/minify/

This compiles your css to a smaller format / single file, which helps keep the size down by merging all called css files into 1 master css file.

At the same time you want to get an adequate balance between load time and maintainability, if lumping some layouts together makes the code easier to maintain and the load time trade off is minimal then there's no real harm in this.

Upvotes: 0

Roooooo
Roooooo

Reputation: 172

Person I use external files for every code so they are all separate. One for my CSS, and one for my JavaScript. I wouldn't say it's bad practice, It keeps things organized.

Is it bad to have too many?

I don't see the point on have multiple external JavaScript or CSS files when you can use a comment line to separate it if you are really OCD about it being organized. If for some reason you want to have multiple files you could create a folder specifically for the group of files.

Overall

I organize a lot with external files, helps me keep track of things. So I would say it's good practice, just trying to not go overboard with it, keep it like at 2-3 of CSS, JavaScript, Or jQuery each.

Edit* I think it's primarily for organization, so, yes it is good practice to use external files.

Upvotes: 0

Related Questions