Don't. Comments are not supposed to be used for extensive code examples. Use answers instead. Or link to a gist.
– OdedMay 15 '14 at 11:31

13

I disagree. There should be a place for discussions, quotes and code examples that are related to the question without actually answering anything. The comment boxes are awfully limited. Particularly, the best kind of questions create a need for this. You could have several experts of a topic debating, quoting standards etc and trying to find a consensus or demonstrating why they believe that a posted answer is incorrect. The limited comment boxes also limit the amount of intellectual activity on the site. But I better stop writing this comment now, because I'm running out of characters.
– LundinJun 3 '14 at 14:17

@Oded, when I tried following your advice, I was chastised mercilessly for using an answer where I should have used a comment. This problem isn't an Issue with languages like Perl/C, but is a big issue with languages that rely on formatting, like Python, to say nothing when you want to include a few lines of a stack-dump...
– boardriderAug 11 '15 at 12:19

@Oded - You could still have short comments with short code examples that would be more readable if a newline were allowed for. E.g., this. You mean to demonstrate a point with a description ("extensive code") that is not the general case (even if I guess it applies to the example in the OP).
– sancho.s ReinstateMonicaCellioFeb 22 '19 at 7:38

I first, put it in comment because it was not related to the question but to the comment, but it was not looking good so I just put the function in answer and the use if it as comment.
– jcubicMay 15 '14 at 12:03

2

What should I do if I need to paste multiple line of code in comment box as a question in any discussion.
– PlanetHackersJul 30 '15 at 8:24

2

@PlanetHackers: Reconsider if you really need those linebreaks. That said, that much code isn't suitable for the comments section.
– CerbrusJul 30 '15 at 8:49

1

@Cerbrus, languages like Pythoninsist on a certain format for the lines, which the comments clobber.
– boardriderAug 11 '15 at 12:22

What about short paragraphs? It really helps to be able to format your text into readable chunks, even if you only get 600 or so characters. Also, why is code unsuitable for comments? What if you're discussing code and it would be even more unsuitable to post a new answer? Using pastebin or the like is clearly suboptimal...
– ekolisAug 19 '16 at 0:10

That is why they have a cap on maximum length and a minimal support for text formatting . As there is a cap on the total length of the comment, how does it follow that code formatting should not be supported. The example in the question is about 300 characters long, well bellow the 600 character cap.
– David SorokoSep 2 '18 at 11:58

You can't and for no good reason as far as I can tell. The (unofficial) replies seem to focus on the size of an individual comment. This is already addressed by the the overall cap on the comment size which is 600 characters at the moment.

In my opinion it is up to the commenter to deal with the cap in whichever way she sees fit and if she wants to use some of the character budget on a code block it should be up to her. Your example is about 300 character and fits comfortably within the cap.

In many (most? all?) programming languages, code tends to be less ambiguous and to carry more information per character than straight prose.

I was about to comment on an answer that contains a code example. It is a good answer and a good example but times move on and the syntax has evolved so that slight modification of the code provides a cleaner solution. Gave up on that due to the lack of code formatting in comments.