6 ms·
Hooks are not pure functions, though, or purely functional. A function that returns different results when called with the same arguments is not pure in any sen
by mplanchard 6y ago
Hooks are not pure functions, though, or purely functional. A function that returns different results when called with the same arguments is not pure in any sense. One of my biggest issues with hooks is that they make it difficult to reason about which functions are pure and which are impure. Just because it's using functions doesn't mean it's functional.
See for example this comment [0] from the hooks RFC discussion how they could have been made more functional.
[0]: https://github.com/reactjs/rfcs/pull/68#issuecomment-433155682 https://github.com/reactjs/rfcs/pull/68#issuecomment-4331556...
- schwartzworld 6y ago> Just because it's using functions doesn't mean it's functional. I never said it was. I said that hooks are a way of abstracting functionality that can't be easily represented as pure functions. > they make it difficult to reason about which functions are pure and which are impure. Hooks have to have 'use' at the start of their name to be recognized by React. It should be instantly obvious which functions are pure and which aren't as long as you don't wrongly namespace your pure functions with 'use'.
- mplanchard 6y agoThanks for replying! I did not downvote your comment, and I don’t think it deserves it. I do disagree with it though. > hooks are a way of abstracting functionality that can't be easily represented as pure functions. Yes, this is true, but in what is (IMO) a much less semantically clear manner than classes or a monadic interface. If a HookMonad or something similar were passed into components as an optional argument, I’d have very few complaints about hooks. > Hooks have to have 'use' at the start of their name to be recognized by React. Is this architecturally true or just conventionally true? It seems like this would just be determined by what they name the functions, which needs to be enforced by convention, but I suppose JS is dynamic enough that they could actually require the names conform to some standard. Either way, it doesn’t prevent people from importing them with other names, and even if it did, digging through a large function searching for “use” function calls seems much less obvious than the presence of an optional function parameter or the fact that a component is a class.
- yazaddaruvala 6y agoHooks definitely meet the dictionary definition of the term “functional programming”. Like Wikipedia states, you’re conflating “functional programming” with “pure functional programming”. https://en.m.wikipedia.org/wiki/Functional_programming https://en.m.wikipedia.org/wiki/Functional_programming
- mplanchard 6y agoThanks for the clarification! The Wikipedia says they’re often used synonymously, and I’m definitely guilty of conflating the two
- flowerlad 6y agoSo is C language a functional programming language? Definition of functional (from Wikipedia page you linked to): In functional programming, functions are treated as first-class citizens, meaning that they can be bound to names (including local identifiers), passed as arguments, and returned from other functions, just as any other data type can. In C language you can do all that. But C is not a functional programming language at all. Neither is Hooks functional. Even assembly language has "functions" but it is not a "functional language".
- Summershard 6y agoC is a FP language. So are C++, C#, Java and JavaScript. They're not functional first like F# or Scala. A programming language can embrace multiple paradigms.
- flowerlad 6y ago> C is a FP language. Well then so is assembly language.
- Epskampie 6y agoFully agree, it’s so weird to read in many places that hooks are supposed to make react more “pure” while they are actually a way to make functions non-pure. Pure being defined like you do.