47

In the suggested edit here: https://stackoverflow.com/review/suggested-edits/34762565, the author of the answer wrote:

var os = require('os');
a.split(os.EOL);

Another person suggested an edit to change it to this:

const os = require('os');
a.split(os.EOL);

(i.e., changing var to const) without any other changes to the answer.

Is that an edit that should be approved? It's not a huge change but it does change the code the person originally wrote in their answer. I wasn't sure if an edit like this is okay or if it wouldn't be right to alter the code.

14
  • 2
    It has no negative impact, doesn't really "improve" anything either, but... i wouldn't blame the author of the answer for accepting it. :shrug: don't think i'd accept it on their behalf.
    – Kevin B
    Commented Jul 28, 2023 at 21:48
  • 19
    This is code in an answer? That is a little different, since in my opinion, this sort of change does improve the quality of an answer more than a similar change would in a question. It's still a small improvement though, and myself, I'd have preferred placing a comment to the answer rather than editing it. Please edit your original meta question to improve its clarity. Commented Jul 28, 2023 at 21:54
  • Of course it depends on if the original code was correct and if the edited code is correct.
    – mkrieger1
    Commented Jul 29, 2023 at 9:57
  • 6
    No. Else you could then argue that we need to constantly update all the code of all answers ever posted to be up to the latest standards. That answer was correct (and still is) at the time it was posted and approving it reinforces wasteful editor behavior. Commented Jul 29, 2023 at 13:43
  • 4
    That's a kind of edit you could do when you have enough rep to not end up in the edit review queue. Before that, don't do them. I'm not sure if we should accept them or not.
    – klutt
    Commented Jul 29, 2023 at 17:12
  • 1
    As authors are people and write answers, I’d comment on the answer rather than edit it out of respect for the author
    – MStodd
    Commented Jul 29, 2023 at 17:57
  • 8
    @cyberwombat Answers that are still relevant today should in general be updated to he latest standards, yes. Such edits make the answer more useful. (Whether they should be made if they need to go through review is a different question)
    – Bergi
    Commented Jul 29, 2023 at 20:05
  • A Douglas Crockford zealot? Commented Jul 29, 2023 at 21:00
  • 2
    @Bergi while I agree as a general principle I feel the reality of it is challenging. Where do you draw the line? I'm not going to update all the code I see that I feel could be better or more modern. I write everything in latest standards so even my code from last year is outdated. I do appreciate new answers to questions with the "2023 answer" title so I can see what I can now do but don't really want other answers to be necessarily edited to reflect that. Commented Jul 29, 2023 at 22:20
  • 6
    @cyberwombat If a totally new approach comes up, that warrants a separate answer (though if you already have posted a highly-upvoted one, an edit might be more visible). I was referring to answers addressing general problems where code is just used for illustration. Even there, code should be updated to modern standards so that the answer stays useful. I'm not asking anyone to actively comb through their answers - challenging indeed - but if you come across something egregiously outdated in an otherwise useful answer, go for an edit.
    – Bergi
    Commented Jul 29, 2023 at 23:21
  • 2
    just a sidenote, var is not deprecated nor obsolete. It's fully compatible with ES6, you can use it without problems, there is no mention of "you should use const / let" anywhere on MDN: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – GrafiCode
    Commented Jul 30, 2023 at 11:37
  • 2
    @GrafiCode It's not deprecated because it can't be removed. A codebase that mixes var, let and const for no reason, or uses exclusively var even though it's transpiled, would have a huge code smell/red flag to any experienced JS dev. var never needs to be used any longer, except possibly in browser-compatible legacy code that isn't transpiled, which is very rare, or trivial throwaway userscripts and bookmarklets. ESLint, no var
    – ggorlen
    Commented Jul 30, 2023 at 18:44
  • 2
    I made this edit because I thought it was generally considered standard to make references to modules read-only. You don't want someone editing your code accidentally changing a module reference! I don't think it's deprecated or wrong to use var here (though I think its a bad idea), I just thought in the majority of instances in which someone would copy-paste, const would make more sense, and I thought if the OP answered today, they would probably use const. In any case, I want to learn, and if this was not the right move, by all means reject the edit.
    – iownbey
    Commented Jul 31, 2023 at 18:10
  • 2
    Difficult question. Changing to const makes the code more copy-pastable tailored for this day and age, but that is not why we put code in answers. If you want to do Stack Overflow-driven development that's on you but we're not here to endorse that. If code contains var keywords, perhaps the entire solution is a bit out of date. I kind of like the code to represent the age it was written in.
    – Gimby
    Commented Aug 1, 2023 at 14:39

