Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 155 Stack Exchange communities.

What is meta?
Here's how it works:
  1. Any Stack Exchange user can ask a question
  2. The community provides support, votes on ideas, and reports bugs
  3. Your voice helps shape the way Stack Exchange operates

Would it make discussing code easier if you could simply refer to a particular line or range, or am I wrong?

share|improve this question
Oh, the good old time of GW-Basic. ;-) – Wim ten Brink Dec 23 '09 at 22:09
I second this request. Line numbers if implemented properly should not make copying code difficult as the other comments seem to suggest. I am sure you have seen plenty of other sites with copy-safe line numbers. google search for "code show line numbers blog" gives… which is pretty copy-safe – abel Oct 2 '10 at 10:31
This request may be more relevant for the new codereview.SE site. – BenV Jan 19 '11 at 21:55
How do you request it ? – Nick Jun 21 '11 at 13:49
@BenV: disagree. SO questions require us to link a compiler message or exception trace to a specific line in the code about half the time. This sucks since the line numbers never match. Giving self-numbering code blocks will aid the asker in explaining what line is involved – sehe Jul 4 '11 at 13:55
10 PRINT "Send me teh codez " 20 GOTO 10 - your error is on line 20, much easier! – Widor Nov 4 '11 at 10:50
Somebody asks a question about a feature he needs. Then another one tells he thinks it is unnecessary. Then the question owner marks this answer as the accepted answer. This is the most interesting question/response I ever see on StackOverflow. I think that this is a disrespect against the people who marked this question as useful/favorite. – Rancs Jan 21 '14 at 7:49
@Rancs fixed that – mrt181 Jan 21 '14 at 11:25
@Rancs - I don't think accepting an answer is a disrespect against the people who marked this as useful/favorite. It simply mean that the OP has accepted that this feature is not useful/viable. Those people can downvote the answer if they disagree. E.g. see this answer – hims056 Jan 21 '14 at 12:00
@mrt181 Thanks. – Rancs Jan 21 '14 at 12:03
I found a userscript :) – Wolf Mar 7 '14 at 8:55

13 Answers 13

While this might be handy to provide a description of what a small block of code is doing line by line, I fear that it might enable people to paste huge blocks of code and say things like "I'm getting a segfault on line 213" and expect other users to dig through the code to identify it. In other words most of the time you think you need line numbers you're probably including too much code.

share|improve this answer
Someone who presents a question so poorly would earn downvotes – Chris Stratton Jun 22 '12 at 12:34
@Kyle, There are many cases where line numbers would be appropriate (especially on CodeReview.SE). See – Pacerier Jun 17 '14 at 16:21

There's a reason why this hasn't been wildly requested as of yet. Most code that is going to be posted on the site is going to be a short snippet where the relevant parts are the only ones going to be posted. Anything that gets a little longer will usually have comments in relevant parts explaining what's going on. We're programmers, I think that suffices. Allowing lines might encourage bigger code blocks, and that's not a behavior I think we want to encourage.

That being said, I'm not really super against this, just don't really think it's needed.

share|improve this answer
I agree that most code snippets are short, but even then, they can benefit from line numbers. My comment on this answer could have benefited from a line number. Also, go Noles! – Hooray Im Helping Jun 10 '11 at 2:26
Good points. However, line numbering is a lot easier to implement than a more targeted 'mark-this-line' feature. It's about tradeoffs here. Linenumbering gives us most functionality at least cost – sehe Jul 4 '11 at 13:57
What about not enabling line numbers on the code blocks, but having an option in profiles ettings to see the line numbers, or allowing the option to appear past a certain reputation? – knownasilya Dec 5 '12 at 20:57
I have seen quite a few questions saying: I get compilation error on line <line>. Why is that?. For such questions the line numbers would be really helpful. – izomorphius Jun 27 '13 at 10:47
@izomorphius: The person should not be pasting the entirety of the script like ever, so the line number SHOULDN'T be helpful. That's exactly why we wouldn't want line numbers, so people don't plop their 100 page PHP script and ask us to look at line 59. – Paolo Bergantino Jun 27 '13 at 13:19
@PaoloBergantino yes but the facts is they do and they describe the errors with line numbers. – izomorphius Jun 27 '13 at 13:21
@izomorphius: So instead of correcting the behavior you'd rather enable them? – Paolo Bergantino Jun 27 '13 at 13:23
Referring to the code from post's content could be replaced with smart use of comments in the code, but what about comments under a post? How do I ask further question (or give an explanation) about line X then? – j_kubik Nov 15 '13 at 2:30

