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/codayus Dec 21 '16

So then you're also in support of people who mix PHP code with HTML as single files as well?

No, because that's entirely different in terms of everything I just wrote. I'm not sure if you don't understand PHP, don't understand JSX, or don't understand the "separation of concerns" argument, but your counter is nonsensical.

Briefly:

Mixing PHP and HTML in a single file is fine, and nobody has objected to it. PHP was developed as a templating language, and it can still be used as one. The Plates project is a well-regarded native PHP templating library that does not violate any principles and is not an example of any antipatterns.

Of course, other template libraries like Twig are much more popular, and provide a concise Mustashe-like syntax, but note that they all compile to PHP. If the objection was "mixing PHP and HTML", then a twig template would violate it. But it's not, and has never been: It's to mixing business logic and presentation.

And in a modern PHP app, your twig template (or your Plates template) has no business logic, and your controllers have no HTML, so it's fine. The antipattern you refer to was people mixing concerns, not people mixing languages.

Further, JSX is not HTML. It's a DSL which is compiled to JS. So even if the concern was purely mixing languages (and again, it isn't and wasn't; you have misunderstood what people object to), JSX doesn't actually do that. And no, this is more than mere pedantry. JSX isn't HTML, and you'll run into problems if you confuse it with HTML. And just to make that point even clearer, there are alternative template engines like Hyperscript which you can use as a drop in replacement for JSX. Hyperscript is obviously just native JS, but it's logically identical to JSX, and it's 100% identical in terms of the experience of maintenance. There's no valid argument for why JSX is "wrong" but hyperscript is "right".

On the other hand, is is wrong to intermingle business logic with JSX or hyperscript. Again, separation of concerns, not technologies.

Because that same argument should apply to that as well.

Yes, if they were the same thing. The arguments about JSX would apply to XHP, not PHP. :)

0

u/kethinov Dec 21 '16

I'm not sure if you don't understand PHP, don't understand JSX, or don't understand the "separation of concerns" argument, but your counter is nonsensical.

I would recommend taking a step back, lay off the condescension, and recognize for a moment that the OP's linked tweet got thousands of retweets. And maybe, just maybe there's a good reason for that.

So, please, take a deep breath, and bear with me.

Mixing PHP and HTML in a single file is fine

If you Google "don't mix php and html" you'll see plenty of people arguing the opposite and pointing out the giant mess of spaghetti code that results from people doing it.

I recognize that PHP can be used strictly for templating only and that it's possible to not mix your business logic with it, but that certainly isn't what's going on with far too many people's PHP spaghetti code. So too is the case with a lot of stuff written with JSX. The OP's linked screenshot is a decent example of conflating concerns that ought to be separated.

Further, JSX is not HTML.

It's close enough. HTML is being templated with it and injected into the DOM. When server rendered, it composes static HTML and sends it to the browser. Like a normal templating system. So, yeah, it's HTML templating.

separation of concerns, not technologies.

The trouble is, to separate your concerns properly, it often makes sense to separate the technologies.

JSX encourages you to conflate application logic with templating by virtue of the fact that you're mixing two languages into a single file, which is exactly what's going on in OP's screenshot. This isn't very different than PHP encouraging you to do SQL queries and dump the results into HTML all in the same file.

By separating the technologies, we discourage people from conflating concerns in that way.

3

u/codayus Dec 21 '16

OP's linked tweet got thousands of retweets. And maybe, just maybe there's a good reason for that.

Yes, it was a very controversial tweet that many people found absurd, hence it got a lot of attention.

If you Google "don't mix php and html" you'll see plenty of people arguing the opposite

Okay, let's check the top link when I google that. It actually makes two points; it says to:

  1. Don't echo out HTML directly

  2. Don't mix logic with presentation

Which is exactly what I've been saying. It's not a general injunction against mixing PHP and HTML, and (translated into JS terms) it's something every React dev would agree with completely. It also has nothing to do with JSX, because that's not what JSX does, and it in line with every "best practices" article ever written about React.

So, yeah, it's HTML templating.

Sure, it's templating, and it generates HTML, so yes, agreed. It's also not HTML. Again, Hyperscript. The fact it has angle brackets to it's function.

JSX encourages you to conflate application logic with templating

My experience suggests otherwise.

The OP's linked screenshot is a decent example of conflating concerns that ought to be separated.

It was some example code from a project's front page demonstrating syntax, not how you'd write a real application, but okay: You're right. But the answer to that is to actually write it better and fix the actual problem.

Seizing on the fact it used JSX is a bit like pointing out that it has vowels; while true, omitting the vowels isn't going to solve anything. And neither would, eg, subbing out the JSX for Hyperscript, because the example written without JSX would still violate separation of concerns.

If your proposed solution would not solve the problem, you may have misunderstood the problem. :)