News for hackers. Stuff that matters.
Re:An experienced Javascript developer’s account of learning... by olavgg on Saturday April 22, 2017 @09:19AM (#14172350)
At our company, we have banned React, Angular and other "overengineered" JS frameworks.

Our HTML is rendered from a standard boring Groovy server page, with some vanilla-js for DOM manipulations here and there.

We have hired developers that were React fans, which quickly changed opinion that this old-school way for developing web applications is indeed better. It is faster to code, an order of magnitude easier to maintain, DOM rendering time is faster, so a click to open a new page/view happens instantly. Our web application is so fast, that non tech people and clients always comment how smooth and slick it behaves compared to Facebook or LinkedIn.

Our clients are very happy with our solution too, as it is very simple for their developers to integrate our solution into their web application since it doesn't mess up their existing web framework. It doesn't block their rendering time, and it is easy for them to override changes.

We also have one complex UI part in our application, which can consume some CPU. This part we render with canvas, and it feels like a native app as it runs stable at 60fps. Who wants to run a lot of slow DOM manipulations when canvas is a lot more efficient.

Re:An experienced Javascript developer’s account of learning... by jasim on Saturday April 22, 2017 @10:10AM (#14172583)
If your web-app has even a modicum of complexity on the client side, the vanilla DOM based solution is going to get unwieldy real fast.

Consider a simple list of elements to which you can add or delete items. With vanilla DOM, you’ll have to write handlers that will `$.find` the particular element, and then run a `$.remove` to remove it. To insert something, you’ll have to do a manual `$.appendHTML`. And when you have to send the data over to the server, you have to iterate over the elements, collect it into an array and send it across. But if you used something that had an explicit notion of the application state, and allowed you to express your view as a simple function of your state, all you would have to write is an `array.push` and another `_.remove(array)`. The view will update automatically because we’ve already told the 'over-engineered' framework what to render for any given state.

But you could avoid framework and still not fall prey to spaghetti DOM manipulation. All you need is to keep an explicit state variable and have a single `render` function to deal with the DOM. This method will be invoked everytime you change your state, and it'll recreates the necessary DOM from scratch. It is a simple straight-forward implementation, but now you're going to lose cursor position, and with large enough DOM trees, the lag will be noticeable.

To fix this, you can add a virtual-dom implementation that'll maintain the DOM tree in memory and avoid re-rendering things that haven't changed.. and then you can have some sort of immutability checker to skip even the virtual-dom rendering part. And by now you have a home-brewed over-engineered React-like framework, but without any of its documentation or robustness or community.

We definitely need to be conservative in our choice of tools, but React today is a conservative choice to build rich front-end web applications. More than a framework, it is an approach to user interfaces, and it is a damn good one too.

Re:An experienced Javascript developer’s account of learning... by DenisM on Saturday April 22, 2017 @10:23AM (#14172645)
To be fair diffing virtual dom is not the only way - you could also be diffing the model and only update the parts which need it. Knowing the model well allows you write an efficient diff. The problem of course is that dom diff is universal while model diff is specific to application.
Re:An experienced Javascript developer’s account of learning... by lukaszkups on Sunday April 23, 2017 @03:03AM (#14176936)
or operate on document fragments
Re:An experienced Javascript developer’s account of learning... by olavgg on Saturday April 22, 2017 @11:04AM (#14172864)
No adding/removing/modifying elements in a list is very simple and fast, even with thousands of elements.

Example: https://pastebin.com/Fu39i47z

Re:An experienced Javascript developer’s account of learning... by drdaeman on Saturday April 22, 2017 @11:42AM (#14173034)
The good part about React is that given a `messages` data structure and a `render` function, it implements `addMessage`/`addMessageBefore`/`updateMessage`/`removeMessage` on its own. Writing once and not multiple variants of the same is good. Another good thing is that that there are a lot of components someone already published, that are ready to use (immediately, or after a few small fixes/PRs).

The bad part is that React has a lot of code on its own, and somewhat strongly suggest use of even more. It's orders of magnitude more than that your `{add,update,remove}Message` snippet has. While React tries its best to hide all that complexity, it's still there. Designing, writing, reading, debugging and rewriting (if necessary) those few functions is all trivial (but somewhat routine) - not so much with React.

Re:An experienced Javascript developer’s account of learning... by platz on Saturday April 22, 2017 @12:32PM (#14173267)
all the add/remove list stuff can be implemented by a simple templating library (handlebars/mustache) which doesnt mess up the whole client side architecture
Re:An experienced Javascript developer’s account of learning... by drdaeman on Saturday April 22, 2017 @12:51PM (#14173384)
Oh, no, I'm sure that's incorrect.

Templating libraries can't update documents, they can only overwrite them completely. This either loses state (entered-but-unsaved text, cursor positions, <detail> collapse status, etc.), or requires a lot of watchers and extra state information.

Those DOM updates is the whole point of React.

There are other libraries that work in a similar fashion, but React has more traction thus reusable components and recipes available...

Re:An experienced Javascript developer’s account of learning... by platz on Saturday April 22, 2017 @01:02PM (#14173441)
no, the point of the template engine is not to push all the logic into the templates. as you say you still handle the state yourself. the template render just skips the messy output building step, which for many cases is all you need
Re:An experienced Javascript developer’s account of learning... by drdaeman on Saturday April 22, 2017 @04:26PM (#14174554)
Ah, I think I see.

React is a templating library. Nothing more, nothing less.

It takes a state ("props"), a template ("component") and renders the DOM tree. That's all it does. (Non-templating uses like react-router are kind of hacks, rather than intended use. YMMV, of course.)

However, it's more complex than previous-generation templating libraries (that had emitted strings, rather than operating on DOM trees), because DOM trees are stateful. This is why React is so huge, when compared to, say, Mustache.

(And, of course, it's really easy to mess things up by putting application logic right in the component class. And, I'd say, sometimes it's really tempting to do so. Mustache/Handlebars doesn't have this only because they're specifically designed to disallow such things.)

Re:An experienced Javascript developer’s account of learning... by platz on Saturday April 22, 2017 @06:17PM (#14175149)
no disagreement on what react is. yes, I understand react can be used just to template (no one uses it this way). yes, react is powerful. yes, react provides a good mechanism for handling state and events. what i am saying though is that for some apps, you can get by with simpler templating and avoid the conplexity/power react gives you, handling state and events yourself manually. does it sound crazy? if you know it will work for your particular use case, i dont think its crazy, and you avoid a bundle of complexity that react gives you, in essesnce you own the complexity now
Re:An experienced Javascript developer’s account of learning... by drdaeman on Sunday April 23, 2017 @09:01AM (#14177986)
> react can be used just to template (no one uses it this way)

I don't know about the others but I do. Got bad experience when I've shoved logic where it didn't belong, don't repeat the same mistake again.

As the templating "got data, want a form, a table and a chart" engine it's good - exactly in a sense that I can write one template instead of multiple partials (add+update+remove). The complexity hidden underneath doesn't strike my fancy, but... it works, and saves coding time - especially because a lot of components are already written by someone else and is available on npm.

Still, it's really really easy to shoot yourself in the leg. And I found a fair share of tutorials and examples teaching those sort of things.

> react provides a good mechanism for handling state and events

No, it doesn't have those. Unless you count react-router/Redux/Mobx/whatever as React-the-umbrella-naming.

Well, there is some basic internal state management (via constructor + `setState` + `componentWillReceiveProps`) but that's about it, and IMHO it best very used conservatively, for approximately the same reasons why having logic in, say, Jinja2 templates isn't the best idea in Python world. Or maybe I just don't know how to cook stateful components properly, but last time I made a stateful component I ended up rewriting it some weeks later, extracting the state away, because it became a problem.

There is no event handling at all - except for the "props are changing, this is going to update" event, of course. All React has is `onClick`-like attributes and handlers bound to those are best left as some dispatch calls to the outside, with no logic inside the React component code whatsoever.

Re:An experienced Javascript developer’s account of learning... by zepolen on Saturday April 22, 2017 @02:01PM (#14173741)
I don't think you understand what OP is talking about.
Re:An experienced Javascript developer’s account of learning... by platz on Saturday April 22, 2017 @03:07PM (#14174157)
I think you're expecting a simple templating engine to do more than I'm claiming it's doing.
Re:An experienced Javascript developer’s account of learning... by csallen on Saturday April 22, 2017 @11:34AM (#14172998)
jasim isn't complaining about performance, but about the complexity of the code.
Re:An experienced Javascript developer’s account of learning... by valarauca1 on Saturday April 22, 2017 @01:01PM (#14173435)
Parent's paste bin is ~40 lines of code (with white space) consisting of 4 functions, with what? 4 loops.

If you consider 40LOC complex I don't understand why you work as a dev.

Re:An experienced Javascript developer’s account of learning... by jasim on Saturday April 22, 2017 @02:11PM (#14173811)
> If you consider 40LOC complex I don't understand why you work as a dev.

Goodness, your snark that questions the parent's competence is not only unwarranted, it is based on an incomplete understanding of the value of abstractions in programming.

It is not about 40 lines of code, it is about a basic programming model that allows us to understand and create user interfaces irrespective of how complex or large it is. It lets you compose your UI based on a tree of state and pure functions. If you don't have this abstraction, you do not have a coherent programming model to build UIs.

Re:An experienced Javascript developer’s account of learning... by drdaeman on Sunday April 23, 2017 @09:13AM (#14178016)
Actually, I wonder if there's something out there that has all the declarative state-to-DOM manipulation goodness, but does this at build time.

React is a runtime that works on the client, and its compile-time stuff is just syntax sugar (JSX) and some minor optimizations. As I understand, this runtime is the complexity that some complain about. At least I perceive this as a downside.

If there's something that can, given a component definition, generate those sets of add/update/remove functions and appropriate calls on state updates (preferably, as human-readable as possible) - I think that would be interesting to see and compare.

(I hope no one normally dynamically mutates their components at runtime?)

Re:An experienced Javascript developer’s account of learning... by jasim on Sunday April 23, 2017 @11:03AM (#14178420)
https://svelte.technology/ is what you're looking for.

I haven't ever used it because I find React quite adequate. I share your concern about the complex runtime layer that React introduces, and it is getting even more complex with the introduction of Fiber.

However, the test of an abstraction is whether it leaks undesirably, and whether it lets you escape to the lower layers when you need to. React scores well on both counts.

So while the implementation might be complex and will get even more elaborate over time, the abstraction is as water-tight as I can hope. In that case complex implementations with better performance might even be a good thing.

Also, I strolled through the React codebase in its early days and found it eminently understandable. So if it ever comes to it, I'm confident I can contribute a patch for the things I need with enough time and effort.

Re:An experienced Javascript developer’s account of learning... by titobrown on Saturday April 22, 2017 @12:03PM (#14173127)
Replies like this are always interesting to me. It seems like you're saying these "overengineered" JS frameworks are useless, but I can't believe thats the case. You must just be saying they don't fit your use case, correct? If so you are not getting that point across very clearly.

Otherwise are you honestly saying you can't see the use case for these frameworks? You would rebuild Facebook with static HTML and a sprinkling of js? You believe all these very smart people who created, worked on, and use Angular, React, Vue, etc. are just way off base and should go back to your method?

Re:An experienced Javascript developer’s account of learning... by olavgg on Saturday April 22, 2017 @12:35PM (#14173285)
There is a story called The Emperor's New Clothes, the morale of that story is that you should question your decisions and not blindly believe what other people say, even if you look up to them.

When I saw the "light" about React, me and friend were coding our own e-commerce system each for fun. Him with Ruby and Rails and jQuery, me with Grails and React. When we implemented the shopping cart, I wrote like 200 lines of code in React that would dynamically update that shopping cart. My friend did it with just 5 lines with jQuery. Yeah most of my React code was boiler plate, but still...

What I do, is not at Facebook scale, complexity or size. I have no idea about how challenging their issues are technically. What I do know as a user of Facebook though, is that Facebook is annoyingly slow these days, and makes my Macbook pro fan spin at full power.

Re:An experienced Javascript developer’s account of learning... by vladgur on Saturday April 22, 2017 @02:40PM (#14174001)
You are comparing apple to oranges. Why use react or angular or vue with fun hobbyist projects meant for a team of one developer that is going to throw that code out and move on to another hobby.

In the real world you're going to have to come up with a homegrown vanilla approach for all the things that a good framework does for you: escaping user input, provide setup and cleanup hooks for your DOM manipulations, maintain state and I'm not even touching routing because you seem to be focused on server-rendered apps although the community does prefer hybrid apps that start out server-rendered and load more data while the user is browsing.

I've worked in companies that started out homegrown and it's a bitch to maintain, evolve and for that matter teach to new hires.

Consistency matters on teams

Re:An experienced Javascript developer’s account of learning... by RodericDay on Saturday April 22, 2017 @12:48PM (#14173366)
> What I do know as a user of Facebook though, is that Facebook is annoyingly slow these days, and makes my Macbook pro fan spin at full power.

i can't echo this sentiment strongly enough

Re:An experienced Javascript developer’s account of learning... by xmatos on Saturday April 22, 2017 @05:54PM (#14175040)
I've said it before here, but the best web app I've built was basically Django, with bare minimum js to handle in-page effects.

I've been working on a project which is being ported to React for no reason, just because it's shiny, and it's been worst than expected.

Bundling the entire website into a huge js file doesn't make any sense. In practice, every time you make the slightest change, like adding a div, or changing a class name, your users have to re-download the entire thing.

Webpack takes 1GB of memory and 90 seconds to build it. Firefox Dev Tools eats all my ram if I try to reload the page with it open. And Redux is just bad. So much boilerplate for a dynamic language, with constants and if clauses to decide which action to dispatch. Apparently, it's ok to have presentation logic in a component, but god forbid internal state!

I do believe the reason front-end devs praise it so much is because it allows them to work solely in js, despite the end product being slow and heavy.

I always compare reddit's mobile site, written in react, with Hacker News. The first takes around 7 seconds to load on my cel phone, while the latter loads instantly.

No one but frontend devs care about js. The web hasn't become js centric and I bet it never will. It has become mobile first and all you need for 90% of that is css.

I don't know why fb and google spend money building these frameworks, but maybe because of this:

https://www.joelonsoftware.com/2002/01/06/fire-and-motion/

"But the end result is just cover fire. The competition has no choice but to spend all their time porting and keeping up, time that they can’t spend writing new features.

Look closely at the software landscape. The companies that do well are the ones who rely least on big companies and don’t have to spend all their cycles catching up and reimplementing and fixing bugs that crop up only on Windows XP.

The companies who stumble are the ones who spend too much time reading tea leaves to figure out the future direction of Microsoft. People get worried about .NET and decide to rewrite their whole architecture for .NET because they think they have to. Microsoft is shooting at you, and it’s just cover fire so that they can move forward and you can’t..."

Replace Microsoft with Google or Facebook...

Re:An experienced Javascript developer’s account of learning... by ng12 on Saturday April 22, 2017 @08:24PM (#14175694)
If you can build your application without a framework, do it. It's the right call every time. Not every web project calls for React, but for the ones that do it's a god-send. The alternative to me is not a minimal Django app -- it's nigh-unmaintainable jQuery spaghetti.
Re:An experienced Javascript developer’s account of learning... by noway421 on Saturday April 22, 2017 @11:49AM (#14173058)
Thank you. This is true, the frameworks are not only trying to speed up the performance, but first and foremost abstract from the DOM. And for quite a while they've been failing on delivering that performance. Things get better with react server side rendering and the like, but they still need tons of javascript loaded alongside the nicely rendered html which still makes it quite laggy.

Recent example of it is gitly.io. It is a beatiful web service which is I believe written in go. I just looked it up and it merely uses 200 lines of vanilla js. When I'm logged in and actually browsing the file tree.

Re:An experienced Javascript developer’s account of learning... by rpedela on Saturday April 22, 2017 @09:44AM (#14172464)
React is for complex/dynamic UIs. By using canvas, you kind of admitted that the old-school way isn't good enough for that case. Having said that, if canvas works and is better than React for your case, then great! Use whatever works.
Re:An experienced Javascript developer’s account of learning... by olavgg on Saturday April 22, 2017 @11:27AM (#14172967)
Its more like that we needed to display A LOT of data, that you also needs to interact with, much more than you can fit in a screen. So we present this with a simple way to navigate this data by using simple pan and zoom operations! Our clients love this!
Re:An experienced Javascript developer’s account of learning... by shouldbworking on Saturday April 22, 2017 @12:23PM (#14173227)
I had a client request we put an entire 10k+ searchable and sortable product table on a single page (with thumbnails for every product). After much reservation and trying to convince them otherwise, we just did it.

After a few seconds of the browser creaking the page renders fine and runs great. This is a page with over 10000 images on it.

I highly doubt your canvas approach is needed.

Re:An experienced Javascript developer’s account of learning... by jhedwards on Saturday April 22, 2017 @01:35PM (#14173616)
But how do you handle the encapsulation of views? If I am working on a React app and I want to add a new interface, say a simple view that displays a record and has some controls on the left side, I might write something like this:

  <Panel>  
    <Left>  
       <ControlPanel record={record} />  
    </Left>  
    <Right>  
       <RecordView record={record} />  
    </Right>  
  </Panel>
This view would be injected into the frame of the app that already has a navbar and footer, the layout would be pre-built via my panel components Left and Right, and if I want to change the control-panel I can go to that file and make any changes I want so long as the API stays the same. Similarly, if the schema changes and we need to update the way we display records, I would go to the RecordView component and change it because it is a decoupled module. I just don't see how you can accomplish this with vanilla HTML and JS.
Re:An experienced Javascript developer’s account of learning... by amnah on Saturday April 22, 2017 @09:32AM (#14172403)
Just curious, how do you handle the vanilla-JS stuff?

1) inline with your view files

2) write them in separate js files and include them depending on the page

3) concat them into one big js file which gets included on every page

4) something else?

Re:An experienced Javascript developer’s account of learning... by olavgg on Saturday April 22, 2017 @11:10AM (#14172893)
Yes, we do a lot of inlining at the end of a view. In every case when the javascript is only usable for that view we find this very clean, every developer can immediately see what javascript is running on that particular view.

We have our own small library for the more common operations, for small things like more effecient navigating of the DOM three or more complex "components" that for example convert a select element to a nicer selectbox with built in full text search. That library is included for every view. This library weight around 50kb gzipped.

Re:An experienced Javascript developer’s account of learning... by noway421 on Saturday April 22, 2017 @11:52AM (#14173071)
Some companies go for that 'disciplined javascript' style. It's a nice paradigm to follow, but every time you'd want to make it slightly more advanced (as the app grows) you'd have to implement tooling on your own.
Re:An experienced Javascript developer’s account of learning... by BjoernKW on Saturday April 22, 2017 @09:34AM (#14172411)
Might I ask what kind of applications you're developing?

While I generally like the idea of doing web applications the old-fashioned way depending on the requirements manipulating the DOM using vanilla JavaScript can quickly get out of hand.

Re:An experienced Javascript developer’s account of learning... by olavgg on Saturday April 22, 2017 @11:13AM (#14172908)
We are creating a data management framework for AI-trainers, which are people creating data and analyzing data for our chatbot.
Re:An experienced Javascript developer’s account of learning... by wavefunction on Saturday April 22, 2017 @09:23AM (#14172367)
Angular/Angular4 is a joy to develop with, so that's too bad you're limiting yourselves artificially.

We have groovy in my stack at work and everyone despises it. Fortunately that's a legacy we're moving away from.

Re:An experienced Javascript developer’s account of learning... by izacus on Saturday April 22, 2017 @09:44AM (#14172463)
But is it joy for customers to use the stuff you wrote in it? So many of these opinions always focus on the developers having fun instead of wondering what experience is for the user and maintainers.
Re:An experienced Javascript developer’s account of learning... by vladgur on Saturday April 22, 2017 @03:14PM (#14174198)
It's a valid question that has nothing to do with a tool/framework and everything to do with product goal and time/resources
Re:An experienced Javascript developer’s account of learning... by TomMarius on Saturday April 22, 2017 @04:06PM (#14174459)
95% of the time I can significantly improve the user experience thanks to React.
Re:An experienced Javascript developer’s account of learning... by bollockitis on Saturday April 22, 2017 @11:18AM (#14172933)
Out of curiosity, what does everyone despise about Groovy?
Re:An experienced Javascript developer’s account of learning... by virmundi on Saturday April 22, 2017 @02:10PM (#14173800)
It's not the new hotness anymore. Scala came along. The author of Groovey says Scalia is better. It's also semidynamic. So if you don't like that, you won't like that (tautology of the day).

Updated for Scalia to Scala.

Re:An experienced Javascript developer’s account of learning... by vorg on Saturday April 22, 2017 @06:48PM (#14175307)
> The author of Groovey says Scalia is better.

You mean James Strachan? That was 8 years ago. In November last year he said "I still love groovy (jenkins pipelines are so groovy!), java, go, typescript and kotlin. I don't use Scala anymore." [1]

Sounds like he uses Kotlin instead of Scala, and only uses Apache Groovy in Jenkins pipelines. And given that the Groovy collections methods are disabled so they don't actually work in Jenkins, I imagine he uses Java 8 or Kotlin for that purpose.

[1] https://twitter.com/jstrachan/status/784333918078169088

Re:An experienced Javascript developer’s account of learning... by bobwaycott on Saturday April 22, 2017 @03:40PM (#14174329)
Do you mean Scala? I got a good a chuckle imagining the late Justice Scalia here. :)
Re:An experienced Javascript developer’s account of learning... by thaiphanvevo on Saturday April 22, 2017 @11:33AM (#14172996)
I have never heard anybody say that Angular 2+ is a joy to work with before. What about it do you like?