The real reason why there are no line numbers is because it hasn't been written yet.

Unless it is a feature that the developers consider fairly important, they don't get implemented.

So, since the code posted here is usually short snippets, line numbers aren't very important.

share|improve this answer

I usually refer to specific lines with an inline comment:

int x = 10;

// v is still statically typed as an int
var v = 10;

That usually works fine as far as I'm concerned.

Don't get me wrong - there are some cases where line numbers would be useful - but they're rare enough that I think there are higher priorities. In particular, IIRC the syntax highlighter is Prettify rather than a home-rolled one - that doesn't stop Jeff and the team from including line numbers, but it means that until it got put back into the main tree they'd have to maintain their own branch, which may not be a lot of fun.

share|improve this answer
I'd think this would be (more) useful when someone else refers to specific lines of code, in a comment on a question or an answer. – Jonik Jul 19 '09 at 19:04
@Jonik: That's true - but I think the code would usually be easier to read without the line numbers. Ideally it should be toggle-able while reading the post, I guess. – Jon Skeet Jul 19 '09 at 20:05
That's also true. Toggleable line numbers would solve that, but also add unnecessary complexity... Maybe they should just be styled to be as unobtrusive as possible (small font, grey colour, etc). Anyway, as others said, we surely can continue to live without them too. – Jonik Jul 20 '09 at 16:39

I don't feel much need for line numbers in code snippets.

However, there are times, especially when dealing with certain web services questions, when I'd really like them to post the complete WSDL, all XML Schemas, actual XML being sent and received, and maybe even all the code necessary to reproduce the problem.

I'd rather they didn't paste that inline (even if they do learn to use the "code sample" button). For that, there's a format I like, though it should add the ability to collapse a block of code to a one-line description. See for an example.

share|improve this answer

I think line numbers in code would be useful. More and more often my questions get the response of something to the effect of "show us more code so we have more context".

It is hard to keep the discussion going when there is more than 15 lines of code.

share|improve this answer
provide URLs with examples of this "needs lots of code" – Jeff Atwood Dec 23 '09 at 21:34
As I said, it is "to the effect" of show us more code. Some answer's need more code. I don't usually like posting more, but sometimes people get caught up in the "why do you need to do that the way you say you need to" attitude. This usually neccesiates more code to explain why. While not a perfect example (I did not feel like digging through all my questions on Christmas weekend) here is an example that needed me to add code because people where questioning why I was doing something the way I was. – Vaccano Dec 27 '09 at 23:12
Here is another one from today:… – Vaccano Dec 29 '09 at 0:18
Here is another one from just a few minutes ago:… – Vaccano Dec 29 '09 at 20:58
Here is another one where the answer was asking for more details about the code:… – Vaccano Jan 22 '10 at 21:59
Here is another where only showing ~15 lines of code caused mis-understanding. If it were easier to add more lines it would have gotten a lot better answers:… – Vaccano Jan 28 '10 at 3:58
I definitely second this. There sometimes is the need for > 20 lines of code. Line numbers as an option would be great. – Pëkka Apr 1 '10 at 18:48
@Jeff, Here is another one. I had forgotten about this tally I was keeping till I got this more explicit one: – Vaccano Apr 13 '10 at 23:47
@Jeff, I'm more interested in examples of "hard to keep the discussion going." I had the same thought at first, and tried to look for situations where the lack of line numbers led to awkward phrasing in answers, but couldn't find any. To the contrary, a lack of line numbers seems to increase the tendency to quote the relevant lines, which increases clarity by showing instead of telling. I think people like the idea of line numbers because they help a lot when someone is sitting next to you talking aloud about your code, but that doesn't transfer to written communication. – Karl Bielefeldt Feb 3 '11 at 23:26
I can only imagine how narrow Jeff's eyes were when he posted that first comment -_- – bobobobo Jun 24 '12 at 1:41

