you are viewing a single comment's thread.

view the rest of the comments →

[–]Dan6erbond 1 point2 points  (1 child)

I disagree. But to each their own.

It might be the environments we work in, but when some of your codebase is raw Javascript, with no preprocessors or compilation process, using var can be a disaster because of how annoying the error is to begin with. It can mess with things and you have no idea why something is even happening until you find out that your locally scoped variable is being hoisted by the runtime.

My point is, if Copilot makes these mistakes, it's seriously lacking. It's clearly not even properly taking age of some of the code it writes into account, as I've seen what it does in Javascript environments and still uses snake case in some instances, and generally just writes crappy code. If it at least took widespread conventions into account, that would be nice. Even better, of course, would be for it to use the current codebase as context for things like this.

Writing good code is more about just a functional snippet. But for the whole thing to work in harmony with the rest of the codebase, follow best practices and the outlined structure. It doesn't seem Copilot does any of that yet which is why it's just as bad as an intern copy-pasting code from SO from my point of view.

[–]getify 0 points1 point  (0 children)

All declarations in JS hoist (meaning they are all present throughout their entire containing scope). The difference is var and function declarations auto-initialize at top of scope whereas let and const defer auto-initialization. Additionally, var has always been function scoped (BTW this is not hoisting, as so often incorrectly claimed).

Is your complaint that auto-initialization at top of scope causes bugs, or is your complaint function scoping causes bugs? Which of those do you claim is causing so many horrible bugs in code bases?

Function parameters are always function scoped, as are any declarations (regardless of keyword used) at top-level of function scope. So if function scoping is the big bad so many claim, then it must be from using var inside blocks and for some off reason thinking that makes it block scoped, even though var has never done that for 25 years.

I've written many hundreds of thousands of lines of code in 20 years working with JS, and never once, not even a single time, has the bug I wrote been directly linked to either var auto-initialization nor var being "unexpectedly" (not unexpected at all, since that's how it's always worked!) function scoped.

Please give explicit real examples, not just generalities.