3

I made this edit to correct a syntax error in some JavaScript code in an answer.

I added the var keyword to the code. Before my edit, values were being assigned to undefined variables.

This accepted answer suggests that correcting a syntax error in code in an answer is a good enough reason to make an edit.

My edit was rejected by three different users, twice because the "changes are either completely superfluous or actively harm readability" - and once because it "was intended to address the author of the post and makes no sense as an edit."

I believe that the correction of a syntax error improves the quality of the answer, so why was my edit rejected?

14
  • 14
    I rejected a similar edit from someone else a couple of days ago, and although using var is more syntactically correct, going without the var is also valid JS and may very well be what the OP intends (for example, the user expects those variables to be already be existing as global variable from another script elsewhere)
    – gitsitgo
    Commented Mar 18, 2016 at 15:00
  • 18
    Making a change in the code of an accepted answer that has been reviewed by 200,000 programmers (only one of which commented "it doesn't work") requires being really, really sure that the change is appropriate. Not the kind of confidence you can expect from reviewers. Commented Mar 18, 2016 at 15:02
  • 18
    Before my edit, values were being assigned to undefined variables. Yes, they were, and the code will function just fine when doing that, so you're not actually fixing a problem. It would probably be better practice to scope the variable locally, but that's a matter of preference, and not something that you should be changing in another's post in an edit.
    – Servy
    Commented Mar 18, 2016 at 15:09
  • 2
    Users may copy and paste the code as-is which could cause unintended behaviours if the scoping of the variables isn't guaranteed by the var keyword. On that basis I would argue that the var is necessary to ensure that the answer works as intended by the answerer
    – glcheetham
    Commented Mar 18, 2016 at 15:11
  • 3
    @HansPassant Haha good of you to point that out! :-) Though even the wikipedia page for Barack Obama is edited almost every day so I'd like to think that there's room for improvement in anything
    – glcheetham
    Commented Mar 18, 2016 at 15:16
  • 4
    @glcheetham I'm pretty sure plenty of edits on that page get rejected/rolled back as well ;)
    – Gimby
    Commented Mar 18, 2016 at 15:20
  • 9
    @glcheetham That's some good information to include in a comment, but not a reason to change the code in a suggested edit.
    – Servy
    Commented Mar 18, 2016 at 15:29
  • 8
    Suggested edits are, in practice, held to a much narrower criteria than direct edits. Anything more than simple unambiguous grammar/spelling fixes has a good chance of being rejected. I think this is a very sensible edit (it's not actually changing the behaviour of the code in context (global scope in both cases), but makes it behave better when copied to a function context), which I could see myself making. Unfortunately, until you can edit directly, making changes like this is kind of hopeless. SO users are way too paranoid about violating author's intent, though author can just rollback Commented Mar 18, 2016 at 20:38
  • @JeremyBanks thanks for the info. I'll bear this in mind when making future edits.
    – glcheetham
    Commented Mar 19, 2016 at 11:07
  • 1
    @gitsitgo there is nothing in the question that would make that assumption warranted. Commented Mar 19, 2016 at 22:37
  • 4
    I don't think it's safe to just assume these variables already exist from another script. With that logic you might as well assume another script wrote 'use strict'; and any global declarations like this would throw an error. Using var will always work, and going without the var causes a reference error in strict mode. Shame to see good practices are being rejected because sloppy JS can work sometimes, even if they fail in strict mode. Commented Mar 20, 2016 at 16:43
  • @MichaelTheriot Good contribution. Before I made the edit I checked and triple-checked to see whether the variables were meant to reference globals or not - and I decided that they didn't.
    – glcheetham
    Commented Mar 20, 2016 at 17:32
  • @MartinSmith you missed the "for example" part
    – gitsitgo
    Commented Mar 21, 2016 at 13:51
  • @gitsitgo This question is about a real world example not some hypothetical case. Commented Mar 21, 2016 at 17:42

1 Answer 1

15

This particular edit is properly rejected as edit comment based on false assumption "fixed syntax error in ..." when there is no syntax error. It is perfectly valid (syntactically) to not declare variables in JavaScript.

Note that code changes in general have very high bar during reviews and most likely will be declined. Coordinating change with author of the post via comments/chat is better route, especially for such essentially coding style edits.

3
  • 10
    It's worth also noting that sometimes the lack of var is the problem that the question is needing fixed. Editing in var might be acceptable for an answer, but for a question sometimes "fixing code" is exactly what the whole question needs. Commented Mar 19, 2016 at 22:24
  • 3
    In this case the author of the answer is not available as it is a deleted account. Commented Mar 19, 2016 at 22:44
  • 5
    For what it's worth, under strict mode this would throw a reference error. Commented Mar 20, 2016 at 16:46

You must log in to answer this question.

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