5

How should suggested edits like this, where the entire edit essentially just removes the whole block of import statements (or include, use, etc... for other languages) be handled? Can understand both sides of it

Benefit:

  • Shorter, more readable code, allowing users answering the question to focus on more on the code that matters.

Harm:

  • May be slightly harder to answer for some users if they want to test code are not as familiar with the language.

In cases where some rare libraries are used, I would think the suggested edit should definitely be rejected. But what about the example case, where most of the import statements are very common, and considering that IDE's and other editing tools generally have some functionality to include used libraries on the fly or with one command (so shouldn't really make it any harder to test)?

4
  • @devnull Do you remember in which posts? A bit hard to search for this question, since import is overshadowed by important, include by include(non-programming)/including/included.
    – user244310
    Mar 17, 2014 at 7:46
  • 1
    It turns out that is an exact duplicate of a question I posted earlier. The answer to it explains it well.
    – devnull
    Mar 17, 2014 at 7:50
  • 1
    @devnull A bit surprised how strongly against the answers are, but thanks for the link. Agree it's a duplicate, just couldn't find the original (don't have enough rep to close on meta).
    – user244310
    Mar 17, 2014 at 7:57
  • That's ok. I too realized it later that this is a dup. In a way it's correct that the code not be touched, as it'd often lead to changing the meaning of the post.
    – devnull
    Mar 17, 2014 at 7:59

2 Answers 2

2

Sometimes the problem and the solution is in the imports. I've seen a few questions like that in Android tag where the classes have the same name in standard SDK and support library but differ by namespace.

I think we should be very careful with such edits and remove imports only if it's very clear that they are not necessary to answer the question and solve the problem correctly.

2

Benefit of not having the imports:

  • Shorter code, so less scrolling.

Harm of not having the imports:

  • Code that doesn't work out of the box, it's up to the reader to fill in the details.

No, code without import statements is not more readable. The imports are grouped in a block at the beginning and it's absolutely immediate to skip them if you don't need to pay attention to them. But when the imports matter, it's vital that they're present.

Conversely, not having the imports there makes it harder to test the code. Even if I'm thoroughly familiar with the language, I don't want to hunt down which libraries are needed.

Compilable code is always preferable. In the words of the help center: we encourage a complete, tested example.

Missing imports should be edited in if they're obvious, and if they're not, then the question should be closed as unclear with a request for the asker to post compilable code, complete with imports.

Now, when it comes to editing somebody else's question, there's another factor to consider: it's risky to edit code, because it might change the problem. Editing code in questions should only be done when it's absolutely certain that it won't happen to solve or move the problem around. Generally speaking, in a question, only edit for indentation and the like.

So when reviewing suggested edits:

  • If the edit removes necessary imports, reject.
  • If the edit removes superfluous imports from a question, reject unless it is absolutely completely obvious that these superfluous imports won't be causing any trouble such as name shadowing.
  • If the edit adds imports to code in a question, proceed with caution. If the imports are correct and complete, and you're sure that the problem is unrelated to having wrong imports, accept. If the imports look plausible but you aren't familiar enough with the language to be certain, skip. Otherwise, reject.
  • If the edit modifies imports in an answer, accept if the change is correct (adding necessary imports, removing superfluous ones), reject otherwise. If you aren't sure, skip.