2

I am a beginner of Javascript. I have done some Python and Java before.

I am not clear when and why we should store codes into different .js files or the same file.

Is there any conventions and rules for this?

Meng Zhang
  • 337
  • 1
  • 4
  • 13
  • This is a very opinion-based question, but you can find a lot of different answers out there. One of the better discussions can be found over here: http://stackoverflow.com/questions/247209/current-commonly-accepted-best-practices-around-code-organization-in-javascript?rq=1 – Cellivar Aug 09 '14 at 20:25
  • I think it depends on how your javascript will be used by a user. Do you use javascript for some webpage or are you using something like XUL-Runner? In XUL i would suggest you put your logic into modules (single JS files). In a "normal" webpage i would suggest putting it all into one file. If your page has some complicated logic just use multiple files for overview's sake. – Flocke Aug 09 '14 at 20:46
  • Learn the [AMD pattern](http://addyosmani.com/writing-modular-js/) and use [Require.js](http://requirejs.org/). – Jared Farrish Aug 09 '14 at 20:47
  • Or have a look at Node and the CommonJS module system. – Felix Kling Aug 09 '14 at 22:48
  • This is not an opinion based question there are facts about upsides and downsides about putting code in seperate files and when you should do that. For example if you put code in seperate files too often when you should not, then you will create artifacts over time and your codebase will become messy. – Ini Dec 04 '22 at 13:37

1 Answers1

0

Well, the main reason of javascript code into different folders is for organization. Similar to Java or Python, or any language, you should organize your code in different files to get a code that any other in your team can read easily.

But in production scope, or better said, in your website, it's a best practice to reduce the number os javascript files for performance reasons. If you have 2 javascript files, the server will send 2 request. In the case that you have 10 or 15 files, the web will have performance problems.

So, in development it's important to have multiple files to organice your code, but in production you can minify then into one. Uglify is a good tool to monify your code into one file.

Hope it helps.

Mario Araque
  • 4,562
  • 3
  • 15
  • 25
  • Yes. It is my problem that splitting codes into different .js files will increase the load of requests. It is not considered in Java and Python. I will see how Uglify works, many thanks. – Meng Zhang Aug 09 '14 at 21:35
  • You should always be thoughtful about when to split code into multiple files. Eg. if you have a constant which will always stay in the App for sure then you may outsource it into another file, but if you have a constant which is only required if a certain feature exists, then it may not be a good idea to outsource it into lets say `/const/translations.js` or so, because if that feature gets removed, then you may forget to remove that constant and create artifacts in your codebase. This is of course a simple example. Another example would be outsourcing redux actions into a seperate files etc. – Ini Dec 04 '22 at 13:46