34

See the code block in the first revision of this question Hi! I am learning c and I don understand why this code doesnt work.

Notice that the first line #include <stdio.h> is indented an extra 4 characters. This is not an isolated occurrence, I see it many times every day and usually just fix it silently (I haven't fixed this one so you can see it). So there must be something happening when new users paste code into the question that causes the first line to get extra indentation.

I think this is related to What causes new contributors to miss the last line in a code block and paste the image as a hyperlink?. This happens when the user pastes a function where the body is indented but the closing } is not. The above issue causes the first line of the function to be indented, so the start and body get indented to make a code block, but the last line is out of the code block.

If we know why this is happening, maybe something in the question creation flow can be changed to mitigate it.

0

1 Answer 1

56

The cause is a user incorrectly thinking that the "Code Sample" (Ctrl+K) button in the editor should be pressed before pasting the code, instead of after pasting and selecting the code block.

Try it yourself: click the {} button in the toolbar (or press Ctrl+K). It will insert an indented paragraph containing the text "enter code here", which will be selected. If you now paste code, the first line will be indented, but the subsequent lines will have the same indentation as the original (which is likely none). Or alternatively, see what happens in this animated gif demonstrating the behaviour:

Note that this also solves another mystery: the frequent occurrence of the string "enter code here".

This is not actually bad design or inconsistency on the editor's part. The "Code Sample" button works the same way as the standard Bold and Italic buttons. The problem is users not paying attention to the post's preview.

20
  • 1
    This explains some of the questions where the rest of the code isn't in the code block. But the ones I'm talking about have all the code in the block, and the first line is indented an extra level. Look at the question I linked to -- the first line has 8 spaces of indentation, the rest have 4.
    – Barmar
    Commented Jul 27, 2022 at 8:35
  • 9
    It/when we ever move to switching to code fences, maybe it''ll occur less, as they have 3 backticks to put their code between (though I suspect we'll see in influx of the first line missing ;) ); it won't improve the vigilance; not sure that will ever get better.
    – Thom A
    Commented Jul 27, 2022 at 8:35
  • 2
    Oh, I see what happened. They pasted like you said, then added code fences around it.
    – Barmar
    Commented Jul 27, 2022 at 8:36
  • 14
    This is what is Cody is suggesting is happening @Barmar .
    – Thom A
    Commented Jul 27, 2022 at 8:37
  • 5
    The difference between code-indent versus bold/italic is that the latter work with multi-line content. So the "enter code here" is somewhat misleading as code-indent usually doesn't properly handle entering code manually. It would be a different story if the {} button added code fences. Commented Jul 27, 2022 at 8:37
  • 10
    @Larnu unlocked the achievement of getting me to upvote a comment containing an animated GIF. Feel free to inline it in the post.
    – Cody Gray Mod
    Commented Jul 27, 2022 at 8:38
  • 10
    So maybe the solution is to change this control so it doesn't create the Enter code here placeholder when nothing is selected. Instead it should tell the user to paste the code first and mark it.
    – Barmar
    Commented Jul 27, 2022 at 8:43
  • 17
    Or it should create the code fences with a non-indented Enter code here in between them. This will also solve the problem of users putting the code fences on the same line as the code.\
    – Barmar
    Commented Jul 27, 2022 at 8:45
  • 7
    @Barmar The new beta Stacks editor has two code buttons: one that creates single backticked code (for inline code) and the other that creates code fences. This editor will replace the current one in the future.
    – Laurel
    Commented Jul 27, 2022 at 14:41
  • 2
    @Laurel So we can expect more multiple-line inline code blocks? ;-) Commented Jul 27, 2022 at 14:50
  • 19
    This is yet another reason why the editor should favour code fences. Commented Jul 28, 2022 at 4:09
  • 30
    "This is not actually bad design ... The problem is users" - A common sentiment, I'm sure.
    – kaya3
    Commented Jul 28, 2022 at 12:47
  • 11
    @kaya3, yes, and, speaking as a software developer, it's usually a sentiment made by software developers that think they know more about the how the software is going to be used than the customer they are making it for. Commented Jul 28, 2022 at 15:16
  • 19
    "This is not actually bad design or inconsistency on the editor's part." no, it is inconsistent. If I press the bold button and then paste in some text, all of it will be bold. It's not wrong to assume that a code button will work that way. Especially since it does work that way in many other editors. The whole mechanism is the opposite of what you'd expect - bold will add the start/end delimiters and anything in between will be bold. While the code button marks each individual occurrence as code.
    – VLAZ
    Commented Jul 29, 2022 at 6:23
  • 2
    @PaulRougieux add a code fence before and after. Also where the mouse cursor is (or the beginning of the selection, if anything is marked) should be placed on a new line It would work more like bold and other markdown. It's not exactly the same but still better. If you use the code button and paste in code, it would be consistent with marking the code and pressing it.
    – VLAZ
    Commented Jul 29, 2022 at 14:16

You must log in to answer this question.

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