JS, people still use var?

Looking at javascript examples and source code for node red palette packages, the use of "var" to declare variables is rampant. I can only see a few reasons for this:

  1. People who write this code doesn't know any better.
  2. Muscle memory, old/bad habits.
  3. Ancient code not updated.

Bad code being so widespread will only cause fresh developers to make the same mistake, learning from the old.

This would easily be solved with a linter or even better, a formatter which auto-change to const/let on save.

Thoughts?

Missing point 4. "If it's working, don't touch it."

5 Likes

Maybe easy, but also time consuming. It requires someone to put the time in when there are other priorities.

2 Likes

Admittedly those configs are a hassle, and time is long overdue for an industry standard to copy-paste from. Chatgpt or similar should also be able to make something fairly decent instantly. Best case it's a matter of spending 10 min setting up a project before writing code. A one-time job. If you have done so already, just copy to new projects. More often than not, you end up saving that invested time down the line.

Haha don't touch it yeah, related to "ancient code not updated". You would have some extremely specific cases where changing "var" to "const"/"let" breaks something. Average node-red level is far below that level.

Plus all the re-testing, release notes, pushing to github releasing to npm, etc. And for what purpose?

For what purpose? You mean having a linter and formatter for your project? If you spent 10 min beforehand, you wouldn't need to do additional re-testing, release notes, push to github and npm release. If you do it afterwards, then yeah. Tests can be automated, so ideally that'll be no issue. Pushing to github takes literally seconds. No idea about the npm, probably automatable too? For changing var to let, my guess is those who used it unknowningly, it won't have any effect at all.

For var itself I get that you ask for purpose. But for general code quality, you think that has no value in itself?

I have many older function nodes using var and many newer function nodes using let and const.
As far as I know they all work fine.

If there was a tool to rewrite these old functions, what would I gain by using it?

That'll be " Ancient code not updated." :grinning: Cleaner code? Best-practice? You can write entire code on single line too if you want, still works fine. Yet using a formatter would be much better (with negligible cost in setup).

Well I'm sure that since you think this should be done, you could start making changes and submit PR to have the code changed.

1 Like

You don't agree that var shouldn't be used? Instead of fixing it, it would be far more practical to set up a CI/CD pipeline to prevent it ever happening in the first place.

Alas it would take much more than s/var/let/ to make my code resemble "best practice"

My question "what would I gain...?" was in fact a question and not the statement "I would gain nothing".
"Cleaner code" really tells me nothing.

I feel that what is important is to be able to read and understand code months down the line.
Actually I find "let" rather impairs understanding since I know other languages where let is an assignment not a declaration.

let someThing;

reads to me like a syntax error

I do try and use it though because people say it is in some mysterious way better.

Not sure I want to spiral down a var vs const/let debate, as I thought the community was in agreement on the topic. But here is clearly some doubt! I don't remember working with any other language that use let as assignment, but probably stuff like kotlin, f#, swift and rust? However in js (and more so in ts), using var actually reads like a syntax error. I stumbled upon it here with node red recently, accidentally using var myself, as I spent more time working in typescript earlier and then, as mentioned, these sorts of issues are automatically handled (so never thought about it). Until stumbling across one of many bugs due to incorrect use of var. Which is hard to read, particularly when coming from other languages (let's say c#, c++, java or even python?).

Your code may have many other problems than this which would be best suitable for another topic. But if you don't care it hardly matters? What matters is when you start publishing your code for others to read, learn from or even maintain. That's what triggered this discussion.

I meant for what purpose reworking and re-releasing my six year old code, which is currently working well? That is what you appeared to be suggesting.

I was absolutely not suggesting that new code (or old code that is being worked on anyway) should use var. I often point out to posters here that it is better to use let/const.

You are presumably to young to have experienced the joys of Basic. Lucky you!

4 Likes

Try this for example

// declare a variable x with function wide scope
var x = 3
var y = 4
// some code
if (x != y) {
    // declare a variable local to this block
    var x = 0
    // some more code
}
// set payload to the function wide variable
msg.payload = x
return msg;
2 Likes

Yeah agreed. This topic was not triggered by your old code hehe :smile: It is a very minor thing, but pops up regularly in code some publish for others to read, learn from or contribute on.

Woah!

The use of var isn't bad code!

An application that was written long ago using "old money" is not bad code, the style/choices are indeed dated, doesn't make it bad code!

A lot of developers starting in JS (who hasn't sat in a class room to study) are usually using the Interweb to gain examples, the internet doesn't purge so to speak, so examples are indeed used, and built upon.

So I guess this falls under Point 1??

Point 2: I'm not sure, when I learn a different trick, I like to show it off quite a bit :nerd_face:

Point 3: Like @hotNipi says, it works - there is no need to fix it (it's not bad code).

I, indeed like to make Sure I am not using old money, but I approach It as a tip for the user, and to update them.

Its a very critical topic - many many many views here :sweat_smile:

1 Like

No, I think that is too simplistic.

"It shouldn't be used most of the time."

There are a few edge cases where having the variable hoisted is very useful and saves time when needing to get things done quickly. As others have said, time is valuable.

But in general, I agree, I always use linting and have a favourite, slightly adjusted version of "Standard" JavaScript that I prefer to use. Full Standard is rather overly aggressive for my liking.

But then, I'm not part of a team and if I were, I'd need to work to the team standards not my own.

:smile: It does take a while to get used to it. But in general, it does save you from some subtle bugs that can be really hard to track down. I don't regret training myself to commit var to the very bottom of my bag of tricks.

That is certainly a good point. Though one doubtless countered by many open source developers who might say that they don't develop for that reason.

Personally, I like to write the best code I can, and often refactor old code if I come across it in the future with more knowledge to hand. But that is me and one of the reasons I code personally is for the creative joy of it. So I personally consider that time investment well worth while.

But even I will only do this if there is also some other reason to update things. I don't have the capacity to rewrite old code just for the sake of it I'm afraid.

:rofl: Actually, BBC Basic was a superb and well structured language. :smile: But yes, it was certainly easy to trip yourself up. Just be thankful that you never had to deal with APL! I once had to spend an entire week pondering over a calculated goto in APL.

Hmm, I'd say that mostly the use of var in new code is bad code. It does greatly annoy me when people trying to show others how to do things in articles in 2024 use var indiscriminately. There is no call for that. What's worse is that this then gets picked up in LLM's and so you end up with perpetuated bad code.

The use of var in old code is expected. But worthy of updating if making amendments anyway and if time allows. I have found bugs lurking due to incorrect use of var, so there is certainly value in this.

3 Likes

Oh yes, I had forgotten BBC basic, how could I have done that? I was thinking about MS Basic.

I did try and understand it at one point, but decided life was too short.

1 Like