Would it make discussing code easier if you could simply refer to a particular line or range, or am I wrong?
14 Answers
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.
-
21Very. Good. Point. Specialized markers would win the day...sehe– sehe2011-07-04 13:57:55 +00:00Commented Jul 4, 2011 at 13:57
-
3Not a good argument, I saw edits where I first had to count to detect that it was invalidating the line number reference.Wolf– Wolf2014-03-07 08:01:25 +00:00Commented Mar 7, 2014 at 8:01
-
18We'd "just" need a tag for linenumbers, so prefixing lines properly renumbers all references. BASIC gained that ability somewhere in the previous century.MSalters - reinstate Monica– MSalters - reinstate Monica2014-04-15 13:26:16 +00:00Commented Apr 15, 2014 at 13:26
-
3@Karl, The problem you state can be fixed by referential line numbers (aka markers).Pacerier– Pacerier2014-06-17 16:24:20 +00:00Commented Jun 17, 2014 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– jpmc262015-10-06 21:01:25 +00:00Commented Oct 6, 2015 at 21:01
-
2Any edit can invalidate the existing responses.Dávid Horváth– Dávid Horváth2016-04-30 15:07:14 +00:00Commented Apr 30, 2016 at 15:07
-
2+1: I came here to downvote whatever reason was given for not having line numbers, then read it and found it makes perfect sense.trichoplax is on Codidact now– trichoplax is on Codidact now2016-05-29 01:29:50 +00:00Commented May 29, 2016 at 1:29
-
+1 Epic answer! Very good point!clickbait– clickbait2018-06-27 04:20:17 +00:00Commented Jun 27, 2018 at 4:20
-
As @DávidHorváth pointed out, any edit can invalidate any answer, creating the same "chaos". So it's hardly an argument against what would be a very handy feature.John– John2020-08-22 12:26:09 +00:00Commented Aug 22, 2020 at 12:26
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.
-
1I 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– Hooray Im Helping2011-06-10 02:26:12 +00:00Commented Jun 10, 2011 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 costsehe– sehe2011-07-04 13:57:17 +00:00Commented Jul 4, 2011 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– knownasilya2012-12-05 20:57:31 +00:00Commented Dec 5, 2012 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.Ivaylo Strandjev– Ivaylo Strandjev2013-06-27 10:47:40 +00:00Commented Jun 27, 2013 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– Paolo Bergantino2013-06-27 13:19:55 +00:00Commented Jun 27, 2013 at 13:19
-
@PaoloBergantino yes but the facts is they do and they describe the errors with line numbers.Ivaylo Strandjev– Ivaylo Strandjev2013-06-27 13:21:36 +00:00Commented Jun 27, 2013 at 13:21
-
@izomorphius: So instead of correcting the behavior you'd rather enable them?Paolo Bergantino– Paolo Bergantino2013-06-27 13:23:04 +00:00Commented Jun 27, 2013 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– j_kubik2013-11-15 02:30:54 +00:00Commented Nov 15, 2013 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.
-
4I'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– Jonik2009-07-19 19:04:28 +00:00Commented Jul 19, 2009 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– Jon Skeet2009-07-19 20:05:02 +00:00Commented Jul 19, 2009 at 20:05
-
2That'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– Jonik2009-07-20 16:39:59 +00:00Commented Jul 20, 2009 at 16:39
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... -->
-
Would this depend on support for it being added to google prettify?agf– agf2011-11-04 11:55:39 +00:00Commented Nov 4, 2011 at 11:55
-
This would better be an option for the reader to activate.Wolf– Wolf2014-03-07 09:08:07 +00:00Commented Mar 7, 2014 at 9:08
-
4+ optional parameter which line to start nubering from, as this will enbale pasting short snippets of relevant code with line numbers preserverd.Erbureth– Erbureth2014-03-28 08:07:00 +00:00Commented Mar 28, 2014 at 8:07
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.
-
7Someone who presents a question so poorly would earn downvotesChris Stratton– Chris Stratton2012-06-22 12:34:38 +00:00Commented Jun 22, 2012 at 12:34
-
1@Kyle, There are many cases where line numbers would be appropriate (especially on CodeReview.SE). See sivers.org/punishPacerier– Pacerier2014-06-17 16:21:01 +00:00Commented Jun 17, 2014 at 16:21
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 -->
@Override
public int add(int num1, int num2) {
<!-- break:"here" color:yellow -->
return num1 + num2;
}
And the expected result:
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.
-
6provide URLs with examples of this "needs lots of code"2009-12-23 21:34:35 +00:00Commented Dec 23, 2009 at 21:34
-
1As 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/listVaccano– Vaccano2009-12-27 23:12:45 +00:00Commented Dec 27, 2009 at 23:12
-
1Here is another one from today: stackoverflow.com/questions/1972128/…Vaccano– Vaccano2009-12-29 00:18:40 +00:00Commented Dec 29, 2009 at 0:18
-
1Here is another one from just a few minutes ago: stackoverflow.com/questions/1976669/…Vaccano– Vaccano2009-12-29 20:58:07 +00:00Commented Dec 29, 2009 at 20:58
-
1Here is another one where the answer was asking for more details about the code: stackoverflow.com/questions/2119586/…Vaccano– Vaccano2010-01-22 21:59:14 +00:00Commented Jan 22, 2010 at 21:59
-
1Here 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– Vaccano2010-01-28 03:58:29 +00:00Commented Jan 28, 2010 at 3:58
-
3I definitely second this. There sometimes is the need for > 20 lines of code. Line numbers as an option would be great.Pekka– Pekka2010-04-01 18:48:51 +00:00Commented Apr 1, 2010 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-questionVaccano– Vaccano2010-04-13 23:47:57 +00:00Commented Apr 13, 2010 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– Karl Bielefeldt2011-02-03 23:26:10 +00:00Commented Feb 3, 2011 at 23:26
-
1I can only imagine how narrow Jeff's eyes were when he posted that first comment -_-bobobobo– bobobobo2012-06-24 01:41:46 +00:00Commented Jun 24, 2012 at 1:41
-
2
SyntaxHighlighter.jsrequires 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– Yi Jiang2010-10-08 15:10:29 +00:00Commented Oct 8, 2010 at 15:10 -
I found a helpful userscript: SO - Code line nums
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)
-
1Great recommendation! I'm not biased at all... :)MDEV– MDEV2014-03-08 15:58:30 +00:00Commented Mar 8, 2014 at 15:58
-
// @match *://stackoverflow.com/questions/*Smart Manoj– Smart Manoj2019-06-23 04:21:16 +00:00Commented Jun 23, 2019 at 4:21
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.
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.
-
1Just allow starting from an offset. Line numbers should be optional but allowed...Thomas Guyot-Sionnest– Thomas Guyot-Sionnest2022-02-21 23:04:55 +00:00Commented Feb 21, 2022 at 23:04
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.
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.
Because no-one seems to have written a smart enough numbering/breakpoint system :-)
I mean come on, people use GreaseMonkey to enable a basic functionality of any text editor and I caught myself just today counting lines. Not for the first time.
Obviously, BASIC was a bad idea, but a "simple" option to tag a single line of text (preferably with the current line number as default name) would not only clarify even small snippets a lot, but also allow for automatic adjustment and post-edit-correction.
Actually, I would love to spent some extry brain-Watts on looking out for these kind of references when reviewing a queue, because I knew, it could save others a lot of possible confusion.
And it would be language independent, not like any jsfiddle/RUN approach.
-
Let's say I would be interesting in implementing something like that. Apart from GM, how would one go about and suggest/provide such a feature?Xan-Kun Clark-Davis– Xan-Kun Clark-Davis2018-02-23 16:01:35 +00:00Commented Feb 23, 2018 at 16:01


10 PRINT "Send me teh codez " 20 GOTO 10- your error is on line20, much easier!