News for hackers. Stuff that matters.
Re:Things every React.js beginner should know by spion on Monday January 25, 2016 @08:30AM (#10967034)
This is one of the main reasons why I've become so skeptical about best practice advice, especially if it doesn't come with a plain logical explanation for the reasoning behind it. Its similar to the problems with outdated laws - if we don't keep track of the reasons why a law came into use in the first place, we might miss the moment where it might not apply anymore (we wont even know, really)

While I'm not entirely sure what the original reasons were, I can see a big difference between the way, say, PHP was embedded in HTML and now JSX is embedded in JavaScript:

1. PHP was embedded in HTML, not the other way around.

2. The preprocessor basically concatenated everything as strings without caring about what DOM would end up being a result.

Because of these two reasons it was extremely hard to protect against improper injection of data into the resulting HTML. There was no way to make the preprocessor understand the different types of "placeholders" in HTML and what kind of data should be allowed within them.

In contrast, JSX embeds HTML within JS. Furthermore, JSX (pretty much) compiles to function calls, not string concatenation. This means that the engine has great degree of control over the interpretation of the data: its impossible to get malformed HTML, or for an attribute value to escape the actual attribute and so on. Which in turn eliminates the original problem that was present in PHP.

As for separation of concerns, thats still possible with JSX. We have CommonJS (or ES6) modules and we can move the render functions anywhere we want. But now its up to us to decide whether it makes sense or not.

Re:Things every React.js beginner should know by andreyvit on Monday January 25, 2016 @09:29AM (#10967307)
Actually, the issue with spaghetti PHP is not (only) string concatenation; it's mostly that view logic, domain/business logic and database boilerplate code are intermixed in a single file.

React components only ever contain view logic, which already solves a part of the problem. Furthermore, you typically split that logic into multiple components, many of which are stateless, independent and small.

Re:Things every React.js beginner should know by spion on Monday January 25, 2016 @01:14PM (#10968791)
See the problem with this explanation is again, it doesn't explain the reasons why this is bad. I'm not saying the reasons don't exist - on the contrary I agree they're very much real. However I don't think "view logic, domain/business logic and database boilerplate code are intermixed in a single file" qualifies as a straightforward, logical explanation. It simply doesn't answer the "why" question at all.

Let me try and cover some of the reasons why. I'm sure you know about these reasons (and can probably list more), my goal is just to present an example of what I consider to be a straightforward logical explanation:

If domain/business logic is mixed with view logic, then it would not be possible to reuse the same logic in a different view. These changes happen more often than anticipated: for example, a PHP project that has the view logic separated from the domain/business logic can more easily get an API (a HTML is one view, a JSON API would be just another view)

Additionally separating database code from business logic lets you switch databases more easily or to give you multiple ways to access the data. For example, data may be directly fetched from an SQL database initially, then its determined that this is too slow and results need to be cached in Redis. If there is no separate code for the data access layer, you would need to locate every database query and make sure it queries the cache first.

So this is what qualifies as a straightforward, logical explanation for me: lets assume the opposite statement was true, then we demonstrate how this leads to a bad situation and why its bad (more bugs, more work, and so on).

Another would be doing a case study: we did X (mixed business logic and templates in this way), then when we wanted to do Y (present a different view V) we had to not only write new code (for the new view) but deal with this problem (uncouple the existing logic from the existing view). If we originally did otherwise (wrote the logic separately) this would've been the same or similar amount of work (demonstrate this) yet later we would not have to do the hairy decoupling (which took Z amount of time and caused N bugs)

I think the whole industry would do much better if we focused more on actual specific examples and case studies of problems and drawbacks of different approaches rather than giving vague descriptions of the current state and claiming its "bad". Its just not helpful at all. There is so much reinventing the wheel in our profession simply because we are not communicating knowledge effectively. Frankly, most of it reminds me of religious dogma.

Re:Things every React.js beginner should know by taytus on Monday January 25, 2016 @09:50AM (#10967430)
In all fairness this is ancient history on the PHP world.
Re:Things every React.js beginner should know by mattmanser on Monday January 25, 2016 @09:53AM (#10967445)
That's the point that pkrumins/andreyvit are making, code where logic and presentation are mixed turn into a nightmare to maintain. And the practice is now ancient history.

Apart from here it is again...

Re:Things every React.js beginner should know by andreyvit on Monday January 25, 2016 @10:00AM (#10967485)
I explicitly do _not_ state that it applies to React, because there's no domain logic in React components. (Or at least there shouldn't be. It is certainly possible to write a spaghetti component that would do everything.)
Re:Things every React.js beginner should know by gemma on Monday January 25, 2016 @01:52PM (#10969050)
> It is certainly possible to write a spaghetti component that would do everything.

So...like PHP, then?

Re:Things every React.js beginner should know by mercer on Tuesday January 26, 2016 @12:36PM (#10974607)
Not really. You're strongly coaxed into doing it the right way by design, which is not my experience with the old-skool PHP approach.
Re:Things every React.js beginner should know by aikah on Monday January 25, 2016 @11:39AM (#10968122)
> In all fairness this is ancient history on the PHP world.

No, because PHP still work that way (hypertext pre-processor) and Yes because, PHP has frameworks that makes PHP work like other solutions (Ruby,Python,Java + frameworks), but one still need a <?php on top of a file.

And again, a lot of PHP developers despise these frameworks and question their usefulness. In theory, they are right since PHP is a template language which goal is to render text content, and separation of concerns can be easily achieve without a complex framework in the case of a web application. In practice a framework makes large codebases more maintainable, no question.

PHP is a strange beast. It's somehow rigid like Java, at least more rigid than most dynamic languages, it's a dynamic language, and a template language and it keeps on getting stuff from Java like languages (PHP 7).

It's ironic that PHP was created because its author kind of found Perl too difficult to use for web dev, then wasn't taken seriously by pros then tried to imitate Java in some ways to feel more professional and now is on part with Perl in terms of complexity, while half of the PHP community praises over engineered codebases often found in the JEE world.

Re:Things every React.js beginner should know by preinheimer on Monday January 25, 2016 @11:53AM (#10968238)
I have no idea how starting with <?php in a file is relevant. Should we also start mocking languages that expect a given file extension?
Re:Things every React.js beginner should know by aikah on Monday January 25, 2016 @12:11PM (#10968365)
My point is that PHP renders text first and foremost, writing code in PHP is "opt-in" somehow. An HTML file for instance is a valid PHP file. AFAIK an HTML file isn't a valid Ruby or Python file. You don't need to tell the interpreter to kick in at first place. But that's not the main idea I tried to convey in my comment.
Re:Things every React.js beginner should know by gemma on Monday January 25, 2016 @01:51PM (#10969041)
Right, your comment argued that "view logic, domain/business logic and database boilerplate code intermixed in a single file" is not "ancient history in the PHP world". Which it is, regardless of the `<?php` tag at the top of all the files. How a source file is processed has no impact on how programmers choose to model their applications.
Re:Things every React.js beginner should know by mercer on Tuesday January 26, 2016 @12:38PM (#10974623)
Theoretically no, perhaps, but practically I think there is an impact.

I can't dump html into a .rb or .js file, so I'm less likely to do so as a beginner. On the other hand, starting out with php, that seemed like the logical thing to do. Because I could.

Re:Things every React.js beginner should know by recursive on Monday January 25, 2016 @01:50PM (#10969037)
Because it demonstrates that that pattern does not follow the default mode of operation for the language.
Re:Things every React.js beginner should know by debaserab2 on Monday January 25, 2016 @12:13PM (#10968383)
> No, because PHP still work that way (hypertext pre-processor)

I think what the commenter is saying is no one builds like this anymore in PHP, regardless of whether or not you're using a framework.

Re:Things every React.js beginner should know by ChrisArgyle on Monday January 25, 2016 @09:28AM (#10967304)
The big reason I remember from my PHP days (late 90s early 2000s) is that you want to separate design from development.

Graphic designers were expected to know HTML/CSS while developers focus on PHP.

Re:Things every React.js beginner should know by mattmanser on Monday January 25, 2016 @09:57AM (#10967465)
That's never really been true. Graphic designers rarely knew HTML/CSS in the old days.
Re:Things every React.js beginner should know by Moru on Monday January 25, 2016 @01:28PM (#10968870)
I still get everything as a .jpg. Or a photoshop image if I'm lucky.
Re:Things every React.js beginner should know by Can_Not on Thursday January 28, 2016 @10:22AM (#10988649)
I have given up on graphic designers that know HTML/CSS. I consider myself lucky if they use some GUI program that outputs clean HTML.
Re:Things every React.js beginner should know by ChrisArgyle on Monday January 25, 2016 @09:59AM (#10967475)
I remember this well. Especially when it came to browser compatibility.