I'm not sure about line numbers, but being able to copy to the clipboard would be very useful, or opening in a new window that has a textarea.

share|improve this answer

SyntaxHighlighter is a Javascript based solution

Syntax Highlighter screenshot

Edit:Added some PNG love.

share|improve this answer
SyntaxHighlighter.js requires you to specify the language - it simply won't work without a brush explicitly specified. It doesn't update live as you type (check that out when writing an answer next time - Prettify kicks in if you stop typing for about a second or so). It's also a lot more heavy, and would require some heavy lifting to integrate into the existing system. So all in all, no. – Yi Jiang Oct 8 '10 at 15:10
@Yi Jiang nice review – abel Oct 8 '10 at 15:36

One very important drawback that hasn't been mentioned is that editing a post would potentially invalidate all the existing answers and comments referring to a specific line number in a previous revision. Code blocks are probably edited more than any other part of a question because people often don't post enough code to answer the question at first. I think the initial convenience of line numbers would too often be outweighed by the confusion of not knowing how much you can trust references to them.

share|improve this answer
Very. Good. Point. Specialized markers would win the day... – sehe Jul 4 '11 at 13:57
Not a good argument, I saw edits where I first had to count to detect that it was invalidating the line number reference. – Wolf Mar 7 '14 at 8:01
We'd "just" need a tag for linenumbers, so prefixing lines properly renumbers all references. BASIC gained that ability somewhere in the previous century. – MSalters Apr 15 '14 at 13:26
@Karl, The problem you state can be fixed by referential line numbers (aka markers). – Pacerier Jun 17 '14 at 16:24
@Wolf You should reject those edits (or maybe approve and improve in some cases) and rephrase it so that it doesn't depend on line numbers. Don't waste time counting. – jpmc26 Oct 6 '15 at 21:01
Any edit can invalidate the existing responses. – Dávid Horváth 2 days ago

In my opinion, this is a great idea (if it is implemented as optional). Maybe add to the current formatting directives; something like:

 <!-- language:lang-c ln:true -->

 or as a new indicator:
 <!-- language:lang-c -->
 <!-- ln:true/yes/false/no/etc... -->
share|improve this answer
Would this depend on support for it being added to google prettify? – agf Nov 4 '11 at 11:55
This would better be an option for the reader to activate. – Wolf Mar 7 '14 at 9:08
+ optional parameter which line to start nubering from, as this will enbale pasting short snippets of relevant code with line numbers preserverd. – Erbureth Mar 28 '14 at 8:07

Another drawback of automatic line numbers is that they won't necessarily match the actual line numbers in the source code.

It's usually best to post an entire source file (pared down to what's necessary to reproduce the problem), but sometimes it makes sense to post just the relevant portion of a larger source file. A line number in an error message might refer to line 123 in the original source, which might be labelled as line 7 in the posted snippet.

There are ways to avoid such problems, but they impose extra work on people posting questions, and that's not going to happen.

share|improve this answer

I found a helpful userscript: StackOverflow line numbers for Greasemonkey

The script toggles line numbers of code boxes when you click on the #L symbol in its top right corner. (It refers to, so script blocking has probably to be adjusted)

share|improve this answer
Great recommendation! I'm not biased at all... :) – SmokeyPHP Mar 8 '14 at 15:58

If line numbering is really so problematic (IMO not), what about debugger-like break points? Not a complete solution, but at least very "visual".

<!-- language: lang-java -->

<!-- break color:red -->
    public int add(int num1, int num2) {
<!-- break:"here" color:yellow -->
        return num1 + num2;

And the expected result:

enter image description here

share|improve this answer

You must log in to answer this question.

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