23

I want to write short code in a comment like:

function ngWrap($scope, fn) {
    return function() {
        var args = [].slice.call(arguments);
        if (!$scope.$$phase) {
            fn.apply(null, args);
        } else {
            return $scope.$apply(function() {
                fn.apply(null, args);
            });
        }
    };
}

But when I put the code in backticks, the newlines are removed when rendered. How can I insert code into a comment, like on this answer?

12
  • 21
    Don't. Comments are not supposed to be used for extensive code examples. Use answers instead. Or link to a gist.
    – Oded StaffMod
    Commented May 15, 2014 at 11:31
  • 35
    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.
    – Lundin
    Commented Jun 3, 2014 at 14:17
  • 5
    @Oded: But any answer must still be an answer, right? meta.stackoverflow.com/questions/300458/… Commented Jul 30, 2015 at 10:12
  • 12
    @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...
    – boardrider
    Commented Aug 11, 2015 at 12:19
  • 2
    @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). Commented Feb 22, 2019 at 7:38
  • Great readability question: 👏🏻!newlineLook at the [return] key on your keyboard. You will notice that it is larger than punctuation characters keys, and than any alphabetic character key. Do you guess why?
    – athena
    Commented Nov 13, 2020 at 12:18
  • 4
    @dan sorry I don't understand what is your question.
    – jcubic
    Commented Nov 13, 2020 at 15:13
  • 7
    @dan Because modern keyboards are modeled after typewriters, where the carriage return button had to physically move the carriage back to the left of the paper, and shift the paper up one line. It required more physical effort to do that than for a normal key, so they made the key larger to accommodate using multiple fingers to press it. Now, what does that have to do with being able to add new lines to comments? Commented Nov 13, 2020 at 18:11
  • The [return] key on any keyboard is much important than anyone except [space] on your keyboard because this key is fundamental as space to bring a minimal level of readeability of any form of text. [return] is not any form of advanced formatting. It is an elementary function to maintain readability of any text.
    – athena
    Commented Jul 3, 2022 at 16:30
  • 3
    @dan I don't understand what it has to do with my question.
    – jcubic
    Commented Jul 3, 2022 at 17:12
  • 1
    I haven't checked, but I believe this works on Codidact. There are still restrictions on what Markdown and/or HTML is supported in comments, but code blocks and blockquotes are supposed to work IIRC. Commented Dec 19, 2023 at 9:46
  • So you want to react to answer, using that many text with important formatting so that a comment is not suited? Then why don't you simply edit the answer, adding a title like Edited after this particular insight and you put your source there. Additionally, you can add a comment, explaining to the answer author what you have done and why and he or she can decide whether or not to leave your edit.
    – Dominique
    Commented Dec 19, 2023 at 10:35

4 Answers 4

13

Comments are meant to be ... comments. That is why there are a cap on maximum length and a minimal support for text formatting.

Pasting that code snippet in your answer seems to be okay.

7
  • 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.
    – jcubic
    Commented May 15, 2014 at 12:03
  • 3
    What should I do if I need to paste multiple line of code in comment box as a question in any discussion. Commented Jul 30, 2015 at 8:24
  • 2
    @PlanetHackers: Reconsider if you really need those linebreaks. That said, that much code isn't suitable for the comments section.
    – Cerbrus
    Commented Jul 30, 2015 at 8:49
  • 2
    @Cerbrus, languages like Python insist on a certain format for the lines, which the comments clobber.
    – boardrider
    Commented Aug 11, 2015 at 12:22
  • 1
    Blocks of pythong code really aren't suitable for comments though
    – Cerbrus
    Commented Aug 11, 2015 at 12:27
  • 3
    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...
    – ekolis
    Commented Aug 19, 2016 at 0:10
  • 1
    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. Commented Sep 2, 2018 at 11:58
4

You can't and there is no good reason you should 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 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 characters long and fits comfortably within the cap.

In many (most? all?) programming languages, code tends to be less ambiguous and 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.

1
  • 2
    What do you think of this comment style: [newline] making the need for a newline fully explicit?
    – athena
    Commented Nov 13, 2020 at 11:54
-7

Newlines are not preserved in comments because Stack Exchange says so.

If you want to add to an existing answer, e.g. with some lines of code or a list of steps to follow, your only options are to edit it into the existing answer or post a new answer and refer to it in a comment.

Even though editing the question or answer or posting a new answer is often less helpful and appropriate than just commenting on an existing answer, these are the only work-arounds if you need multiple lines.

2
  • 4
    You have quite some reputation, but you use this as Forum. StackOverflow is not a forum it's a Q&A website even Meta.
    – jcubic
    Commented Jun 25, 2022 at 14:35
  • 1
    Stack Overflow is not a forum. Commented Jun 25, 2022 at 20:57
-18

If, for Question / Answers Select code in edit mode and press Ctrl + K. If required, for keys enclose in an XML kbd starting and /kbd ending tag and enclose in an XML snippet starting and /snippet ending tag for code:

System.out.println("Hello, World!")

But for comments the same cannot be done.

2
  • 12
    This misunderstands the question completely. It is about code in comments, not questions or answers. Commented Dec 19, 2023 at 9:40
  • I agree @Karl, it went misleading cause the question was about comments not just post. probably, it might help someone just posting Question / Answers, so I have edited the same as suggested by you. Thanks for helping me fix the same Karl.. :) Commented Dec 25, 2023 at 10:54

You must log in to answer this question.

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