Whenever I ask a user to show some code, or a more specific part of their code, they will very often paste it into a comment reply instead of appending it to their original question. This is especially true for new users where it may not be obvious that they can edit their original question.

There are libraries to facilitate classifying text from code. Whenever we detect an asker including code in a comment on their own question the UI would suggest that they append it to their original question instead.


There may be libraries built for this specific purpose that I wasn't able to find at first glance. Barring discovery of one, I see a few potential ways of doing this.

  1. google/code-prettify has the ability to detect languages. Fork this code and repurpose it for this problem.

  2. Roll your own simple code detector using some pattern matching as suggested here. Also, in addition to detecting code detect words with a dictionary lookup. Categorize terms as cody and non-cody. If "cody" terms cross some threshold then its probably code. This is obviously not a perfect solution and will result in a lot of false negatives for some languages, which I think is tolerable. There would also be a lot of false positives for those with troubled english, so english dictionary misses would need to be weighted lower than more obvious cody terms like brackets and semicolons.

  3. Train a bayes classifier and host a code detection service (yikes).

Again, ideally we would find an existing library built for this specific purpose, and if it doesn't exist then it's definitely possible that the complexity ends up outweighing the benefit here :)

  • 13
    Or even more simply, look for the code markdown backticks ` – ryanyuyu May 15 '15 at 16:17
  • 41
    Makes sense, though if they're already trying to shove code blocks into this comment field I doubt most of them will bother to use backticks ;) – Andrew Lavers May 15 '15 at 16:19
  • You wouldn't happen to know of which libraries these would be or have an example of them working somewhere? It may make your feature-request more plausible. – Travis J May 15 '15 at 16:37
  • 8
    In your original comment, indicate that they should edit it into the question. Or make the comment afterwards. With what you suggest, there'll be all sorts of stuff end up in the question (the system told me to do it) which will have to be unpickled. I don't think it's going to work very well with COBOL :-) It would make it tedious for (presumably just the question-asker) to refer to code in comments. – Bill Woodger May 15 '15 at 17:04
  • 2
    If newlines were automatically stripped while typing comments, everything would be so much more obvious: no long code on comments, don't draw ASCII diagrams, etc. – Kroltan May 16 '15 at 3:55
  • 3
    Additionally, we could suggest adding code to a question that doesn't have any detected code contained within. "Did you forget to include example code?" with a link to How Do I Ask a Good Question? and the MCVE/SSCCE links. – Will May 16 '15 at 4:36
  • 7
    Alternatively, just vote their question into oblivion for not posting the relevant code in the first place ... – The Blue Dog May 16 '15 at 18:22
  • 1
    If we think it's possible to heuristically detect code well enough, I think an even more useful place to use that would be in detecting questions with code that isn't indented as code. New users screw that up even more than they paste code into comments. (Also code that's marked as a JS code snippet but is actually Python or C# or whatever.) – abarnert May 17 '15 at 22:35
  • Also, doesn't "cody" mean "male child born in the US during the years 1890-1895 or 1986-1991"? – abarnert May 17 '15 at 22:39
  • 3
    We all often enough write code in comments, and that for good reasons. Knowing what the purpose of comments is, we do only use oneliners and single expressions however. What this "no code in comments" detection really needs to look out for is multiline code only, when it is copy-pasted into the comment box. – Bergi May 17 '15 at 23:48
  • 1
    I also think that code in comments is not a problem is general, but badly/not at all formatted complex multi-line code in comments. And detecting such should be really easy: Whenever there is a newline and/or tabulator character in the comment, it's very likely to be copy-pasted multi-line code. – tobias_k May 18 '15 at 11:04

Alternatively, change your phrasing. Instead of posting a comment:

Could you please post code that can be used to reproduce the problem.

You could post a comment like the following one when you encounter an user with < 100 rep. Optionally, add braces around edit to make a magic link to the edit page (per @Jeffrey Bosboom).

Please add the shortest code that can be used to reproduce the problem by clicking the edit link under your question. Afterwards post a comment to notify me of the change. Thanks in advance.

I think having a popup show up is overkill when the real problem is not giving clear directions.

  • 18
    There's also an edit magic link (put brackets around edit). – Jeffrey Bosboom May 17 '15 at 15:13
  • 10
    It's not magic when no unicorns show up – Sumurai8 May 17 '15 at 15:44
  • 2
    Slight rewording suggestion: "Please add the shortest code that can be used..." to emphasize that you shouldn't post your entire application. – Fund Monica's Lawsuit May 17 '15 at 23:03
  • 1
    It would be nice to have this text as a canned comment to trigger just by clicking something, or saying [move-code-to-question!] – fedorqui May 18 '15 at 9:37
  • stackapps.com/questions/2116/… – Sumurai8 May 18 '15 at 9:51
  • 7
    Yes let's just reproduce the entire help centre in canned comments on every new bloody question – Lightness Races with Monica May 18 '15 at 9:57
  • +1, this would be my suggestion, too.. – Gogol May 18 '15 at 9:59

I don't see why we would like to special-case additional code provided by the OP. The recommended way for adding any kind of improvement or additional information to the question is to edit it right into the question itself and not hiding it somewhere down in the comments. Many new users fail at doing this, but I guess that treating code different from other information will not decrease the confusion about what should go into a comment and what into the question.

You must log in to answer this question.

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