What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 122 Stack Exchange communities.

This is related to this feature request around "training videos" but is more specific.

It seems to me that it wouldn't be too hard to make a pretty good guess about when a question contains code that isn't formatted. Some heuristics would be biased towards particular languages, but even so, I think the team could make some good guesses. Heuristics might include:

  • Consecutive lines which contain amounts of whitespace differing by 4 spaces in several places
  • Common programming language keywords
  • Lots of symbols compared with normal text
  • Potentially language-specific detection: semi-colons at the end of lines, braces on lines on their own or at the end of lines
  • Single-letter words other than "a" and "I"
  • Long words with underscores or capitalsWithinTheWord

In an ideal world, it would be great to guess at the precise chunk of text which contained the code, and present the user with a choice:

It looks like you were trying to post code, but didn't know about the formatting. Did you mean it to look like this (original) or this (formatted)?

User then chooses the appropriate option, and is shown what they should have done in the input box as well.

share|improve this question
    
Interesting idea. Maybe the code detection mechanisms from whatever SO uses to highlight code can serve as a "code sniffer" as well? This would be a notable improvement, as a lot of time is wasted formatting people's code. –  Pëkka Jan 11 '11 at 19:49
1  
@Pekka: the highlighter doesn't sniff code. It's just instructed to be executed on formatted code. –  Chichiray Jan 11 '11 at 20:05
    
@BalusC but it manages to recognize what language it's in, doesn't it? –  Pëkka Jan 11 '11 at 20:06
    
@Pekka: yes, but not whether it's code or just natural language. I don't think that it has a "text/plain" detector. If I create ASCII tables, I'm forced to use <pre> rather than {} because the highlighter messes it up. –  Chichiray Jan 11 '11 at 20:08
    
@BalusC yeah. My thought was that maybe the code can be tweaked to "detect code" in a more general way. But that may be naive, I don't know prettify's internal workings at all –  Pëkka Jan 11 '11 at 20:11
    
@Pekka: We could definitely take a look how Prettify does it and benefit it. –  Chichiray Jan 11 '11 at 20:15
1  
PLZ. SEND TEH CODEZ! It looks like your capslock key is broken. Did you mean: ... ...? –  Wouter van Nifterick Jan 12 '11 at 1:06
    
Further, language specific heuristics could be used based on language tags. I believe some of this sort of thing already goes on within code formatting, specifically text coloring for keywords/comments/variables/etc. –  Mark Storer Jan 31 '11 at 18:51
add comment

3 Answers

up vote 17 down vote accepted

Per

Simple method for reliably detecting code in text?

We just deployed a method that we think is quite reliable for blocking 98% of posts submitted with code that has improper code formatting (or no code formatting at all).

For example, when I go into Chrome incognito and attempt to ask this question body on Stack Overflow right now:

I needed to add up all the rows for a result. Using the select_sum as follows

Here is the model

function Dues_Paid_Tot($date)
    {
        $query = $this->db->select_sum('Dues_Paid', 'Dues_Paid_Tot');
        $query = $this->db->get('Membership');
        return $query->result();
    }

Here is the controller

function Fiscal2()
    {
    $date = $this->input->post('Select_Date');
        if($query = $this->report_model->fiscal_list($date))
        {
            $data['records'] = $query;
        }
    $data['date'] = $this->input->post('Select_Date');
    $data['Dues_Paid_Tot'] = $this->report_model->Dues_Paid_Tot($date);
    $data['main_content'] = 'report_fiscal_view';
    $this->load->view('includes/template', $data);
}

I get back:

Oops! Your question couldn't be submitted because:

Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon.

This check is limited to users with <= 50 reputation and is performed on questions / answers / edits and suggested edits.

If you see or hear of any cases where this is triggered inappropriately -- or not triggered, and it should have been -- let us know.

share|improve this answer
3  
Awesome, woot 'n stuff. –  Jon Skeet Jul 13 '11 at 12:30
    
Why are only questions checked for compliance? Looks to me like unformatted code is a problem in answers as well, especially among new users. –  dandan78 Jul 13 '11 at 12:38
1  
@dan questions have a disproportionate influence on the system. –  Jeff Atwood Jul 13 '11 at 13:03
    
It didn't seem to work for me: stackoverflow.com/questions/6698410/add-all-rows-for-a-result –  bkaid Jul 14 '11 at 18:50
    
@OffBySome: I saw your moderator flag, but the user ID that posted the question has a different email address and IP address, so I have no way of verifying that it was you who posted the question. –  Robert Harvey Jul 14 '11 at 20:32
    
@Robert Harvey Close it as duplicate of this one then: stackoverflow.com/q/6338256/265570 And regardless, it didn't give me the code formatting prompt. –  bkaid Jul 14 '11 at 20:42
    
@OffBySome: That's close enough. Do you want me to delete the extra account? –  Robert Harvey Jul 14 '11 at 20:53
    
@Robert Harvey yes please. –  bkaid Jul 14 '11 at 20:55
    
@off that was a minor bug I checked in.. it was matching [A-Z]() but not [A-Z0-9](). So fiscal2() no longer matched, which was a killing blow because it failed to trigger matching of all indented lines underneath it too. Minor condition, now fixed -- thank you for checking it :) –  Jeff Atwood Jul 14 '11 at 22:14
    
@Jeff funny it worked for you when you tested because I just pasted the exact same thing in. –  bkaid Jul 14 '11 at 22:19
    
@off the code changed between then and now. I made it more strict. –  Jeff Atwood Jul 14 '11 at 22:24
add comment

Good idea. Another dead giveaway is the use of the <code> tag. People sometimes attempt to format code using rules they already know from somewhere else, with results like:


#include 
#include 

...
share|improve this answer
    
+1 for finding code blocks and reparsing it somehow (yes, fair bit of effort, but even just all CHAR(10)||CHAR(13) between the <code> getting CHAR(10)||CHAR(13) and the <code> removed would be nice enough –  jcolebrand Jan 11 '11 at 21:46
    
Often you need to use <code><pre> in order to get code to format correctly when indenting doesn't work for some reason. –  Justin Apr 29 '11 at 13:38
    
@Kragen: I've never seen indenting not work. Can you give an example? –  Greg Hewgill Apr 29 '11 at 20:20
    
I'm struggling to find any but there are definitely a couple around (thought I could use the data dump to find them, but that has the post HTML instead of the Markdown) –  Justin Apr 30 '11 at 3:07
    
Actualy I think I it's meta.stackoverflow.com/questions/3327/… I was having problems with - now I know how to fix it though. –  Justin May 8 '11 at 23:01
add comment

I don't think that the rules around detecting malformatted code needs to be that complicated. From what I've seen newline characters in markdown that don't correspond to a corresponding new line character in the displayed post is unintentional more often than it's not - a high number of these in a post (maybe more than 1 occurrence per 50-100 characters) is probably a strong indication that the user needs some help.

This would also catch the times where users get things wrong outside of code blocks (which is also pretty frequently).

All they would need is some subtle prompting:

  • Select code regions and press the code button to format code correctly
  • Use two newlines to separate paragraphs
  • etc...

I also can't see any real benefit in showing this more than once per user - if they didn't read it the first time its just going to be annoying the second time and the same information is now easily accessible with the new Inline Comment and Post Markdown Help if they ever want to find it again.

share|improve this answer
add comment

You must log in to answer this question.

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