Why are there no line numbers in the code listings?

by mrt181   Last Updated May 01, 2016 08:10 AM - source

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



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.

Kyle Cronin
Kyle Cronin
July 19, 2009 18:18 PM

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.

Paolo Bergantino
Paolo Bergantino
July 19, 2009 18:18 PM

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.

jjnguy
jjnguy
July 19, 2009 18:24 PM

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.

Jon Skeet
Jon Skeet
July 19, 2009 18:31 PM

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.

John Saunders
John Saunders
July 19, 2009 20:20 PM

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.

Vaccano
Vaccano
December 23, 2009 21:09 PM

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.

Chris S
Chris S
December 23, 2009 21:56 PM

SyntaxHighlighter is a Javascript based solution

Syntax Highlighter screenshot

Edit:Added some PNG love.

abel
abel
October 08, 2010 14:39 PM

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.

Karl Bielefeldt
Karl Bielefeldt
February 03, 2011 23:04 PM

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... -->
chown
chown
October 30, 2011 18:43 PM

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.

Keith Thompson
Keith Thompson
February 21, 2014 20:18 PM

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)

Wolf
Wolf
March 07, 2014 08:49 AM

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

Dávid Horváth
Dávid Horváth
April 30, 2016 15:42 PM

Related Questions


Add a way to highlight/emphasize changes in code

Updated August 11, 2017 18:24 PM