News for hackers. Stuff that matters.
Re:A Visual Guide to React Mental Models: UseState, UseEffec... by discreteevent on Friday May 29, 2020 @09:26AM (#23349697)
In part 1 they talk about everything being a function including react components and closures. But I come from the "closure is a poor mans object and a function is a poor mans closure" perspective. In other words everything is an object. This is most likely because I started programming in the early 90s when first-class functions were not that common in popular languages (C had function pointers but it wasn't pretty)

But either way it works fine. Everything is a first class 'thing' that has properties that can be parameterised. If the 'thing' is an object the properties are alive and can be mutated (if mutable) for the lifetime of the object. If the 'thing' is a function the properties (arguments and local vars) are alive for the duration of the call.

When first class functions became mainstream though, I couldn't really understand the idea that they were fundamentally new compared to objects (in fairness this was only put forth by a few people)

Re:A Visual Guide to React Mental Models: UseState, UseEffec... by Quekid5 on Friday May 29, 2020 @03:02PM (#23354146)
I lean towards closures having primacy. FTR, I learned OOP way before I'd ever heard of functional programming.

My reasoning is based on two things: Firstly, objects have identity and this is a superfluous notion for most cases. Functions just "do things" to their inputs and return the result. Secondly, OOP is fundamentally predicated on the notion of mutation[0], but I think that is totally antithetical to local reasoning (chunking). The latter may just be a preference, but IME it seems to bear out. Mutation seems to be a consequence of the focus on identity, now that I think about it after writing that footnote.

> I couldn't really understand the idea that they were fundamentally new compared to objects (in fairness this was only put forth by a few people)

Just a historical note: First-class functions were invented waaaaay before OOP was a gleam in somebody's eye.

[0] The only non-mutating OOP system I know of is O'Camls version of OOP, but it's not very popular because it's just... unnecessary. It also does away with "identity", now that I think about it.

Re:A Visual Guide to React Mental Models: UseState, UseEffec... by 7786655 on Friday May 29, 2020 @03:55PM (#23354736)
In Javascript, closures both have identity and are mutable, which is a huge pain when using React.
Re:A Visual Guide to React Mental Models: UseState, UseEffec... by ss3000 on Saturday May 30, 2020 @01:27AM (#23358711)
I would say the biggest JavaScript inflicted pain when working with React is the lack of a notion of value equality in the default mutable collections.

Coming from a ClojureScript background, it pains me to have to deal with all the idioms in React that hack around this fundamental impedance mismatch by forcing users to carefully maintain reference equality for collections between renders for perf optimizations, and more recently for correctness in hooks like useEffect.

Re:A Visual Guide to React Mental Models: UseState, UseEffec... by Quekid5 on Wednesday June 03, 2020 @06:33PM (#23409716)
I realize I was quite off-putting in my original reply, but "have identity" in what way, exactly? A memory address?
Re:A Visual Guide to React Mental Models: UseState, UseEffec... by Quekid5 on Friday May 29, 2020 @04:52PM (#23355394)
JS... the "winner" of the Accidental Complexity game. (It was made in a couple of weeks, so it's excusable, but... it just hurts so much that this was the language that conquered the web :| )
Re:A Visual Guide to React Mental Models: UseState, UseEffec... by eitland on Friday May 29, 2020 @05:30PM (#23355785)
Yes, and realizing there is probably a larhe subset of people who'd say they love JS but hate PHP can make a man who know both too well confused. Or at least aware of the hypocrisy ;-)
Re:A Visual Guide to React Mental Models: UseState, UseEffec... by jchw on Friday May 29, 2020 @09:30PM (#23357632)
To be fair, PHP5 really was quite a nightmare for honestly a lot of reasons that JS was never. I think most people remember PHP5 and compare it to fairly modern JS, but I must say, modern PHP is a lot better than older PHP (i still prefer just about anything else.)
Re:A Visual Guide to React Mental Models: UseState, UseEffec... by eitland on Saturday May 30, 2020 @07:28AM (#23360375)
Totally agree.

But comparing modern Javascript to ancient PHP would be even more unreasonable.

Re:A Visual Guide to React Mental Models: UseState, UseEffec... by jchw on Saturday May 30, 2020 @02:06PM (#23363210)
Yes, to be clear, I agree. I think most people just don’t realize PHP actually did improve. I’ll admit until digging into a modern PHP library I didn’t even believe it could be meaningfully better.
Re:A Visual Guide to React Mental Models: UseState, UseEffec... by obedm on Friday May 29, 2020 @09:40AM (#23349852)
Thank you for reading both articles! (author here).

When I write these sometimes-overly-simplified-articles I'm terrified of explaining something wrong, or that my knowledge may be incorrect on a very specific point.

I really like the way you express "everything is a first class 'thing'", I never thought of it that way.

Re:A Visual Guide to React Mental Models: UseState, UseEffec... by jlelonm on Friday May 29, 2020 @07:34PM (#23356879)
FWIW I thought they were both pretty spot on. In fact, you mentioned this about DNS:

"As I read articles and tried stuff out (and broke my server config more than once) I started to grasp the system, to get glimpses into how it all worked, until eventually it “clicked” and I felt comfortable working with it."

Have you written about your mental models for DNS? Color me interested!

Re:A Visual Guide to React Mental Models: UseState, UseEffec... by obedm on Saturday May 30, 2020 @06:45AM (#23360164)
That's something I hadn't considered before. That's probably something that a lot of people would find valuable.

Out of all the questions my students ask me, there's always confusion about the way servers talk to each other, or what they even are.

I'll definitely write about this now haha. Thank you for the idea :)

Re:A Visual Guide to React Mental Models: UseState, UseEffec... by pacala on Friday May 29, 2020 @11:02AM (#23350727)
"closure is a poor mans object and an object is a poor mans closure" is true in principle. Devil's in the details. If the language forces the declaration of the object equivalent of a closure in a different scope, or even a different file, then massaging captured variables by hand, it makes objects a whole lot more laborious to use, especially for those using rapid iteration to get a feel of the design space.
Re:A Visual Guide to React Mental Models: UseState, UseEffec... by jayd16 on Friday May 29, 2020 @01:21PM (#23352767)
I can see why objects and closures are reducible to each other. As a Java dev who's written listener objects, closures seem like simple sugar to me. They hold private state or references to state. Functions can be passed references to mutable data so they are equivalent as well.

However, there is the concept of a pure function (one that is unaffected by and causes no side effects) which I think can be argued is fundamentally different.