9

I see a lot of questions where the poster has assumed that a function works in place (for example, the way JavaScript Array.sort works) and consequently failed to assign the result of the function call to the variable they wish to change. For example with JavaScript Array.concat users will write:

let arr = [ /* some array data */ ];
arr.concat(arr2);

where they should have written:

arr = arr.concat(arr2);

I also (to a lesser extent) see questions where people expect the return value from a function which modifies the input in place to be the modified input (e.g. JavaScript Array.splice).

Is it appropriate to close these questions as typos/non-reproducible? Should they even be closed at all? If they should be closed, but not as typos, what is the appropriate close reason? Because of the sheer number of such functions it doesn't seem practical to set up a canonical that would allow these questions to be closed as duplicates.

13
  • 9
    "doesn't seem practical to set up a canonical" - why not? These all boil down to the inability to understand that a method either creates something new or mutates something in place. I am not sure if we have a canonical on the difference, though (that does not deal with one method at a time). But otherwise, yeah, closing as "typo" seems just about right (this is a typo, after all - forgetting to assign the return value)
    – 0Valt
    Commented Apr 23, 2021 at 2:48
  • 2
    @OlegValter I don't think it's practical because there are so many of these functions, for so many different languages
    – Nick
    Commented Apr 23, 2021 at 3:35
  • 5
    Absolutely close them. Here's a common dupe target example for .replace too: stackoverflow.com/q/1433212 Nothing wrong with setting up canonicals, or figuring out an existing one to link to for each type of problem. Don't have just one, of course, have one for each sort of situation/language. Commented Apr 23, 2021 at 4:02
  • 1
    Nick, so you meant for all languages? I envisioned something closer to one per language and/or per function because each has its own set of peculiarities answers could talk about. And if there is no canonical, you are sure the issue boils down to forgetting to assign the returned value - then I think typo closure is also acceptable.
    – 0Valt
    Commented Apr 23, 2021 at 4:05
  • 1
    @OlegValter no, I do mean one per language, I guess one per function would be optimal...
    – Nick
    Commented Apr 23, 2021 at 4:47
  • @Nick - maybe cigien's idea of grouping by mutating / non-mutating, sounds more manageable than per-function
    – 0Valt
    Commented Apr 23, 2021 at 4:55
  • 1
    @OlegValter: “…this is a typo, after all…” Not really, in these circumstances, the actual error is semantic; it can be caused by a mere typo—that is, a physical typing error or a clerical error when entering, editing, or copying program text—but more often it seems to stem from intentionally entering code with mistaken expectations. If such expectations are reasonable or common (e.g., if other languages/APIs work differently), then it’s a gotcha/pitfall/caveat. In that case, it is likely to help future readers, so the important part of that close reason doesn’t apply.
    – Jon Purdy
    Commented Apr 23, 2021 at 22:40
  • @JonPurdy - well, I don't disagree, the proper conduct is dupe closure as these questions certainly have been asked and answered to death before. One can also choose to answer if there are none or the targets are subpar. But I do think that if not the first two, "typo" reason is acceptable as well - especially if the answer is "you forgot to assign the result". Re: gotcha - don't think mutating methods are gotchas - they are well-documented and, frankly, suspiciously smell like RTFM problems. That said, yeah, going by the dictionary definition of "typo" these are not typos.
    – 0Valt
    Commented Apr 23, 2021 at 22:57
  • The answer is going to be strongly language/package-specific, and depend on how much that language/package relies on fluent paradigm and in-place operations, vs. imperative ones, or a mix. In Python we get this so often we have dupe targets Why doesn't calling a Python string method do anything unless you assign its output?. In that case close-as-typo is undesirable when you can/should close-as-duplicate. Here you asked a general question but your body & example are JS
    – smci
    Commented Apr 23, 2021 at 23:28
  • @smci I didn't know of that question, I'll use it in future for python string questions, thanks.
    – Nick
    Commented Apr 24, 2021 at 1:07
  • 3
    @smci As a bit of a nitpick side note, I would argue "unless you assign its output" shouldn't be in that question, as that ("assign its output") is basically the answer for most people. Questions shouldn't contain their own answer, as that's not what others will search for, and it may appear to not answer their question if they see it. In that case it's probably okay or not a huge deal, as you can simply remove those words without change anything else and it still makes sense. But it is something to be cautious of in cases where it changes the question more significantly. Commented Apr 24, 2021 at 6:33
  • 1
    Just earlier today, a question was asked that had someArray = someArray.push(xyz) in its code. I voted to close it as a duplicate of Why do I get “.push not a function”?. Some of these “canonicals” do exist. Commented Apr 25, 2021 at 11:24
  • @SebastianSimon - btw, I think we are conflating canonicals with dupe targets. It was always my understanding that a canonical is an exceptional great answer (possibly accompanied by a great question) that taps into language spec, explains caveats and corner cases, etc. P.s. That said, yeah, I think every one of those questions have a relevant dupe target.
    – 0Valt
    Commented Apr 25, 2021 at 11:27

