r/javascript • • Dec 19 '16

~15 years trying to make everyone separate HTML, JS & CSS. And then suddenly everything went south and we’re writing code like this.

https://twitter.com/thomasfuchs/status/810885087214637057
483 Upvotes

258 comments sorted by

View all comments

Show parent comments

3

u/Retsam19 Dec 19 '16

i was under the impression this post was a commentary on nearly all of the framework libraries.

No, most frameworks (Angular, KO, Ember, Backbone) maintained a far more traditional JS/HTML separation, until React came on the scene. It wasn't until React and JSX showed up that the "keep HTML and JS separate" idea was challenged.

so, you're "ideally separated button" would have no onclick function?

your examples depict a "pre-javascript" web. Removing javascript entirely is not a "separation of concerns", you're describing a completely different medium.

where is the javascript?

It's in the JS file, using JS to attach events to the HTML elements. Like this:

HTML:

<button id="look-ma-no-js">Pure HTML button</button>

JS:

var buttonEl = document.getElementById("look-ma-no-js");
buttonEl.addEventListener("click", function () {
    doSomething("Foo");   
});

This is what "properly separated HTML and JS" has looked like for the last ~15 years. It looks a bit different if you use a framework, but it was always the same idea: HTML in one set of files, JS in others.

9

u/edwardmsmith Dec 19 '16

Ok, but why was that separation maintained?

I started writing web pages in '93, so I've been all up and down the progression of "separation of concerns" over the years in web dev, and for the most part, it was really about not mixing your DB access with your HTML in server-side templates, but when it was applied to HTML/CSS/JS it was done for the "HTML describes the structure, CSS describes the style, and JS describes the behavior" reason.

Now, at that time, when you were talking about the "behavior", you were talking about progressively enhancing a static website to add some (small) client side behaviors to a functional static website.

You had functional anchors that would submit to the server, and you'd tack on a bit of behavior, such as client-side validation, and eventually AJAX, as a progressive enhancement for the browsers that supported it (IE6 to start..).

So you have your static website served from the server, and if the browser supported javascript decently and had decent functionality, you'd add interactive behavior by tacking on the JS - you wanted to separate the JS so your server-side HTML generation didn't have to produce the JS.

It all made sense at the time, and if that's the kind of web site (although, we're really talking web apps now...) you're making, then this sort of "separation of concerns" still works.

But, if we're creating webapps that are large, complicated and don't really work without javascript enabled, the code in your example above, with that old idea of separation of concerns, is silly because without the actual behavior, the button is useless - it does nothing at all.

The idea being that the coupling between the element and the behavior is so tight now that in almost every conceivable circumstance of developing or modifying the code, you're going to be editing the element and the behavior at the same time.

This goes even further when you're working with something like React, where you're not even dealing with HTML at all, but instead you're dealing with (a virtual) DOM.

When I'm writing React, with the exception of my leaf components, and wrappers around component collections, I'm not really thinking in terms of HTML anyway, but instead, composing components.

The concerns of structure and behavior have blended, and therefore maintaining separation between them no longer really makes sense.

The concerns I want to keep separate now, in my modern web applications, are the concerns of dumb display components (that may or may not have hooks in the DOM event system) and smart components that act on those events and modify application state.

1

u/SamSlate Dec 20 '16 edited Dec 20 '16

isn't that "attaching JavaScript with JavaScript"? ok, i think you meant "onclick" attribute when you said "no JavaScript in the the event listener". i see.

i guess you could develope with no js in the html.. what's the advantage? aren't you adding more work because you need to check that the element you want to attach to exists? ... maybe you're not suggesting it should be done this way, rather it could..

1

u/Retsam19 Dec 20 '16

No I'm definitely saying you should develop without JS in the HTML. That has been the prevailing wisdom for over a decade, and pretty much any JS/HTML tutorial worth its salt should teach you that.

JS in HTML is hard to read, hard to write, annoying to debug, and it doesn't even try to separate concerns. Yes, you have to make sure the right elements exist, but it's better than the alternative.


Using JS to add event listeners to elements has been the gold standard "vanilla JS" way of doing things for ages.

Most "traditional" frameworks provide some sort of data-binding: where the framework will manage the work of connecting your JS and HTML, to avoid the need for getElementById stuff. They often by replacing or augmenting pure HTML with a template language: the templates references the JS code in some way, but it still keeps JS and HTML separate.

And then "react"-style frameworks have to HTML controlled by JS; this violates the traditional "no-HTML in JS" idea (hence "rethinking best practices"): it does this because React, instead of dividing code into JS vs. HTML, divides its code into "JS + HTML" components. It's a different organization structure with its own advantages and disadvantages.

1

u/SamSlate Dec 20 '16

Yes, you have to make sure the right elements exist, but it's better than the alternative.

I'm not convinced. if i'm binding events to buttons it make sense to have those functions in the element itself. you're executing "passive" code that can be re-used. if the code doesn't exist the button loses it's function but the page is unaffected. If you can't find the element you want to act on, or your code is present on the wrong page and the element doesn't exist at all then you javascript file breaks and stops executing. I can respect a clear separation of files, but your setting yourself up to with a need to create large swaths of parallel operations and checks for only handful of cases.

1

u/Retsam19 Dec 20 '16

I've coded JS for almost a decade, and worked professionally with JS for the last 3 years, and I've never heard anyone advocate for inline JS. If you're doing anything with the slightest bit of complexity, inline JS is a nightmare.

But, if that's the way you want to roll, go for it.

1

u/SamSlate Dec 20 '16

a js file with nothing but functions, an HTML file with inline function calls.

Am I really that crazy?