News for hackers. Stuff that matters.
Re:JavaScript Structs by afavour on Thursday October 10, 2024 @03:12PM (#41802341)
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... 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.

Re:JavaScript Structs by hinkley on Thursday October 10, 2024 @04:22PM (#41803150)
When 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.

Re:JavaScript Structs by afavour on Thursday October 10, 2024 @06:44PM (#41804342)
ES modules too would have been a great opportunity to cut out some of the outdated cruft.
Re:JavaScript Structs by leetharris on Thursday October 10, 2024 @04:10PM (#41803000)
> 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.

Re:JavaScript Structs by thot_experiment on Thursday October 10, 2024 @03:20PM (#41802451)
> but there's absolutely no reason to use var any more

Naw, var has function scope and hoisting, both of which are useful.

Re:JavaScript Structs by afavour on Thursday October 10, 2024 @03:28PM (#41802546)
Not in a sensible codebase
Re:JavaScript Structs by hinkley on Thursday October 10, 2024 @04:26PM (#41803204)
My 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.

Re:JavaScript Structs by thot_experiment on Thursday October 10, 2024 @05:43PM (#41803886)
Sure and I'd accept the weaker claim that "there's no reason to use var in a production codebase touched by many people"
Re:JavaScript Structs by yakshaving_jgt on Friday October 11, 2024 @04:56AM (#41807654)
> 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.

Re:JavaScript Structs by hombre_fatal on Thursday October 10, 2024 @04:51PM (#41803458)
You should probably accompany this kind of claim with a code snippet to show what we're missing out on.
Re:JavaScript Structs by int_19h on Thursday October 10, 2024 @06:45PM (#41804344)
Useful for what?
Re:JavaScript Structs by dunham on Thursday October 10, 2024 @06:13PM (#41804107)
> 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)[] = [];
Re:JavaScript Structs by nsonha on Thursday October 10, 2024 @07:13PM (#41804525)
I 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

Re:JavaScript Structs by afavour on Thursday October 10, 2024 @07:23PM (#41804584)
All of those feel like anti patterns to me. Much more difficult to read.
Re:JavaScript Structs by nsonha on Thursday October 10, 2024 @08:12PM (#41804866)
That'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.

Re:JavaScript Structs by wruza on Friday October 11, 2024 @05:30AM (#41807843)
The 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.

Re:JavaScript Structs by goodoldneon on Friday October 11, 2024 @09:09AM (#41809159)
If performance is so important for your app that `var` is causing issues then JavaScript is likely the wrong language
Re:JavaScript Structs by yakshaving_jgt on Friday October 11, 2024 @04:48AM (#41807607)
> 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.

Re:JavaScript Structs by conartist6 on Friday October 11, 2024 @10:21AM (#41809708)
"let" is one of the good parts. Just don't use const or var.
Re:JavaScript Structs by yakshaving_jgt on Friday October 11, 2024 @10:38AM (#41809862)
Why is it? Why is block scoping useful?
Re:JavaScript Structs by colejohnson66 on Friday October 11, 2024 @03:13PM (#41812554)
Because it's how almost every C-syntax-like language works. JavaScript is the odd one out.
Re:JavaScript Structs by yakshaving_jgt on Friday October 11, 2024 @03:19PM (#41812613)
I’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.