10

Recently I've seen a lot of code being formatted as inline style. This is not how it should be done.

All code should be formatted with one of two styles.

  1. If it is just 1 line, with other text around it, use the ` ` formatting, like this:

    Hello. This is code and continued sentence.

  2. If it is one line, but on its own, it should have 4 spaces before it:

    Hello. This is code
    

    To save adding the 4 spaces to each line manually, highlight your block of code and click the { } button on the editing toolbar.

    Sometimes the inline code is better - if scrollbars are created. For long one liners the inline code can better, so it code wraps. I'll leave this up to the reader to use either, depending on what they feel fits - or split the code onto multiple lines and use 3.

  3. If it is multi line, use the 4 spaces before each line:

    Hello.
    This is code
    

    To save adding the 4 spaces to each line manually, highlight your block of code and click the { } button on the editing toolbar.

  4. If it's multi line and in a list, indent the code 8 spaces:

    • Hello
    • This is a list

      Hello
      This is code.
      

How not to do it.

  1. Accent (``) formatting on a multi-line code block:

    Hello. This is badly formatted code

  2. Quote format (even if you append the 2 spaces):

    Hello. This is badly formatted code

  3. 2 line breaks between each line:

    Hello.
    
    This is badly formatted code
    
  4. No formatting. Seriously, this is so hard to read. Compare this with this:

    Hello. This is badly formatted code

  5. HTML tags (<code>)

    Hello. This is badly formatted code

    HTML tags (<pre>). It looks okay, but is less nice to edit.

    Hello.
    This is badly formatted code
    
  6. Using speech marks

    "Hello. This is badly formatted code"

Reminder:

All code should be between ` characters (grave accent, often found below the ESC key), or have 4 spaces at the start of the question.

`Only format inline code with this syntax`
    Format multiline code with this syntax
3
  • Good but that would be hard to make new comers to follow such guidelines. – sepehr Jun 30 '15 at 19:47
  • 1
    @SepehrLajevardi I'd recommend linking this into post, so they learn. It's not just guidelines, it is the right way to format. – user3 Jun 30 '15 at 19:47
  • It's good to link to the post, but sometimes it's also worth making the edit - if it's a good question/answer with formatting issues – SamMorrowDrums Apr 17 '18 at 17:04

You must log in to answer this question.