168

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

9
  • 33
    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
    Commented Oct 2, 2010 at 10:31
  • 11
    This request may be more relevant for the new codereview.SE site.
    – BenV
    Commented Jan 19, 2011 at 21:55
  • How do you request it ?
    – Nick
    Commented Jun 21, 2011 at 13:49
  • 1
    @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
    Commented Jul 4, 2011 at 13:55
  • 3
    10 PRINT "Send me teh codez " 20 GOTO 10 - your error is on line 20, much easier!
    – Widor
    Commented Nov 4, 2011 at 10:50
  • 1
    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
    Commented Jan 21, 2014 at 7:49
  • @Rancs fixed that
    – mrt181
    Commented Jan 21, 2014 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
    – Himanshu
    Commented Jan 21, 2014 at 12:00
  • 2
    I found a userscript :)
    – Wolf
    Commented Mar 7, 2014 at 8:55

14 Answers 14

110

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.

9
  • 20
    Very. Good. Point. Specialized markers would win the day...
    – sehe
    Commented Jul 4, 2011 at 13:57
  • 3
    Not a good argument, I saw edits where I first had to count to detect that it was invalidating the line number reference.
    – Wolf
    Commented Mar 7, 2014 at 8:01
  • 17
    We'd "just" need a tag for linenumbers, so prefixing lines properly renumbers all references. BASIC gained that ability somewhere in the previous century. Commented Apr 15, 2014 at 13:26
  • 3
    @Karl, The problem you state can be fixed by referential line numbers (aka markers).
    – Pacerier
    Commented 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
    Commented Oct 6, 2015 at 21:01
  • 2
    Any edit can invalidate the existing responses. Commented 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. Commented May 29, 2016 at 1:29
  • +1 Epic answer! Very good point!
    – clickbait
    Commented 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
    Commented Aug 22, 2020 at 12:26
40

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.

8
  • 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! Commented 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 cost
    – sehe
    Commented 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? Commented 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. Commented 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. Commented Jun 27, 2013 at 13:19
  • @PaoloBergantino yes but the facts is they do and they describe the errors with line numbers. Commented Jun 27, 2013 at 13:21
  • @izomorphius: So instead of correcting the behavior you'd rather enable them? Commented 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
    Commented Nov 15, 2013 at 2:30
27

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.

3
  • 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
    Commented 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
    Commented Jul 19, 2009 at 20:05
  • 2
    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
    Commented Jul 20, 2009 at 16:39
19

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... -->
3
  • Would this depend on support for it being added to google prettify?
    – agf
    Commented Nov 4, 2011 at 11:55
  • This would better be an option for the reader to activate.
    – Wolf
    Commented Mar 7, 2014 at 9:08
  • 2
    + optional parameter which line to start nubering from, as this will enbale pasting short snippets of relevant code with line numbers preserverd.
    – Erbureth
    Commented Mar 28, 2014 at 8:07
15

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.

2
  • 7
    Someone who presents a question so poorly would earn downvotes Commented Jun 22, 2012 at 12:34
  • @Kyle, There are many cases where line numbers would be appropriate (especially on CodeReview.SE). See sivers.org/punish
    – Pacerier
    Commented Jun 17, 2014 at 16:21
9

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:

enter image description here

8

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.

10
  • 6
    provide URLs with examples of this "needs lots of code" Commented Dec 23, 2009 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
    Commented Dec 27, 2009 at 23:12
  • 1
    Here is another one from today: stackoverflow.com/questions/1972128/…
    – Vaccano
    Commented Dec 29, 2009 at 0:18
  • 1
    Here is another one from just a few minutes ago: stackoverflow.com/questions/1976669/…
    – Vaccano
    Commented Dec 29, 2009 at 20:58
  • 1
    Here is another one where the answer was asking for more details about the code: stackoverflow.com/questions/2119586/…
    – Vaccano
    Commented Jan 22, 2010 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
    Commented Jan 28, 2010 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.
    – Pekka
    Commented 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-question
    – Vaccano
    Commented 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. Commented Feb 3, 2011 at 23:26
  • 1
    I can only imagine how narrow Jeff's eyes were when he posted that first comment -_-
    – bobobobo
    Commented Jun 24, 2012 at 1:41
8

SyntaxHighlighter is a Javascript based solution

Syntax Highlighter screenshot

Edit:Added some PNG love.

2
  • 2
    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
    Commented Oct 8, 2010 at 15:10
  • @Yi Jiang nice review
    – abel
    Commented Oct 8, 2010 at 15:36
6

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)

2
  • Great recommendation! I'm not biased at all... :)
    – MDEV
    Commented Mar 8, 2014 at 15:58
  • // @match *://stackoverflow.com/questions/* Commented Jun 23, 2019 at 4:21
3

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.

3

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.

3

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.

1
  • Just allow starting from an offset. Line numbers should be optional but allowed... Commented Feb 21, 2022 at 23:04
2

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.

0

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.

1
  • 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? Commented Feb 23, 2018 at 16:01

You must log in to answer this question.

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