4 Answers 4

69

I don't think it matters much either way. I don't see harm in approving it because I tend to fall in the "even small improvements are worthwhile" camp, but if you reject it as being too inconsequential, that's fine as well.

Reasons to approve:

  • The edit poses virtually zero risk of changing the behavior of the code and has no bearing on the author's main point in the answer, so the edit does not conflict with the author's intent.
  • The original answer was posted in 2014 and hasn't been touched since. 2014 is around the time const was just coming into existence (ES2015). Had this answerer posted it in 2023, they'd probably use const, which they used in most of their 2018 answers. The edit doesn't conflict with the author's intent.
  • It's unambiguously better code. var doesn't need to be used any longer, and certainly not for a require. If the edit changed const to let or var, then I'd definitely reject it as no improvement or conflicts with the author's intent.
  • There's no apparent legacy situation here where the answer needs to be compatible with extremely old versions of Node which would conflict with the author's intent.

Reasons to reject:

  • It's rather inconsequential and improves safety of the code by such a marginal amount that there's arguably no improvement.
  • Depending on the nature of the comment left with the edit, the edit may be an attempt to reply.

Note that if this was an edit to a question rather than an answer, I'd be much more likely to reject the edit as it would probably conflict with the author's intent. Changing code in a question can harm reproducibility.

