News for hackers. Stuff that matters.
Re:If Not SPAs, What? by switz on Wednesday October 28, 2020 @09:34PM (#24926161)
> developing became more and more inefficient

Anecdotally I find the opposite to be true. I've been writing frontend code for over a decade, but I've never moved faster and wrote less buggy code than now. Is that because I've become a better developer? Sure, a little bit. But by and large, I don't believe that ultimately is the reason. I think it's the maturity in the technology. My growth as a programmer is hardly linear and the past 5 years have not matched the growth I achieved in my first 5. Frontend tooling has never been better than it is today.

What I believe, is that the bar to build web applications has been lowered, and there are more programmers than today than ever before. You have people who are not experts in frontend development and javascript trying to build complex UIs and applications. So you take this person who doesn't have the requisite experience and put them to work on a paradigm with a lot of depth (frontend) using frameworks that are really simple and easy to get started with, but compound problems as they are misused.

Another factor is that since SPAs are stateful, complexity mounts aggressively. Instead of a refresh on a stateless page every few seconds, one page causes bugs that rear their head for the duration of the session. These inexperienced people are put in charge of designing codebases that don't scale and become spaghetti. But when designed properly, these problems are largely negated.

I'm not advocating that SPAs are the solution to all problems. I think there's gross overuse of SPAs across the industry, but that is not an indictment of SPAs themselves. That is someone choosing the wrong technology to solve the active problem.

With respect to angular (1, I never touched 2) specifically, I always found it extremely overengineered, poorly designed, with terrible APIs. But that's a problem with that specific framework and says nothing at all about SPAs at all.

Re:If Not SPAs, What? by Nextgrid on Wednesday October 28, 2020 @10:49PM (#24926580)
> Frontend tooling has never been better than it is today.

What's the library or design pattern to consume a REST API in React or any of the mainstream front-end frameworks? The only thing I'm aware of is Ember Data but Ember is apparently not cool anymore, and I couldn't find a suitable replacement.

I'm asking because in all the projects I've been involved with, consuming the backend API always felt like a mess with lots of reinventing the wheel (poorly) and duplication of code. I can't believe in 2020 there's not some kind of library I can call that will give me my backend resources as JSON and transparently handle all the caching, pagination, error handling (translate error responses to exceptions), etc and people have to do all this by hand when calling something like Axios.

In contrast, Django REST Framework handles all that boilerplate for me and allows me to jump right into writing the business logic. It's insane that ~30 lines of code with DRF (https://www.django-rest-framework.org/#example) gives me a way to expose RESTful endpoints for a database model to the web with authentication, pagination, validation, filtering, etc in a reusable way (these are just Python classes after all) but the modern front-end doesn't have the client equivalent of this.

Re:If Not SPAs, What? by Merad on Wednesday October 28, 2020 @11:37PM (#24926924)
> I'm asking because in all the projects I've been involved with, consuming the backend API always felt like a mess with lots of reinventing the wheel (poorly) and duplication of code. I can't believe in 2020 there's not some kind of library I can call that will give me my backend resources as JSON and transparently handle all the caching, pagination, error handling (translate error responses to exceptions), etc and people have to do all this by hand when calling something like Axios.

If you look at 20 REST API's you'll probably see 30 different patterns for pagination, search/sort, error responses, etc. There have been a couple attempts to standardize REST such as OData but I think it's safe to say that they haven't been very successful. It's kind of challenging to build standard reusable front end tools when everyone builds back ends differently.

Re:If Not SPAs, What? by Nextgrid on Thursday October 29, 2020 @12:16AM (#24927155)
Ember has somewhat solved that problem though.

You have the concept of data adapters which would be clients for your API (you can make a custom one if extending the existing ones isn't an option) and the rest of the application just interacts with the equivalent of database models without ever having to worry about fetching the data. You could swap the data adapter without having to change the rest of the code.

We seem to have lost this with the move to React though, and even the hodgepodge of libraries doesn't provide a comparable replacement.

Re:If Not SPAs, What? by jamil7 on Thursday October 29, 2020 @02:56AM (#24927990)
Haven’t used it but aren’t there things that can connect to a swagger API spec and do some of the heavy lifting for you? I agree that the network layer in frontend is tedious to implement, things like GraphQL and Apollo attempt raise the abstraction level. What I would really like to see is something even more abstracted, e.g a wrapper around indexdb you can write to that syncs periodically over websockets to your server, more similar to the patterns we use on mobile.
Re:If Not SPAs, What? by richeyryan on Thursday October 29, 2020 @05:05AM (#24928534)
It seems that you are describing pouchdb: https://pouchdb.com/
Re:If Not SPAs, What? by jamil7 on Thursday October 29, 2020 @05:09AM (#24928553)
You're right Pouch completely slipped my mind, it's a great solution. But what about something more generic on the backend that wasn't database specfic, some sync engine you could put in front of whatever database you wanted. Can you do something like this with Pouch?
Re:If Not SPAs, What? by dragonwriter on Thursday October 29, 2020 @03:41AM (#24928141)
> What's the library or design pattern to consume a REST API in React or any of the mainstream front-end frameworks?

For React, its out of scope; anything that you can use in JS for this can be used. If you are using a state management library, that's probably more relevant to your selection here than React is.

REST is also too open-ended for a complete low-friction solution, but, e.g., if its swagger/openapi, there's tools that will do almost the entire thing for you with little more than the spec.

> The only thing I'm aware of is Ember Data but Ember is apparently not cool anymore, and I couldn't find a suitable replacement.

Ember Data is definitely a valid choice. It may not be hyped right now, but that has little to do with utility or use in the real world.

Re:If Not SPAs, What? by jeremyjh on Wednesday October 28, 2020 @10:58PM (#24926660)
The graphql frameworks - like Apollo - give you that. I haven't used it. For basic caching the state management frameworks work pretty well, but it is a lot of layers when you add Redux or Vuex to your stack. It works well for us though and I find it much easier to reason about than the old jquery spaghetti code style.
Re:If Not SPAs, What? by catlifeonmars on Wednesday October 28, 2020 @11:07PM (#24926729)
I hear you. I find myself needing to reinvent the wheel far too often to traverse the boundary between the client and server. I also feel that it shouldn’t be this hard. Apollo client and relay solve this problem for GraphQL APIs (quite nicely IMO). What’s missing is an Apollo client for non-GraphQL APIs.
Re:If Not SPAs, What? by place1 on Thursday October 29, 2020 @01:21AM (#24927524)
For what it's worth GRPC-Web is a pretty nice solution here.

My team generates backend stubs from our GRPC spec which allows us to jump right to implementing our business logic.

Frontend projects make use of the GRPC-Web client codegen to make calling the API simple and type safe (we use typescript).

We mostly use all the official GRPC tooling for this. We write backends in golang and dotnet core so GRPC-Web is supported quite well out of the box.

I wrote a slightly modified Typescript codegenerator to make client code simpler as well: https://github.com/Place1/protoc-gen-grpc-ts-web

Re:If Not SPAs, What? by rlt on Thursday October 29, 2020 @04:05AM (#24928239)
Yeah, after experiencing type safe APIs + editor integration with TypeScript I don’t think I can go back.

There are, of course, other solutions besides GRPC.

Re:If Not SPAs, What? by bredren on Thursday October 29, 2020 @03:58AM (#24928203)
React Query.
Re:If Not SPAs, What? by Toine on Thursday October 29, 2020 @03:35AM (#24928125)
React-query.
Re:If Not SPAs, What? by save_ferris on Wednesday October 28, 2020 @10:54PM (#24926626)
> These inexperienced people are put in charge of designing codebases that don't scale and become spaghetti.

I think this is one area where front end tooling can be painful for the average dev. The bar to writing idiomatic JS for a given framework can get pretty high quickly, especially when you look at some of the really popular tools out there (i.e. redux).

Front end work has become so much harder to grok because the patterns around things like state management still have a lot of warts. The terminology of redux drives me crazy because it’s really difficult to explain things like reducers.

Re:If Not SPAs, What? by pwdisswordfish4 on Wednesday October 28, 2020 @11:42PM (#24926956)
What most people have in mind as "idiomatic JS" isn't that. It's usually meant to refer to some patterns that appeared and started getting popular around 8 years ago. And often, code written in this not-idiomatic way works _against_ the language and/or the underpinnings of the Web in general. It's just that the circles promoting the pseudo-idioms have outsized and seemingly inescapable influence.
Re:If Not SPAs, What? by rlt on Thursday October 29, 2020 @04:13AM (#24928282)
This is very vague. Can you give some examples?
Re:If Not SPAs, What? by pwdisswordfish4 on Thursday October 29, 2020 @08:35AM (#24929813)
The question asking for clarification is itself vague. Examples of which part?

Look at JS that's written for serious applications today, identify the stuff that you'd label as "idiomatic", and then look at code that was written 10 years ago for serious applications, and see if it matches what your conception of "idiomatic JS" is. Good references for the way JS was written for high-quality applications without the negative influence of the new idioms (because they didn't exist yet): the JS implementing Firefox and the JS implementing the Safari Web Inspector.

Examples of how "idiomatic JS" is often written by people who are working against the language instead of with it:

- insistence on overusing triple equals despite the problems that come with it

- similarly, the lengths people go to to treat null and undefined as if they're synonymous

- config parameter hacks and hacks to approximate multiple return values

- `require`, NodeJS modules, and every bundler (a la webpack) written, ever

- `let self = this` and all the effort people go through not to understand `this` in general (and on that note, not strictly pure JS, but notice how often the `self` hack is used for DOM event handlers because people refuse to understand the DOM EventListener interface)

- every time people end up with bloated GC graphs with thousands of unique objects, because they're creating bespoke methods tightly coupled via closure to the objects that they're meant for because lol what are prototypes

These "idioms" essentially all follow the same "maturation" period: 1. A problem is encountered by someone who doesn't have a solid foundation 2. They cross-check notes with other people in the same boat, and the problem is deemed to have occurred because of a problem in the language 3. A pattern is adopted that "solves" this "problem" 4. Now you have N problems

People think of this stuff as "idiomatic JS" because pretty much any package that ends up on NPM is written this way, since they're all created by people who were at the time trying to write code like someone else who was trying to write code like the NodeJS influencers who are considered heroes within that particular cultural bubble, so it ends up being monkey-see-monkey-do almost all the way down.

Re:If Not SPAs, What? by CptFribble on Thursday October 29, 2020 @11:52AM (#24931801)
Hi, I'm also a new JS coder, but I'd like to avoid becoming one of "those people" you're talking about. I've been struggling with exactly what you mention - how to find out the "correct" way to apply patterns/do relatively complex things, but all I get on search results are Medium articles written by bootcamp grads.

Can you recommend any sources of truth/books that can guide down the right path? Of course I'll be going through all the things you mention but I'm just curious if there's somewhere I can get the right information besides just reading through Firefox code, for example.

Thanks!!!

Re:If Not SPAs, What? by mercer on Thursday October 29, 2020 @02:33PM (#24933782)
I'd say Eloquent Javascript (available for free online, I think) is a good book to read. "You Don't Know JS" is also a good one!

Basically, go for anything that teaches you non-js-specific approaches as well as as a solid understanding of the fundamentals.

Re:If Not SPAs, What? by CptFribble on Thursday October 29, 2020 @04:11PM (#24935164)
Thank you! I'll check both of those out
Re:If Not SPAs, What? by d13 on Friday October 30, 2020 @02:57AM (#24939813)
And Crockford’s JavaScript: The Good Parts. Although and older book, JavaScript fundamentals never change and it describes a lot of those forgotten foundations.
Re:If Not SPAs, What? by howlinbash on Thursday October 29, 2020 @03:31AM (#24928110)
A good starting point to explaining reducers is that you are reducing two things into one.

A redux reducer: The action and the current state reduce into the new state. And of course it doesn't matter how many reducers or combined reducers your state uses - they're all ultimately just doing this.

This also works for Array.prototype.reduce(). You're reducing two things into one.

Re:If Not SPAs, What? by rlt on Thursday October 29, 2020 @04:11AM (#24928271)
The concept of a reducer isn’t the hard part of Redux... it’s designing your state, organizing actions/reducers/selectors, reducing (no pun intended...) boilerplate, dealing with side effects and asynchrony, etc.
Re:If Not SPAs, What? by mrh0057 on Wednesday October 28, 2020 @11:59PM (#24927043)
Redux is not idiomatic JavaScript though. It’s trying to make JavaScript immutable and have adt which it doesn’t. If you use elm, reasonml, rescript, etc this pattern is a lot easier to implement than with JavaScript.
Re:If Not SPAs, What? by will_pseudonym on Thursday October 29, 2020 @12:09AM (#24927096)
I wasn't readily familiar with the acronym ADT. It's "Algebraic Data Types" for anyone else in the same boat.

https://en.wikipedia.org/wiki/Algebraic_data_type

Re:If Not SPAs, What? by tartoran on Thursday October 29, 2020 @02:01AM (#24927722)
In CS it also refers to Abstract Data Types
Re:If Not SPAs, What? by will_pseudonym on Thursday October 29, 2020 @02:57AM (#24927992)
Ah, good call. Thank you! Here's a reference I found that explains the differences between those two concepts, as applied to Scala:

https://stackoverflow.com/questions/42833270/what-is-the-dif...

Re:If Not SPAs, What? by d13 on Friday October 30, 2020 @02:58AM (#24939820)
Yup, it’s beautiful in Elm and makes no sense in JavaScript, which doesn’t need it.
Re:If Not SPAs, What? by rawoke083600 on Thursday October 29, 2020 @02:37AM (#24927904)
I definitely agree you, F.E Tools has gotten a lot more mature and we have a lot further to go as well.

I'm primarily a backend developer and I think in general backend developers makes for "poor fronted devs". I'm talking about those "occasional" times the backend-dev needs to do some f.e dev work. Just because they don't know the tech as well, best practises and spend as much time with it as a dedicated F.E Dev. jQuery code written by the "occasional front-end dev" is kinda horrific in many cases.

Now please internet hear me. I'm not saying you can't write bad code in a JS-Framework. I'm saying it's usually less often and less bad - especially for non-dedicated f.e devs

Like crossing a street, just looking left and right won't guarantee you to be safe in your crossing, but it damn near makes it less probable.

If you are a shop with mostly backend-devs and don't want to invest in a F.E dev, you definitely should look into a js-framework.

*Svelte is always good start very small and bare bones.

Re:If Not SPAs, What? by ratww on Thursday October 29, 2020 @05:42AM (#24928714)
> If you are a shop with mostly backend-devs and don't want to invest in a F.E dev, you definitely should look into a js-framework.

That matches in my experience.

I worked in a shop with only backend developers and the frontend was an absolute buggy mess of jQuery on top of bootstrap. After migrating most of it to Vue I taught it to the team and all the experienced-but-frontend-shy developers started producing great frontend code by themselves.

Re:If Not SPAs, What? by rendall on Thursday October 29, 2020 @06:20AM (#24928906)
> Svelte is always good start very small and bare bones

I second this

The barrier to entry is lower than for React, and the results are great

Re:If Not SPAs, What? by avereveard on Thursday October 29, 2020 @04:06AM (#24928244)
> Frontend tooling has never been better than it is today.

eh. Swing in its golden age run circle around what we have now. Granted it's old tech now that we settled for in-browser delivery, but still:

- look and feels could do theming you can only dream of with css variables/scss

- serialize and restore gui states partially or whole, including listeners

- value binding systems vue can only dream of

- native scrollers everywhere you could style and listen to without the rendered complaining about passive handlers. - layout that didn't threw a fit about forced reflows

- unhappy with the standard layouts? build your own one as needed

- debug everything, including the actual render call that put an image where it is

- works the same in all supported os

browsers are an insufferable environment to work within compared to that, css is powerful and all but you get a system you can only work by inference, and were everything interferes with everything else by default, which works great to style a document and is a tragedy in an app with many reusable nested components.

Re:If Not SPAs, What? by doctor_eval on Thursday October 29, 2020 @05:03AM (#24928528)
Not to be mean, but I worked with Swing for ten years and it was absolute crap. Constantly dealing with resizing “collapsars”, GBLs, poor visual compatibility with the host OS, a fragile threading model and piles and piles of unfixed bugs and glitches was a nightmare. It might have worked if you had a specific end user environment but it was a PITA for anything else, and deployment was even harder.

There are a few things I definitely miss from my 20 years as a Java dev, but the half assed and under funded Swing UI is not among them.

Give me HTML+CSS+JS any time.

Re:If Not SPAs, What? by LoSboccacc on Thursday October 29, 2020 @08:42AM (#24929864)
well we're talking about the tooling, but point taken, swing wasn't perfect (totally grid bag 2007 short https://www.youtube.com/watch?v=UuLaxbFKAcc )

but it's not like it was that worse than flex bugs ( https://codepen.io/sandrosc/pen/YWQAQO still does render differently in chrome than firefox - which one is wrong is immaterial)

Re:If Not SPAs, What? by thu2111 on Thursday October 29, 2020 @12:28PM (#24932252)
Give me HTML+CSS+JS any time.

Sure! And Chrome is superbly tested.

But HTML/CSS/JS aren't anywhere near good enough to build GUIs of any complexity by themselves, so everyone layers tons of stuff on top. And then those ... those, people have plenty of complaints about too. But if they didn't use them those complaints would migrate to the underlying framework.

I mean, Swing may have had a fragile threading model (not sure what you mean by that really), but HTML doesn't have one at all. Not great!

Re:If Not SPAs, What? by doctor_eval on Thursday October 29, 2020 @05:38PM (#24936295)
I agree with you 100%, and was really just addressing the “swing is awesome” statement in the GP. I’ll take HTML etc over Swing any day, but I’m sure there are nicer alternatives if your deployment environment is native, e.g. SwiftUI (which I have no experience with)

There are plenty of things wrong with HTML & friends, await/async and webpack being my personal hair removers, but if we set that aside and just talk about the DOM as the API for the UI, it’s very robust, well documented and widely available. I don’t love it, but it works.

Re:If Not SPAs, What? by LoSboccacc on Friday October 30, 2020 @04:03AM (#24940082)
"swing tooling" thank you, don't misrepresent my argument.
Re:If Not SPAs, What? by icedchai on Thursday October 29, 2020 @09:28PM (#24938178)
HTML was never meant for building apps. We took a square peg (document markup language) and jammed it into a round hole (app development.) Most of the problems and frustrations with web development go back to this.

We've been using the wrong tool for the job for over 2 decades. Now it's everywhere and nobody knows any better. It's probably too late now.

Re:If Not SPAs, What? by bryanrasmussen on Thursday October 29, 2020 @04:00AM (#24928213)
>What I believe, is that the bar to build web applications has been lowered,

Yes, the bar to build web applications has been lowered. We can all build something on the level of GMail now.

The ability to build websites has been crippled, because you are often forced to build the sites using the tools suited to applications. As you and both the parent comment seem to agree on.

Re:If Not SPAs, What? by kochthesecond on Thursday October 29, 2020 @02:53PM (#24934097)
Yeah, there is some overuse of SPAs I agree, but havent anyone in this thread worked in older java monoliths with JSP or even good old Struts framework?? THEN you can see what inefficient development looks like.