Why is it bad practice to use inline Javascript?

16

The title already describes the question: why is it considered bad practice to use CSS and Javascript inline in our codes?

Is it bad to use this even on smaller projects?

    
asked by anonymous 18.07.2014 / 17:09

3 answers

13

The biggest problem with using CSS and Javascript inline is to mix different types of things in one place.

HTML is a markup language for defining abstract elements. CSS is made to define visual styles. Javascript brings dynamic to pages. Putting everything together makes maintenance difficult and creates a lot of confusion.

Although there are some HTML tags that work in formatting, the best practice recommended by 100% of professionals is to separate content from formatting.

Having CSS and Javascript separately helps not only reuse, but forces the developer to better think about what he is doing, ie, to structure the page into "component" species rather than simply repeating the same style and script in every place.

In addition, maintenance is greatly facilitated because when HTML is decoupled from code and style, you can modify the content without changing the formatting and vice versa. The same goes for new content, if they have the same structure as the formatting already exists and the scripts will be easily reusable.

Note: Of course there are other points, such as less use of bandwidth, already mentioned in other ways in other answers. However, there are cases where this is questionable. My goal in this response was to highlight the point that is by far the most critical in the subject, ultimately leading to bugs and inability to maintain.

    
18.07.2014 / 17:22
9
  • It's easier to keep css and Javascript separate from html.
  • You can reuse the code for other pages or projects.
  • The page is lighter because the browser stores the css in the cache so when you reload the page, browser does not need to read everything again.
  • 18.07.2014 / 17:22
    6

    Disadvantages:

  • Unable to reuse code
  • As a consequence of item 1, the client ALWAYS needs to download the code
  • Of course, you can eliminate the problem 1 using includes in your server side language but problem 2 forces the client browser to download the code to each request in the main document.

    If the code is a separate .js file, the client's browser decides whether to re-download or use the local cache, which improves the user experience and saves your bandwidth.

    But if your project is a single page without new requests, OK, it does not suffer from the problems I listed.

        
    18.07.2014 / 17:14