18
  • 12
    I'd argue that it does conflict with the author's intent -- they have had 9 years to come back and use const if they wanted to via edit. The answer is no more correct for using const than var -- or put another way, it works as-is with var. Commented Jul 29, 2023 at 10:45
  • 15
    I would argue with the argument about negligible impact of the edit. If the answer is popular, a lot of people will copy this code, forcing them to manually change var to const or to have legacy syntax in their programs. It is very small impact per person but it gets multiplied by the number of people who copy this code. Commented Jul 29, 2023 at 13:18
  • 33
    @RoddyoftheFrozenPeas I don't see any evidence that the author cared one way or the other about var or const. They simply used whatever was the normal keyword at the time. The change has virtually no bearing whatsoever on the author's primary point in the answer, has virtually zero risk of altering the semantics of the answer. If you avoid this edit on the grounds of conflicting with author's intent, you'd probably never touch code in any answer, because this is about as safe as it gets. Poster hasn't answered since 2018 and probably does not care to maintain their old posts.
    – ggorlen
    Commented Jul 29, 2023 at 14:38
  • 5
    const changes the semantics, though. let is the more equivalent of var. There's nothing wrong with the code; it works. And yes I do change code, for example if the poster typos a variable or misses a parenthesis or forgets a return statement; but when the code works, then no. Commented Jul 29, 2023 at 15:35
  • 11
    @RoddyoftheFrozenPeas I think your interpretation is overly pedantic. const vs let vs var doesn't change the semantics in a meaningful way in this code snippet because there's no scoping or reassignment here--it's a safety net in the context of larger code that isn't actually present yet, but likely will be when the code is moved into a larger context. Using var is a code smell that calls attention to itself--it begs the question, "is the code intended for a legacy environment or browser compatibility or is scoping/reassignment intended here? ", and the answer is "not in this case".
    – ggorlen
    Commented Jul 29, 2023 at 15:53
  • 7
    @RoddyoftheFrozenPeas I think no improvement and attempt to reply are valid reasons to reject this edit, but it almost definitely doesn't conflict with the author's intent.
    – ggorlen
    Commented Jul 29, 2023 at 15:54
  • 12
    @SecurityHound Again, I think you're interpreting "intent" too strictly. The author's intent is "I'm declaring a variable and doing it in whatever way is normal at the time I'm writing it, which happens to be var but may as well be const, as in this answer I wrote in 2018".
    – ggorlen
    Commented Jul 29, 2023 at 16:50
  • 2
    Intent has a specific meaning in the English language how else should I interpret its meaning other than it’s actually meaning? Commented Jul 29, 2023 at 16:52
  • 16
    @SecurityHound I think we both agree on the definition in the English language. The question we're discussing is not the definition of intent, but what constitutes the author's particular intent in this case. You appear to interpret their intent as whatever their fingers actually typed, which means you can virtually never edit anything except typos, while I interpret intent as the high-level concept they're using the code to illustrate, var being an incidental (now anachronistic) means to achieve that end in this case.
    – ggorlen
    Commented Jul 29, 2023 at 17:02
  • 3
    @ggorlen--onLLMstrike - I have no problem correcting grammatical error or spelling errors but updating code to newer standards is a little different. Fixing formatting issues with code is fine obviously. Commented Jul 30, 2023 at 1:48
  • 7
    @SecurityHound I agree that upgrading code to newer standards is a separate case from formatting improvements, and extreme discretion should be advised. For example, updating code from Python 2 to 3 is generally harmful and conflicts with the author's intent. But in this particular case, the edit proposal is a harmless change and an unequivocal (albeit small) improvement, based on my knowledge of Node and JS. This is a good example of when a change can improve a post in a small way, but isn't strictly a formatting/typo adjustment.
    – ggorlen
    Commented Jul 30, 2023 at 18:41
  • 3
    @PiotrSiupa Good. I have no sympathy for people who copy paste code from SO into their code bases with reading or understanding it. If you understand it, it should be trivial to change var to const, just as it should changing really old answers from explicit function syntax to arrow syntax, if that is desirable. And people should have to do some work when taking code from elsewhere and incorporating into their own (especially, as is often the case, they do so without attribution). Commented Jul 31, 2023 at 14:15
  • 2
    I agree with @RoddyoftheFrozenPeas, the usage of const vs let vs var certainly changes meaning of the code that was written. I'd not approve a change like this an almost all cases. I agree also with Heretic Monkey, it is the reader's responsibility to understand the code they are applying, making changes to tailor it to their specific needs.
    – Drew Reese
    Commented Jul 31, 2023 at 15:13
  • 3
    @TylerH const is always better/safer than var from a programming perspective, which is why it was created. It's definitely an unequivocal improvement. The answer isn't specific to any particular version of Node, and it's on the users who are adapting the answer to an extremely old version of Node (probably Node 6 or something like that, I'm not sure) to make the trivial adjustment. Better put the burden on the 0.000001% of users with this special case rather than everyone else (they'll be used to it, since they won't be able to use much other code on SO, most of which uses const/let).
    – ggorlen
    Commented Jul 31, 2023 at 19:34
  • 4
    @TylerH Like other people in this thread, you seem to be playing pedantic linguistic games to try to coerce a contradiction, with no apparent motivation. There's no contradiction: const is 100% better than var, always. If you can't use it because you're one of the 25 people on the planet that are stuck on Node 0.x or whatever, then you'll know you need to change it to the objectively worse legacy keyword var. I don't think it's appropriate that we write all code with var just to appease virtually nobody. The change saves energy for the other 99.9999% of programmers looking at it.
    – ggorlen
    Commented Jul 31, 2023 at 19:49
26

This improves the answer.

  • Answers should be up-to-date and should teach readers how to write code in a modern style.
  • "varconst" or "let mutlet" are transformations that are encouraged by linters.
  • For people using old language versions, this transformation can be trivially undone anyways.

What's wrong with leaving a comment such as "Prefer const over var!"?

  • Comments are less visible.

  • Comments are temporary.

If the change is non-trivial, and is not what the existing answer was originally talking about, then consider either leaving a comment or writing another answer.


From the point of view of reviewing edits (as opposed to 2000+ rep users making changes), it is recommended that overly trivial edits be rejected as "no improvement" as a deterrent to trivial edits. This is theorized to reduce the load on the edit review queues. Whatever your opinion on this may be, it's certainly not incorrect to accept the edit.

6
  • 4
    What's let mut? Commented Jul 30, 2023 at 18:30
  • 6
    @DonaldDuck Probably a Rust thing
    – ggorlen
    Commented Jul 30, 2023 at 18:38
  • 4
    "Answers should be up-to-date" Citation needed. "and should teach readers how to write code in a modern style." Citation needed.
    – TylerH
    Commented Jul 31, 2023 at 19:24
  • "For people using old language versions, this transformation can be trivially undone anyways." Why are we making work for users where there wasn't work before? That's a bad idea, in general.
    – TylerH
    Commented Jul 31, 2023 at 19:25
  • 3
    @TylerH I checked const support in Node 1 and it's there, so only Node 0 users would possibly have to make the change--that's going back to 2013/2014. You're inconveniencing virtually all Node users who'll need to make the change in favor of probably a dozen people who still use Node 0 and will probably never look at this answer. It's the safest possible, most innocuous code change I can think of. I don't see that you've written any Node answers, so you might want to consider whether you have the experience to weigh in on this.
    – ggorlen
    Commented Jul 31, 2023 at 20:10
  • 1
    Checked Node 0.10 and const still worked. I tried installing even older versions with nvm but the checksums failed, so I can't even find a Node version that doesn't support const.
    – ggorlen
    Commented Jul 31, 2023 at 20:56
