-4

What are the pro's and cons of putting css & javascript code in your html file.

I'm teaching students who are just getting started and not sure if starting with external files would be the best way to start and if just having them add everything in a single file is more beneficial for the early learning stage. What are the pro's and cons?

jibly
  • 115
  • 1
  • 13
  • 1
    You're talking about separation of concerns, which is good, so one person won't mess up another person's work. – a coder May 22 '15 at 22:39
  • 1
    I totally agree with this opinion: http://stackoverflow.com/a/138893/3161440 – jkd May 22 '15 at 22:42
  • 2
    Single file works fine for short and simple programs, but when it gets longer and logically more complicated it becomes impossible to maintain. Teach them to do everything in one single file first, and let them see the problem, then teach them there's a better way to program. – Derek 朕會功夫 May 22 '15 at 22:51
  • This is too broad, there are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs. – Arian Faurtosh May 22 '15 at 23:04

1 Answers1

3

Modularizing your code by separating your HTML, CSS, and Javascript code has plenty of benefits. Here’s what comes to mind:

If you’re placing all of your code into one file, you’re making it really difficult for other members of your team (or future engineers) to collaborate on your project. If four people are working on a project all off the same index.html file, imagine all the merge conflicts that you’re going to have to resolve over and over again.

When debugging, it’s going to be a lot easier to reference a smaller file containing a hundred lines of code than a monolithic one with thousands of lines.

It also adds a lot of technical debt to the project — when future engineers who inherit your work finally decide to modularize your file, they’re going to have to spend a lot of time doing so which will leave a negative impression of your work upon them.

TL;DR — Always modularize your code! It makes it easier to read, understand, debug, and collaborate.

Richard Kho
  • 5,086
  • 4
  • 21
  • 35