12
\$\begingroup\$

The off-topic list at meta includes:

  • Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review.

So questions must compile and work, in order to be considered on-topic questions. Consider: Count every word occurrence from file. This code obviously will not compile as presented. It is missing four #includes (<string>, <iostream>, <vector>, and <fstream>). Based on the phrasing of OP's question, the code does work and the concern is simply algorithmic. So I simply editted in the includes.

This was removed due to:

Code in a question must never be edited. Your edit is more appropriate as a comment.

Obvious, but still an educated guess. We don't know the order of the includes, nor do we know if the code compiles despite missing includes. Therefore, putting the missing includes in there seamlessly is not appropriate; a comment would be better.

I find that reasoning ludicrous. The order of the includes is definitely irrelevant to the question (at best, there could be irrelevant #includes that merit a comment about their irrelevance), and the code will not compile without them (try it). I am not changing variable names, code structure, or anything vital to the question. I am simply making the question adhere to the guidelines.

Otherwise, the only valid response is a vote-to-close - which seems excessive since outside of the missing #includes, the question is perfectly reasonable. As the question currently has 4 upvotes and zero close votes (despite, again, containing broken code), it seems that the community implicitly agrees with this.

I simply want to make it explicit. Should this sort of edit be deemed acceptable? What if OP (a new user) does not return to edit in the missing includes? Should the question be closed then?

\$\endgroup\$

3 Answers 3

16
\$\begingroup\$

So questions must compile and work, in order to be considered on-topic questions.

No. The code must compile and work in author's environment, so that OP isn't asking the CR community to fix their code, which would be Stack Overflow territory.

But the code in the question itself doesn't need to compile straight out of the "ask question" box.

Of course, if the code in the question contains syntax errors and obviously won't even compile, it's broken code.

I don't do C++, but I deem #includes directives similar to C# using directives. Consider this snippet:

public async Task<int> DoSomething()
{
    // do something async
}

If the OP's code doesn't include using System.Threading.Tasks;, this won't compile, because Task<T> isn't in scope. But does it make the question off-topic? No, it doesn't.

Compare to this:

public async DoSomething()
{
    // do something async
}

Whatever using statements OP has, this method can't possibly compile, because the method is missing a return type - OP can't possibly have even tested their code, so claiming "it works in my environment" isn't even a possibility: that is broken code.


Asking that the code is working code makes a great filter for off-topic "what's wrong with this code?" questions, and weeds out the low-quality "please review this code I just wrote on a napkin" questions that contain blatantly broken code that the author couldn't even be bothered with compiling and running themselves.

A legit question that's merely missing #includes directives, probably had them redacted for brevety (a common reflex in users coming from SO) - of course, the code won't compile without these directives, but not having them in the question does not make the question off-topic with broken code.

Whether the code in the question box compiles all by itself and can be copied and pasted into your IDE without adding any directives and additional context, is irrelevant: if that were a requirement for on-topicness, we couldn't have tags like , and people coming here to post parts of their project that reference methods and types that are not included in the post.. and IMO that would be ludicrous.

That said:

  • Should this sort of edit be deemed acceptable? Let's be consistent here, and leave code block edits up to the author of the question. Feel free to comment and ask that the missing #includes are (ooh) included (bah-dum-tish).
  • What if OP (a new user) does not return to edit in the missing includes? That would be sad, but not harmful. The includes don't add much to the question anyway.
  • Should the question be closed then? Nope. The code works, OP isn't asking about a specific programming issue.
\$\endgroup\$
4
  • 4
    \$\begingroup\$ VTC both of these for stub/hypothetical code. \$\endgroup\$ Commented Sep 14, 2015 at 15:31
  • \$\begingroup\$ @EBrown Just because it's not "the code, the whole code, and nothing but the code, so help you.... code" doesn't mean it's stub code. It can still be real code, directly copy+pasted, it's just only a selected portion of it. \$\endgroup\$
    – Simon Forsberg Mod
    Commented Sep 14, 2015 at 16:20
  • 2
    \$\begingroup\$ @SimonForsberg I was referring to the two stub methods Mat's Mug posted... \$\endgroup\$ Commented Sep 14, 2015 at 16:20
  • 6
    \$\begingroup\$ @EBrown Oh, right. Yeah, those would clearly be stub code :) \$\endgroup\$
    – Simon Forsberg Mod
    Commented Sep 14, 2015 at 16:21
10
\$\begingroup\$

I do agree with @200_success that it was better left as a comment. I don't feel that even something like #include should be added to the question by someone else. I would assume that the OP was already aware of #includes for his/her local code (you'd have to be a super beginner to not know about using the most basic ones) but felt that it wasn't necessary to have in the question. This could be because he/she wasn't aware that we prefer a compilable solution as-is, which of course would include the #includes.

If the OP never gets around to making this change, even after leaving a comment and possibly after giving a downvote, then just leave it at that. You should certainly still mention it in an answer, to which the OP may say that they were indeed left out intentionally. But other than that, they shouldn't be added to the question by anyone else.

\$\endgroup\$
2
  • \$\begingroup\$ Do we prefer a compilable solution as-is? \$\endgroup\$
    – Barry
    Commented Sep 14, 2015 at 15:57
  • \$\begingroup\$ @Barry: Whenever possible, as it helps others review the code if it should be compiled and ran. Now, if non-obvious #includes were missing, such as custom ones, then that would be a problem. \$\endgroup\$
    – Jamal Mod
    Commented Sep 14, 2015 at 16:02
8
\$\begingroup\$

#include is a nice-to-have thing in a question. I agree with Mat's and Jamal's answers - that while the code won't compile without the includes, they are not necessary to make the question on-topic.

Additionally, editing them in is also "wrong" because it is the sort of thing only the question asker can know for sure, how it's done (hey, maybe they have their own strings.h!

The point I want to make, though, is about down-votes.

If the way a person asks their question makes it hard to read, or review, then feel free to downvote the question.

I personally try to include as much code as I can in all my code blocks so that it is easy for a person to copy/run the code. I also often include links to sites like ideone so people can see it running.

Part of the downvote tool-tip is "it's unclear or not useful" - if the question is unclear as a result of missing includes, then feel free to apply a -1 (and perhaps a comment).... but a close vote, or edit, are not the solution.

\$\endgroup\$

You must log in to answer this question.

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