1

Changing var to const in that answer from 2014 does not improve the code in any meaningful way, and could cause it to break in some legacy environments. It should be trivial to fix that break if someone were to copy and paste the code and then encounter said error, but why add work where none exists, even if the amount of work is minuscule?

I would reject-and-edit this suggestion to leave the original code in place and insert a mention into the answer of const (or let) being the preferred, modern way to declare most variables over var, perhaps with an embedded link to What is the difference between "let" and "var"? for the purpose of education. This way future readers are informed of modern code practices and OP's code/intent is preserved.

6
  • 3
    The number of Node programmers that don't have access to const must be staggeringly miniscule, far outweighed by those who do. The edit is so trivial that it should be just applied (or not). Leaving a comment/edit on every post that uses var to explain the difference between the keywords is terribly noisy and should certainly not be done. We should assume familiarity with the most basic keywords in the language when writing answers.
    – ggorlen
    Commented Jul 31, 2023 at 19:38
  • @ggorlen--onLLMstrike Who said anything about Node?
    – TylerH
    Commented Jul 31, 2023 at 19:39
  • 1
    Did you read the code in the post? It's 100% Node and would not run in the browser: var os = require('os');. If this were browser code, your answer may hold more water, but this isn't the case.
    – ggorlen
    Commented Jul 31, 2023 at 19:40
  • 2
    @ggorlen--onLLMstrike I did read the post. This question on Meta is about not just that post, but also about the practice in general.
    – TylerH
    Commented Jul 31, 2023 at 19:41
  • 1
    @ggorlen--onLLMstrike "We should assume familiarity with the most basic keywords in the language when writing answers." Citation needed. Don't make the mistake of thinking that, because a question is basic, it is off-topic on Stack Overflow.
    – TylerH
    Commented Jul 31, 2023 at 19:41
  • 4
    I don't think it's appropriate to teach irrelevant language basics when writing answers. The question isn't about const vs let vs var, these are completely incidental to the answer. Should the answer also discuss require vs import? Should every answer that uses const explain what it is, along with for and if? Clearly, no--this is assumed prerequisite knowledge.
    – ggorlen
    Commented Jul 31, 2023 at 19:51
-4

In the case of a simple change of var to const in Javascript, this conflicts with the author's intent.

At the end of the day, var and const do not have the same semantics. In modern Javascript, you use const foo to declare a constant (no changes permitted), and let foo to declare a mutable variable. var is the older way of declaring a variable, and it makes no distinction about the mutability of the variable being declared. var can still be used in modern Javascript, though it is recommended practice to use either let or const to indicate the mutability of the variable. In addition to mutability, different scoping rules apply to var than to let and const.

Therefore, I'd argue that changing the way the variable is declared conflicts with the author's intent and should have been rejected:

  • var is not semantically equivalent to const

    • mutability rules differ

    • scoping differs

  • var is still valid, though no longer the only or necessarily encouraged way to declare variables

  • The original answer, as written with var works. It's valid Javascript; if you run it as originally written it would work today in 2023 as it did when originally written in 2014.

---

I would even argue that a modern answer may still be written with var: it makes no assumption about the required mutability of this declared variable in the greater scope of the original poster's application.

4
  • "you use const foo to declare a constant (no changes permitted)," - that's pretty ambiguous. What const prevents is to reassign a variable, whatever it points to remains as mutable as ever.
    – Gimby
    Commented Aug 2, 2023 at 8:41
  • Only if you assign an object, then the fields are mutable. const foo = "abcd" is immutable. As is const foo = 123. Commented Aug 2, 2023 at 13:41
  • That does not make it less ambiguous.
    – Gimby
    Commented Aug 3, 2023 at 12:24
  • I have no idea what you're trying to say. Commented Aug 3, 2023 at 12:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .