# Catch and educate users posting code without properly formatting it

**URL:** <https://meta.discourse.org/t/catch-and-educate-users-posting-code-without-properly-formatting-it/67958>\
**Category:** Feature\
**Created:** [August 13, 2017, 9:29am UTC](https://meta.discourse.org/t/catch-and-educate-users-posting-code-without-properly-formatting-it/67958 "2017-08-13T09:29:03Z")\
**Posts on this page:** 1\
**Showing post:** 14

<div class="post-metadata">

**Author:** ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)\
**Post date:** [October 13, 2017, 10:13pm UTC](https://meta.discourse.org/t/catch-and-educate-users-posting-code-without-properly-formatting-it/67958/14 "2017-10-13T22:13:34Z")

</div>

> [@Sujan](#):
>
> more than 2 { or } in one paragraph  
> more than 2 [or] in one paragraph  
> more than 2 ; in one paragraph  
> more than 5 \t (tab characters) in one paragraph

I think you need a score which resets every (x) paragraphs that don’t contain enough characters to meet the threshold. So

1. Loop through entire body
2. Look at the current paragraph
3. Check our current “is this code?” score
4. Does this paragraph have a higher than expected special character score? Also, is this paragraph shorter than we’d expect?
5. If we are already in “is this code?” mode, add the number of lines in sequence so far to the current score, to make it greater
6. If the last (x) paragraphs have zero code, set “is this code?” to zero and record the ending line number
7. If this is the first paragraph to trigger code mode set “is this code?” to the value of the paragraph score, and record the starting line number

You’d need a sizable test corpus for this to work. I know @zogstrip has one because I’ve seen it 😉

Also

> we can realistically limit our check to, say, the “big ten” languages. Per the tags page that would be C#, Java, PHP, JavaScript, Objective-C, C, C++, Python, Ruby.

This is also interesting as an approach

> Run your syntax highlighter on the text. If it ends up highlighting some high percentage of it, it’s probably code.

However Random Syntax Highlighting tends to love highlighting numbers in text for no reason. So you’d need to play with that, but it’s a very interesting idea.

```plaintext
What happens when you randomly type text and have it in a syntax highlighting block? 

Let's find out. Is this code? I dunno, is it code? You tell me.

```

 ![image](https://global.discourse-cdn.com/meta/original/3X/c/0/c0d8eb6c4ed58a3104389ac1e97b9c759938645c.png)

---

_[View the full topic](https://meta.discourse.org/t/catch-and-educate-users-posting-code-without-properly-formatting-it/67958)._
