26

I have seen some edited posts, which have been edited to change the way that code is formatted. Sometimes, this is also the only edit they make.

It will be changed from four spaces indentation to triple backticks or vice versa.

I'm pretty sure that both of these choices are interpreted the same by SO as well.

Is there a reason for why people might change the formatting?

To clarify, I'm not asking about the preferred way of formatting as in this question. My question is asking why some people change the formatting.

Examples: (I cannot find the ones I saw previously but I will add more if I find them)

24
  • 43
    There's no difference between them really. If that's the only edit made to a post, then it's an unnecessary edit.
    – cigien
    Commented May 19, 2021 at 2:48
  • 1
    @cigien Not really. My question is why some people change the formatting. That question is asking the preferred mode of formatting. Commented May 19, 2021 at 3:06
  • In that case, it's better to make the title of the question be clearer about what you're asking, instead of adding it as an edit to the question. I've edited the title a bit to make it clearer. It would also help if you added whether these edits that you're referring to were suggested edits, or were made by users with full editing privileges.
    – cigien
    Commented May 19, 2021 at 3:11
  • @cigien I only can find one that I saw today, but the ones I saw previously (including those with editing privileges), I cannot find unfortunately Commented May 19, 2021 at 3:15
  • @12944qwerty what more is missing from the answer below? Commented May 19, 2021 at 3:34
  • 18
    The only plausible reason I can think of is adding a specific language to the block in case there are conflicting tags on the question for example. If the edit was not made to add a lang specification then it is really unnecessary
    – Tomerikoo
    Commented May 19, 2021 at 6:48
  • the edit looks perfekt and i would upvote the editor if i could
    – nbk
    Commented May 19, 2021 at 10:59
  • 16
    Attempting to close a question tagged with [discussion] as opinion based? Defeats the whole purpose of asking (or even having that tag) in the first place, doesn't it?
    – Lino
    Commented May 19, 2021 at 11:19
  • 2
    Sometimes the code indentation very messed up, so I plug it into an indenter and paste it back in. But this looses the initial 4 spaces, so I'm forced to use the backticks.
    – aheze
    Commented May 19, 2021 at 18:18
  • 1
    @aheze, select, Ctrl+K is your friend!
    – HAL9256
    Commented May 19, 2021 at 18:59
  • Specifically for the linked example, the user probably just wanted to fix the formatting for the quoted error message, but couldn't because the edit was too small (needs to be 6+ characters for low-rep users, I think?), so the formatting of the second block got over that hump.
    – Izkata
    Commented May 19, 2021 at 19:50
  • 1
    Interesting. I had been hypothesizing that there are a more and more minor edits by people to build up their scores--but maybe not.
    – adr
    Commented May 19, 2021 at 21:24
  • 3
    In many cases they do it just to earn 2 easy reps.
    – il_raffa
    Commented May 20, 2021 at 7:40
  • 1
    @PM2Ring You may not realise it, but what it does is discourage good-faith editors from making contributions. I certainly don't bother fixing stuff now - I have no idea whether I now have edit permissions, but I've learnt my lesson; the community prefers you not to make edits. If you used the Wikipedia model, where I often find myself fixing typos and making other small tweaks, you might get better outcomes. I mean, if someone is being deliberately disruptive it's trivial to bypass the 6-char limit, no?
    – HappyDog
    Commented May 21, 2021 at 13:34
  • 1
    Also, why on earth does it require 3 people to approve a simple copy-edit! There's your problem, right there!
    – HappyDog
    Commented May 21, 2021 at 13:34

5 Answers 5

69

It depends on the case. As most of the time the "why someone did something" question is asked, only the specific "someone" can really answer it.

In the one you linked, I would say that the editor wanted to change the formatting of an error message to make it more readable, and since they were short of the required "6 characters" for a suggested edit they changed the code formatting option to get there.

I will sometimes change from indented to fenced because I find it easier to fix code formatting issues on fenced code than on indented code.

Or because it's easier to hint the language in cases where the language is not correctly guessed from the question tags (e.g. a post that includes code in more than one language).

There is not a single answer to "why". You'll need to figure it out in each case, or ping the editor if you are really curious and think the question is worth asking.

2
  • 1
    +1 for ease of formatting; fences only need to be added to two lines, but leading indentation must be added after ever carriage return within the code content.
    – Tom
    Commented May 21, 2021 at 19:04
  • I completely agree with the language hints part of this answer: if a question is tagged "sql" and "vb.net" then the vb.net part gets the colours of sql unless explicitly tagged otherwise, and the comment-style language thing is a lot of hassle to look up. Commented May 21, 2021 at 22:03
32

Spaces to fences might be useful. In cases where two language tags are specified the larger tag, by questions, is taken to highlight the code. E.g. a question tagged and will highlight everything as Java by default when using spaces, which is the default when pressing ctrl+k or using the interface. If you then have both a Java and a MATLAB code block, use triple fences to ensure correct highlighting for both languages:

```matlab
matlab code
```
```java
java code
```

I'd say that changing fences to spaces is never necessary and is potentially harmful, given that you might destroy a dual-language post's highlighting as outlined above1.

