What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 134 Stack Exchange communities.

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
6  
Oh, the good old time of GW-Basic. ;-) –  Wim ten Brink Dec 23 '09 at 22:09
18  
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 akomaenablog.blogspot.com/2009/05/… which is pretty copy-safe –  abel Oct 2 '10 at 10:31
8  
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
3  
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 at 7:49
    
@Rancs fixed that –  mrt181 Jan 21 at 11:25
1  
@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 at 12:00
    
@mrt181 Thanks. –  Rancs Jan 21 at 12:03
1  
I found a userscript :) –  Wolf Mar 7 at 8:55

12 Answers 12

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
11  
Very. Good. Point. Specialized markers would win the day... –  sehe Jul 4 '11 at 13:57
2  
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 at 8:01
3  
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 at 13:26
1  
@Karl, The problem you state can be fixed by referential line numbers (aka markers). –  Pacerier Jun 17 at 16:24

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
1  
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

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
4  
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
5  
@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
1  
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

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
5  
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 sivers.org/punish –  Pacerier Jun 17 at 16:21

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 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 at 8:07

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
6  
provide URLs with examples of this "needs lots of code" –  Jeff Atwood Dec 23 '09 at 21:34
1  
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. stackoverflow.com/revisions/1868414/list –  Vaccano Dec 27 '09 at 23:12
1  
Here is another one from today: stackoverflow.com/questions/1972128/… –  Vaccano Dec 29 '09 at 0:18
1  
Here is another one from just a few minutes ago: stackoverflow.com/questions/1976669/… –  Vaccano Dec 29 '09 at 20:58
1  
Here is another one where the answer was asking for more details about the code: stackoverflow.com/questions/2119586/… –  Vaccano Jan 22 '10 at 21:59
1  
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: stackoverflow.com/questions/2151361/… –  Vaccano Jan 28 '10 at 3:58
3  
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
1  
@Jeff, Here is another one. I had forgotten about this tally I was keeping till I got this more explicit one: stackoverflow.com/questions/2633853/c-constructor-question –  Vaccano Apr 13 '10 at 23:47
2  
@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

SyntaxHighlighter is a Javascript based solution

Syntax Highlighter screenshot

Edit:Added some PNG love.

share|improve this answer
1  
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

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'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

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 mdev.me, so script blocking has probably to be adjusted)

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

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 http://forums.asp.net/t/1447256.aspx#3297127 for an example.

share|improve this answer

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

You must log in to answer this question.

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