4 ms·
I think in this specific case it's JavaScript's requirement for backwards compatibility that bloats it... but there's a lot you can ignore. Like, you can declar
by afavour 2y ago
I think in this specific case it's JavaScript's requirement for backwards compatibility that bloats it... but there's a lot you can ignore. Like, you can declare a variable with var, let or const but there's absolutely no reason to use var any more. I feel similarly about the proposals to introduce records and tuples: https://github.com/tc39/proposal-record-tuple https://github.com/tc39/proposal-record-tuple... in most scenarios you'll probably be better off using records rather than objects, and maybe that's what folks will end up doing.
But boy does it all get confusing.
> Now, Typescript is on version 5.6 and there is so much stuff you can do with it that it's overwhelming. And nobody uses most of it!
I'm not so sure about that. I think we end up consuming a lot of these features in the TS types that get published alongside libraries. We just don't know it, we just get surprisingly intuitive type interfaces.
- thot_experiment 2y ago> but there's absolutely no reason to use var any more Naw, var has function scope and hoisting, both of which are useful.
- afavour 2y agoNot in a sensible codebase
- hinkley 2y agoMy job isn't to be infatuated with your code. It's to get through your code and get stories done. People don't really get better at handling the complexity of large code bases. We are fundamentally the same organic matter that existed prior to the first computer coming into existence. So as code bases and library bases grow larger and larger, they need to be proportionately easier to read or even ignore. Your code needs to be dead boring 90% of the time, otherwise you're imposing on your coworkers. And using variables before they're declared is just shitty behavior.
- thot_experiment 2y agoSure and I'd accept the weaker claim that "there's no reason to use var in a production codebase touched by many people"
- yakshaving_jgt 2y ago> Your code needs to be dead boring 90% of the time I also prefer boring code, but I think having the choice between var, let, and const is less boring than only having var.
- hombre_fatal 2y agoYou should probably accompany this kind of claim with a code snippet to show what we're missing out on.
- int_19h 2y agoUseful for what?
- leetharris 2y ago> I'm not so sure about that. I think we end up consuming a lot of these features in the TS types that get published alongside libraries. We just don't know it, we just get surprisingly intuitive type interfaces. Very true. As a downstream consumer, I can do all business logic in ancient, simple languages. But I'm sure these things are extremely nice to have for the more complicated upstream dependencies I rely on.
- hinkley 2y agoWhen they made class declarations imply strict, I thought that was a pretty wise move. But it might have been good if they applied more limitations than that, made them super-strict. Such as for instance making 'var' not work in class declarations.
- afavour 2y agoES modules too would have been a great opportunity to cut out some of the outdated cruft.
- dunham 2y ago> there's absolutely no reason to use var any more. So I also thought. And then I recently learned that typescript uses `var` internally for performance. From src/compiler/checker.ts: // Why var? It avoids TDZ checks in the runtime which can be costly. // See: https://github.com/microsoft/TypeScript/issues/52924 /* eslint-disable no-var */ var deferredDiagnosticsCallbacks: (() => void)[] = [];
- nsonha 2y agoI can think of a few when hoisting is nice, stylistically: if (...) var x = ... else x = ... ///// try { var x = ...} catch (error) { x = ... } ///// for (...) { var x: NodeJS.Dict<any> = {} x[key] = ... } return x
- afavour 2y agoAll of those feel like anti patterns to me. Much more difficult to read.
- nsonha 2y agoThat's subjective, Idk about "MUCH more difficult". All it does is moving the declaration to the correct visual scope, instead of a dangling up-front declaration. Admittedly, I understand most coders are aready trained to read the latter.
- wruza 2y agoThe worst anti-pattern here is the catch- and finally-blocks living in a different scope. Really, who thought it was a good idea that finalization and error handling blocks must have no access to their subject scope? Every damn language copies that nonsense, except for js and its `var` hoisting.
- goodoldneon 2y agoIf performance is so important for your app that `var` is causing issues then JavaScript is likely the wrong language
- yakshaving_jgt 2y ago> Like, you can declare a variable with var, let or const but there's absolutely no reason to use var any more. I am going to continue to use var for everything, because I think let and const are stupid. It is not cool or interesting to learn about new scoping rules introduced by let, and it is not cool or interesting that so many people — especially juniors, but not exclusively — are lulled into a false sense of security by believing const means the referenced value is immutable, which it isn't. I am going to continue to write small JavaScript, like from The Good Parts.
- conartist6 2y ago"let" is one of the good parts. Just don't use const or var.
- yakshaving_jgt 2y agoWhy is it? Why is block scoping useful?
- colejohnson66 2y agoBecause it's how almost every C-syntax-like language works. JavaScript is the odd one out.
- yakshaving_jgt 2y agoI’d prefer to just embrace it. Keep the language small. Prototypical inheritance works just fine. There’s enough in The Good Parts to get all the work done.