68

I'm making this a to implement a solution to the problem described in this question: Why are inline code spans across multiple lines allowed?

From time to time, I see some code formatted using inline code spans like this:

<html> <head> <meta charset="utf-8"> <title>I don't know how to format code properly</title> </head> <body> <p>I'm not able to make my code look properly.</p> <p>Can someone help me? Thanks.</p> </body> </html>

As far as I'm aware, there is no valid reason why one should have line breaks inside inline code spans. If your code is more than one line long or not within a sentence, it should be inside code blocks (indent all lines with four spaces):

<html>
<head>
    <meta charset="utf-8">
    <title>How to format code properly</title>
</head>
<body>
    <p>You can format your code by indenting all lines of your code with four spaces.</p>
    <p>Select all your code, then click the {} button above the text box.</p>
</body>
</html>

The issue still exists (recent example) and continues to be an annoyance. This is something an automated replacement could easily solve without having to waste people's time to edit these out* (which someone just did for the linked example).

Deduplicator makes the following argument in a comment:

[...] there is a valid use for them: Formatting error messages, which may get more line-breaks, but must keep their original linebreaks and should not be interpreted as formatting...

I don't agree with this, because a code block actually does a better job at preserving line breaks. In fact, a code span does not do its job at all if there are concurrent line breaks:

`This is a line break

2 breaks above`

`This is a line
break

2 breaks above`

Another possible argument for using code spans is that they do not unnecessarily prettify (color-code) parts of the text (e.g. numbers, words starting with an Uppercase letter) when it's not necessarily code, but in those cases <!-- language: none --> is sufficient to prevent this from happening.

While new users may not know this, a code block with incorrect syntax highlighting is still better than a multi-line code span if the contents span multiple lines. Sometimes the highlighting actually does a good job, especially with stack traces:

Exception in thread "main" java.lang.NullPointerException at com.example.myproject.Book.getTitle(Book.java:16) at com.example.myproject.Author.getBookTitles(Author.java:25) at com.example.myproject.Bootstrap.main(Bootstrap.java:14)

Exception in thread "main" java.lang.NullPointerException
        at com.example.myproject.Book.getTitle(Book.java:16)
        at com.example.myproject.Author.getBookTitles(Author.java:25)
        at com.example.myproject.Bootstrap.main(Bootstrap.java:14)

* Assuming the rest of the post is still worth keeping and the author just doesn't know how to format code blocks. The competence of people who can't format code blocks properly is a completely different argument.

11
  • 18
    The <!-- language: none --> trick is something new users won't know. Even if they by some miracle read the help, they have to click the question mark -> click advanced help -> scroll down the page to Syntax Code Highlighting -> click show more -> scroll to the bottom of the expanded section -> on the last line there it is.
    – Aaroninus
    Commented Sep 29, 2016 at 14:13
  • 24
    @Aaroninus When the contents span multiple lines, I personally prefer a code block with messed up syntax highlighting over a code span.
    – SeinopSys
    Commented Sep 29, 2016 at 14:31
  • 13
    While we're at it, I suggest allowing fenced markdown blocks. I don't see them as often as the multi-line/inline code blocks but it still comes up since it's a very common way of marking code blocks thanks to GitHub. It also means I don't have to waste time adding extra spaces at the start of each line or ensuring a copy-pasted snippet is indented correctly.
    – Mike Cluck
    Commented Sep 29, 2016 at 19:01
  • 4
    @MikeC The usual counter is that "you can just use Ctrl K or press the {} icon to automatically indent the code you've selected", but I think that's a pretty weak argument against implementing a feature most of us developers are used to on many other platforms. I'd love to see that implemented as well, but I strongly feel the developers that be don't really want to mess with the Markdown parser for whatever reason.
    – SeinopSys
    Commented Sep 29, 2016 at 19:24
  • @MikeC meta.stackoverflow.com/a/260541/248058
    – Knu
    Commented Sep 30, 2016 at 14:57
  • I don't understand how/why this is a problem.
    – Daniel
    Commented Sep 30, 2016 at 15:04
  • @Daniel Inline code spans are being used to represent code blocks. To some extent, this is the same issue as using <kbb> to make links look like buttons. In both cases the feature is not being used for its intended purpose.
    – SeinopSys
    Commented Sep 30, 2016 at 16:10
  • @SeinopSys Is the specific use cases for appropriate "inline code spans" documented somewhere? Absent that this feels more like you asking the system to disallow something you personally don't like.
    – Daniel
    Commented Sep 30, 2016 at 16:45
  • 2
    @Daniel The fact that it's called "inline" (here) is enough documentation. It's meant to be used in line with the text, not as a block. It's styling, markup and behavior also assumes that it's used inline (e.g. no <pre> tag in the rendered output).
    – SeinopSys
    Commented Sep 30, 2016 at 17:18
  • 1
    @SeinopSys, You win. Even if it were appropriate to use inline code blocks for this purpose, the code block format simply looks better.
    – Daniel
    Commented Sep 30, 2016 at 17:19
  • Why not have a button on the toolbar for "insert code fragment", which would automatically insert the <!-- language: none --> thing, and also somehow help the user indent the code.
    – user663031
    Commented Oct 1, 2016 at 22:05

0

You must log in to answer this question.

Browse other questions tagged .