5

There might be similar posts regarding this in the past. So, please forgive me.

I often find it hard to edit a post which has lengthy code (indented using 4 spaces). Also, there are higher chances of editing wrong while dealing with lengthy code which is indented using 4 spaces.

So I generally feel indentation is only good for one-line code and backticks are good for multi-line code.

What's the community's opinion about this?

4 Answers 4

10

My personal preference is to use the triple backtick as that's the most common way to set code blocks in Markdown outside of Stack Exchange. I find it much easier to use.

When I am editing someone else's post though, if it's a small piece of code, I just select it and click on the {} button in the editor. The default behaviour of that button is to use 4 spaces.

If it's a large block of text that needs to be formatted as code, I find it much easier to just add triple backticks at the start and triple backticks at the end.

On a side note, fenced markdown for code blocks wasn't available on SE until it was released in 2019. Another thing to note is that in the case of a very large text, especially logs, using 4 spaces might not be possible as it can cause the post to reach its character limit. Although, I have never encountered this issue myself.

In summary, I don't think we need a consensus. Just go with whatever you prefer until SE decides to stop supporting one of the methods.

2
  • 1
    I agree with this but a small correction, the editor button is contextual, if you use it inline it will use back ticks and on a separate line it will use four spaces, it does not change your answer but is worth noting.
    – Mark Kirby
    Nov 24, 2020 at 11:57
  • 1
    The Ctrl+K shortcut can also be used instead of the {} button (it's also contextual). Nov 24, 2020 at 13:00
4

Just like Dan, I don’t think we need a consensus. In your posts, use whatever you want (but keep the difference of in-line vs. block code formatting as Mark Kirby noted).

However, if I edit someone else’s post, I apply a bit stricter rules. I use ``` for multi-line code as only the line before and after the code block gets modified in the diff view while with 4 spaces, it would be every line of the block. In the case of a single line code block (mostly a command), I prefer 4 spaces because just one line gets modified – and I save at least 4 bytes1 of the disk space. ;-)


1 Just double ``` means 6 characters. And each newline means 1–2 characters depending on the platform.

3

I actually know of five ways to format code on SE, not including the editor button (click edit to see):

back ticks

html

four spaces
blocks using back ticks
blocks using tilde

I know, that is even more confusing, but if we look at the way they format code, I think we can see that back tick and html are for inline code like this and the other are for formatting whole blocks of code.

To back up my claim, if we use the button in the editor, it makes back ticks for this and four spaces for

this

so would strongly imply this is what the different ways to format code were intended for, even if I can't find a direct quote to back it up right now.

10
  • 1
    Since I'm a user with low reputation, I can't click edit on meta posts. I suggest you to share the source link. Nov 24, 2020 at 12:00
  • Why? You have 800 rep, that should allow suggesting edit surely. It is a long time since I was new but we have a suggested edit queue. Do you see the edit button?
    – Mark Kirby
    Nov 24, 2020 at 12:02
  • @MarkKirby There's nothing like edit suggestions for posts on meta.
    – Kulfy
    Nov 24, 2020 at 12:03
  • There is a whole review queue meta.askubuntu.com/review/suggested-edits it works just like main but you need five rep to do anything here.
    – Mark Kirby
    Nov 24, 2020 at 12:04
  • 1
    This is what I'll be shown when I click edit: Suggested edits are not allowed on non-tag-wiki posts on meta sites. Nov 24, 2020 at 12:05
  • 1
    @MarkKirby IIRC that's for tag wiki and excerpts only.
    – Kulfy
    Nov 24, 2020 at 12:05
  • 2
    @technastic_tc Well, seems you guys are right, who knew? This seems bizarre to me, I cant see why it would be like this?
    – Mark Kirby
    Nov 24, 2020 at 12:10
  • above comment also @Kulfy
    – Mark Kirby
    Nov 24, 2020 at 12:11
  • @MarkKirby Do read this: meta.stackexchange.com/q/87890 and meta.stackexchange.com/q/81681 IMO, suggested edits should be allowed in meta sites as well. Some relevant feature-requests posted on MSE: meta.stackexchange.com/q/129033 and meta.stackexchange.com/q/223885 Nov 24, 2020 at 12:15
  • 1
    @technastic_tc I have seen it, I am writing another proposal to allow this on MSE.
    – Mark Kirby
    Nov 24, 2020 at 12:16
3

I don't think it makes any difference at all. I am very used to the 4-spaces indentation, my muscle memory works that way and there is a nifty userscript that means I can indent by just selecting text and hitting tab, just like you do in many code editors. Selecting a block of text and hitting tab to indent it, is so common that I am sure many of us will be very tied to it from the various editors we use. I really, really don't want to lose this.

In any case, as far as I can tell, both three backticks and four spaces produce the exact same html rendering, so I really don't see any reason to have guidelines. We can all use whichever of the two (or ~~~) we prefer.

3
  • It seems that the link to the script does no longer work. Nov 24, 2020 at 13:01
  • @BeastOfCaerbannog please comment on the stackapps post then, I can't do anything about that and I want to link to he stackapps post here.
    – terdon
    Nov 24, 2020 at 13:04
  • There are already comments about that on the Stack Apps post. It is possible, after reading the comments, that the OP is not willing to add it back. Nov 24, 2020 at 13:12

You must log in to answer this question.

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