2 Answers 2

28

I don't think the questions you've described should be closed as typos. e.g. it's not obvious that concat doesn't modify the argument in place, and there's no indication that there's anything wrong with the code. I prefer to use the "Typo" reason for things like a missing ;, or errors that are caught by a compiler.

Of course, the "Typo" reason should be used if the question is not useful to future readers. However, the fact that these questions get asked often means that programmers are running into this on a regular basis, and so these questions are useful, and they're on-topic.

As it happens, there is "concat" does not join JavaScript arrays together? and Why doesn't array.splice() work when the array has only 1 element? which can serve as canonical targets for the two cases you described. Note that I'm not an SME, so those targets may not be appropriate, or even correct, but I'm sure that targets exist for each of those functions. And if they don't, just clean up the next one of those questions that comes along, and start using that one as a target.

Ideally, there would be a canonical for all functions that modify arguments in place, and another for those that return the modified results. These might not be easy canonicals to write (assuming they don't already exist), and they would be susceptible to becoming "list of functions with property X" style questions, which are off-topic. Perhaps a single canonical that covers the differences between the two types of functions could be written, but it wouldn't be easy to do that either.

Regardless of whether the generic canonicals exist, I think the questions you've described are on-topic, and assuming they're clear, focused, include the needed details, etc, they shouldn't be closed, except as duplicates.

5
  • 1
    You're probably right; see my comment to Machavity's answer.
    – Nick
    Commented Apr 23, 2021 at 5:00
  • Another canonical target: Replace method doesn't work for the common issue str.replace(/…/, …);
    – Bergi
    Commented Apr 23, 2021 at 23:34
  • 2
    As a SME: these targets you found are totally appropriate
    – Bergi
    Commented Apr 23, 2021 at 23:36
  • 4
    They're definitely not typos. A typo is something that the person who wrote it can discover by staring at it long enough. This is knowledge that the author simply does not have - the exact semantics of the function that they're calling. Commented Apr 23, 2021 at 23:42
  • 1
    @Bergi Ah, thanks for the confirmation. It's good to know my googling skills didn't fail me this time :)
    – cigien
    Commented Apr 24, 2021 at 1:53
16

There's three categories I tend to think of here

  1. Syntax errors - You misspelled the variable or you forgot the language punctuation
  2. Logic errors - You assigned x and forgot to do the proper thing with x, or called y by mistake
  3. Language gotchas - You have to do something that isn't obvious to coders

#1 and #2 are closable as typos. #2 and #3 seem similar, but you have to ask yourself a key question: Is there some meat to an answer? If you're explaining a mere assignment problem then by all means close it. But if you need to do something not-so-obvious (like an order-of-operations bit) it might be worthwhile to have a question that covers that. PHP, for instance, has just such a problem. Just because the answer is obvious to you doesn't mean it's obvious to everyone, especially if it's something like a new feature.

Language gotchas are a significant portion of the questions on SO (see ). If there's a duplicate, close it. Otherwise, helping new users answer it can be beneficial.

2
  • 5
    I think the problem is it's hard to distinguish whether not assigning the return value was due to a momentary lapse of reason (#2) or not properly understanding the way the function works (#3). I guess if it can fall into #3 it probably makes more sense to answer/close as a dupe (assuming you can find one)
    – Nick
    Commented Apr 23, 2021 at 4:50
  • 3
    @Nick I freely admit there can be a fine line between #2 and #3. There's always going to be some disagreement there (and believe me, as a mod I see that a lot because the flags fly in those cases). The key for me is that sometimes a good explanation of what to watch out for and why can be highly useful. I can't tell you how many times I've searched for something and found an SO question that made me smack my head for missing something so obvious.
    – Machavity Mod
    Commented Apr 23, 2021 at 15:28

You must log in to answer this question.

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