1 as Justin mentioned in their comment a similar construct is possible for four space indented code blocks, but given that that is not intuitive for non-HTML users, I'd guess that's an edge case of people who know what they're doing.

10
  • 11
    <!-- language: lang-java --> before spaced code blocks selects syntax highlighting as well.
    – Justin
    Commented May 19, 2021 at 18:30
  • 10
    @Justin - <!-- language: lang-java --> is deprecated after the transition to CommonMark. From What is syntax highlighting and how does it work?: ...
    – dbc
    Commented May 19, 2021 at 19:05
  • 13
    The former method of specifying a highlighting language can still be used for HTML code blocks: place an HTML comment <!-- language: lang-or-tag-here --> before the <pre><code> tags and it will work. Also, this former method hasn't been completely removed for four-space indented code blocks, but merely deprecated. While it will still work for the time being on four-space indented code blocks, it may/will be removed in the future.
    – dbc
    Commented May 19, 2021 at 19:05
  • 9
    @Justin sure, but to anyone who doesn't have HTML as a native tongue, just adding the language after three backticks is a lot easier to remember than the HTML syntax. So yes, it is possible to do this with the four-space indentation, however, for most people it's a lot more cumbersome I'd say
    – Adriaan
    Commented May 19, 2021 at 20:50
  • @dbc But sometimes it's the only thing that works.
    – TylerH
    Commented May 19, 2021 at 21:36
  • 8
    @TylerH - That's interesting. Got an example?
    – dbc
    Commented May 19, 2021 at 22:28
  • @dbc Not off the top of my head, but I recall having to revert to the comment syntax for when I needed to highlight sections of code with different language syntaxes, including setting one section to none when the auto-detected language was wrong.
    – TylerH
    Commented May 20, 2021 at 14:05
  • @TylerH don't such cases occur when you're trying to have formatted code blocks inside of bullet lists? I remember those being aweful to get formatting into.
    – Adriaan
    Commented May 20, 2021 at 20:26
  • @Adriaan Probably... any time you introduce formatting into a post and want the code to be within said formatted sections, you encounter lots of issues
    – TylerH
    Commented May 20, 2021 at 20:28
  • @Adriaan - Just FWIW, what you add after the three backticks isn't a language unless you use the lang- prefix. The two code blocks in your answer hint the matlab tag and the java tag (that is, whatever the default language is for those tags), respectively. For those two it comes to the same thing. For TypeScript, it doesn't (the default language for the TypeScript tag is JavaScript and SE has said firmly they don't intend to change that, despite it making little sense). Commented May 21, 2021 at 7:09
25

Personally, whenever I'm editing a post that happens to have code, 99% of the time I'll convert from indent syntax to code fences, because it makes the division between "code" and "not code" way, way clearer.

This stems largely from seeing many, many posts which have an errant, unformatted closing curly brace } hanging out just below an area of indented code... Enclosing it in fences makes that mistake harder to make, and honestly just makes it feel cleaner overall to me as an editor. Being able to use non-deprecated language specifier syntax (```lang-X) is a nice bonus too.

3
  • 3
    I used to make edits like that, where I just switch from 4 spaces to 3 backticks. While the backticks method is advantageous (you can use language specifiers), it's more of a personal preference, and I tend to reject such edits if I see them unless they are accompanied by another substantial change. Edits that just add a language specifier are extremely popular but should be accompanied by another change for the edit to qualify as a good one.
    – 10 Rep
    Commented May 19, 2021 at 21:14
  • 2
    @10Rep I don't mean to say that I intentionally make edits of just the format switch (though realistically, I probably have before); I agree with you that those edits are pretty superfluous most of the time. All edits should fix and improve as much as possible, and I certainly try my best to do that! I disagree that adding the syntax highlighting is too minor a change if there's truly nothing more to fix, but that case is admittedly pretty rare lol. There's more or less always more to improve.
    – zcoop98
    Commented May 19, 2021 at 22:01
  • 1
    I wasn't accusing you of making such edits. I was talking in general, of why other people make such edits. Your reasons are good, and I agree with making edits like that. And realistically, I used to do it for the +2 rep ;)
    – 10 Rep
    Commented May 21, 2021 at 3:08
4

I guess this often happens when you fix small a typo or formatting, but since there is a "6 min characters" limit for the edit, we have to add a couple of characters more. The code block demarcation is a simple and safe way to achieve it and be able to commit your edit.

-7

I've done edits like that in the past, with the intention to help new users learn another convenient way to format the code on Stack Overflow. I would do a formatting edit when both conditions are true:

  • The user is new - there is a palm icon next to the user name, and
  • The formatting is off - there are visual imperfections in the formatted code.

I would sometimes leave a comment explaining how to do the formatting, i.e. something like "you can format your code by selecting the code block and clicking the button with two curly braces on it".

1
  • 2
    This question isn't about editing non-formatted code into code, it specifically asks why posts are edited to change four space indentation code formatting to triple backticks, or vice versa.
    – Adriaan
    Commented May 20, 2021 at 21:36

You must log in to answer this question.

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