24

Possible Duplicate:
How about newlines within comments?

When posting a comment in stackoverflow how do I add newlines? I pasted some multi-line code example and stackoverflow stipped the newline characters and the code looks ugly!

  • 1
    if (args.Length > 0) { bool connected = bool.Parse(args[0]); string connectedString = connected ? "Connected" : "Not Connected"; } – Tacky Tacky Aug 13 '12 at 13:17
  • 3
    Look how ugly that paste is! Is there a way to paste code which remains formatted? – Tacky Tacky Aug 13 '12 at 13:18
  • Code should not be posted in comments. Add it to the question itself, or if commenting on answer have its author add it to the post itself where is can be formatted properly. – Shadow9 Aug 13 '12 at 14:11
  • Line ­ ­ ­ ­ ­ ­ ­­ ­ ­ ­­ ­ ­ ­ ­ ­ ­ ­ ­ ­­ ­ ­ ­ ­ ­ ­ ­­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­­ ­ ­ ­ ­ ­ break – John Militer May 26 '17 at 21:32
9

From How do comments work?:

Comments are intentionally short, having maximum length of 600 characters, and allow only limited markup.

As such, they intentionally do not support newlines either.

You can use the help link next to a comment box to see what is supported; from the comment formatting help:

Comments support only bold, italic, code and links; in addition, a few shorthand links are available.

  • 7
    This is a very annoying limitation as it means a single line of code gets broken across two lines, so you end up having to post it as two comments instead of one. An alternative solution would be if it forced code blocks on to their own line if they exceed more than about 40% of the column width. – NickG Sep 7 '16 at 8:54
3

This is a question for Meta/FAQ, but you can use ` to delimit a code-portion in comments.

This is an

`example`
  • If you hit help beside the comment box, it gives you an example of markdown. – David B Aug 13 '12 at 13:22
  • if (args.Length > 0) { bool connected = bool.Parse(args[0]); string connectedString = connected ? "Connected" : "Not Connected"; } – Tacky Tacky Aug 13 '12 at 13:22
  • 2
    i put the "`" char in front of my code (see above) and it still strips the newlines. – Tacky Tacky Aug 13 '12 at 13:23
1

Why do you want to put formatted code in a comment? Even with carriage returns I would find that ridiculously hard to read. Put it in the question or answer, then it has full formatting and it will most likely be syntax highlighted in the correct language, making it ever easier to read. Comments are for comments, not for code!

  • 9
    What you're saying is true, but sometimes I suggest code that really isn't an answer, but it is definitely a comment. I do this to suggest significant improvements to the code that won't necessarily solve the problem. See here. – gobernador Aug 13 '12 at 14:03
  • See I would rather have that edited into my answer, then I can make a more constructive decision about what to do with it. Or describe the concept to me and I can edit it (some folks are very "Get off my lawn!" about editing their posts, even when the edit is completely valid). Code samples, even when formatted as code, are absolutely unreadable to me in a comment. – Aaron Bertrand Aug 13 '12 at 14:12
  • 2
    If it's an answer, I agree with you, but I'm of the opinion that one should think twice before editing the code in a question. Additionally, by posting it in comments rather than editing, I can carry on a discussion about it with the OP and ensure he understands why I suggest it. – gobernador Aug 13 '12 at 14:16
  • @gobernador there is nothing wrong with adding a code sample to a question, rather than changing what's there. And there is also nothing wrong with asking in a comment if you can do so, and conversing about it afterward. Unless it's really a one-liner, I will never appreciate code in a comment. Even with laser-corrected vision. It's terribly annoying. – Aaron Bertrand Aug 13 '12 at 14:18

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