By far the most common edit that I have to make in SO is to correct newbie users who don't know how to format their code. This got me thinking: is there a way to automatically recognize when a post contains unindented code and present the asker with a popup or warning message? Simply pointing out that their code isn't formatted and pointing to the format button would go a long ways towards cutting down the difficulty that new users have. A relatively simple Bayesian classifier could probably catch enough cases to be useful.

I welcome implementation discussion and counter-proposals in the answers.


6 Answers 6



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.

  • 6
    I think the reputation limit it too low. I've seen poorly formatted code from people with much higher reputation.
    – ChrisF Mod
    Commented Jul 13, 2011 at 12:35
  • @chris urls please? Commented Jul 13, 2011 at 13:01
  • I'll let you have some if I come across them ;)
    – ChrisF Mod
    Commented Jul 13, 2011 at 13:02
  • @waffles: Does it really still say "Your question couldn't be submitted"? I hope not! (It would be terrific if it would say question / answer / edit appropriately. In particular for edits it would be nice. For questions / answers a simple "post" is alright of course.) Commented Jul 14, 2011 at 11:47
  • @Hendrik yeah it says your question/answer ... but I changed the specific error to say post ...
    – waffles
    Commented Jul 14, 2011 at 11:49
  • @waffles: I'm not sure if I understand you. Above it says "Your question couldn't be submitted because: Your post appears ..." Is this by intention? Commented Jul 14, 2011 at 11:51
  • well yes @Hendrik ... the code that inserts the message does not know if its a question or answer ... if it REALLY bothers you I can change it, its just a bit of a hassle
    – waffles
    Commented Jul 14, 2011 at 11:52
  • also there are a bunch of other errors that use the term post it is consistent
    – waffles
    Commented Jul 14, 2011 at 11:53
  • @waffles: The inconsistency in the 7 or so answers here that you just edited doesn't REALLY bother me. But it would bother me if the actual message had this inconsistency. It would be OK if it says "post" at all places. Commented Jul 14, 2011 at 11:55
  • @Hendrik if we have errors in a Question ... we say "Your question couldn't be submitted" ... if its an answer "Your answer couldn't be submitted" .... later Validate.Body will insert more errors that use the term Post ... again if you find it really annoying I can go through a sweep it ... I just don't see it as a big source of confusion
    – waffles
    Commented Jul 14, 2011 at 11:58
  • @waffles: OK, it seems that I didn't immediately get what you were writing. What you wrote in your last comment sounds good to me. Commented Jul 14, 2011 at 12:00
  • This explains the poor indentation I've been seeing. I still have to edit their questions, so does this really fix anything?
    – Erik B
    Commented Jul 14, 2011 at 12:01
  • @Erik yes ... we log every block and so far in the last 10 minutes it stopped quite a few problematic posts
    – waffles
    Commented Jul 14, 2011 at 12:02
  • @waffles, yes, but is the submitted post properly indented or do they just fix the formatting with minimal effort?
    – Erik B
    Commented Jul 14, 2011 at 12:09
  • Could we have some statistics about how often this blocks? And,maybe a list of old such posts on the tools page? Commented Jul 19, 2011 at 0:40

A scan of the post to see if it contained common keywords outside coding markup might work, though it would have to be just a hint ("Your post looks like it contains code, have you formatted it correctly") rather than a popup.

However, there are many potential languages - c#, c++, XML, XAML, VB, php, perl, etc. and what's common in one is rare in another and a superset of all languages might generate a lot of false positives.


I think a common pattern for "code-looking text" might be "a bunch of short(ish) lines with a newline at the end of each one". Especially if there's any kind of other indent structure.


I think our best bet is going to continue to train new users in the preferred ways of the community, especially after Jeff's latest blog post on user myopia. I'm a bit with Jeff in "where do we draw the line"?


I'm inclined to agree with ChrisF -- this will likely cause too many false positives. A good heuristic for this that didn't miss any, across the gazillion languages (Ook, anyone?), and was performant and non-intrusive to users ... That'd be a Holy Grail.

How does the heuristic determine inline code (I'm talking about using object.ToString()?) from real code? (I suppose the tick-marks help, but still.) I can think of a variety of situations where code may not even include language keywords -- with as much abstraction as we use these days, it's entirely plausible to have a code block which falls into this. These are just two of the traps.

Not to mention that we're talking hard-core string analysis on posts which can be a couple K ... Every post ... Which happens on SO, what, about every second or so?

No, I think in this case, the "cure" may indeed be worse than the illness. Anyway, I'm working on a Strunk & White here! :)


Ha. I've been on SO for 5+ years and only today have I discovered the { } button (after reading about it on a blog post). I had been indenting my code in my local editor all this time.

Maybe the problem is that said button is just too easy to overlook. I've posted a proposal on how to remedy that.

You must log in to answer this question.

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