-21

I'm baffled why people are using 4 space-indent when posting code. It tends to screw up indentation on posts. It also does not have language-specific highlighting.

What I always use is the triple `. (backtick)

```java
// code
```

Why do people use the other method? Are there some advantages I don't know about?

6
  • 7
    Unless something changed recently, I think the code button in the editor still uses the indent method. Commented Dec 11, 2019 at 0:24
  • 19
    4 space indenting for code format had language specific highlighting years before using 3 ``` marks for code fencing was possible on Stack Exchange was possible. I suggest you read What is syntax highlighting and how does it work? I'd also note that code fencing is not actually a part of Markdown; it's an extension.
    – Makyen Mod
    Commented Dec 11, 2019 at 0:30
  • 2
    I sometimes use 4-space indent when posting a single standalone line of code, since it takes fewer characters than 3 backticks + newline + <code> + 3 backticks + newline. Commented Dec 11, 2019 at 0:34
  • 5
    "It also does not have language-specific highlighting." ... can you proof that the language tag isn't used anymore for syntax highlighting? Currently looks like an empty argument.
    – Tom
    Commented Dec 11, 2019 at 0:36
  • 4
    "It tends to screw up indentation on posts" so does the code fencing, honestly. Or rather, it's not a problem with how you mark something to be presented in code format, the problem is that people post not properly formatted code. So, it's going to have "screwed up indentation" whatever you use. There is also the people who use a code fence but start their code after the triple backticks, so the first line of code is omitted.
    – VLAZ
    Commented Dec 11, 2019 at 6:43
  • Sometimes really badly indented code + 4 spaces renders the first line as plain text.
    – aheze
    Commented Dec 21, 2020 at 20:40

2 Answers 2

18
  • Indenting can be done with a single shortcut crtl+k, not multiple backticks at several locations across the post

  • Using code fences can screw up the formatting if the code itself contains code fences, this for example sometimes happens in the tag

  • "It also does not have language-specific highlighting." is just wrong. With indention the language can easily be adjusted with e.g. <!-- language: latex -->

  • "It tends to screw up indentation on posts": only when used incorrectly :)

3
  • 3
    A way around code fences messing up nested fenced blocks is using another character for the outer than the inner fences, for example outer ~~~ fences to nest inner ``` ones. Commented Dec 11, 2019 at 1:15
  • 1
    @BenjaminW. Thanks for this nice trick! Commented Dec 11, 2019 at 1:17
  • Note that the third point is incorrect as of 2020 - language hints in space-indented blocks are no longer supported. Commented Jul 6, 2021 at 17:22
15

The triple backtick is a very new feature, and there are lots of users who just...don't use it or don't know about it.

The code renders just fine anyway on the site if you use 4 spaces or triple backticks, which is what really matters at the end of the day.

1

You must log in to answer this question.

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