66
\$\begingroup\$

I recall seeing a request for this in meta SO in the pass, but for various reasons that escape me at the moment, there were no plans to do that. Should this be reconsidered for code review considering there'll probably be more code snippets posted here?

Perhaps preferably have an option to toggle them on and off to make copy-paste easier.


Edit: I got quite a few responses so I'll just summarized it here. The general consensus agrees that some form of line numbering should exist to help with code readability. There are some good suggestions on exactly how it should work:

  • The starting line number should be settable by the user. @Hailwood's suggestion
  • Line numbering automatically kicks-in after x lines of code. @cbrulak's suggestion

The primary concern about this feature is that the line numbering can go out-of-sync or even reference non-existing code if the original poster heavily modifies his question or answer.

In spite of this concern, my personal feeling is that having line numbering in code snippets would make this site easier to use than if we were to not have it. As such, we should carefully consider possible solutions to the concern stated above rather than not implement it.

\$\endgroup\$
2
  • 11
    \$\begingroup\$ Line numbers would be amazing feature, +1 \$\endgroup\$
    – RobertPitt
    Commented Jan 19, 2011 at 23:57
  • 4
    \$\begingroup\$ +1 - I imagine that the code posted here will be much bigger and more complex than on SO, so line numbers will help with readability and when referencing stuff. Good idea. \$\endgroup\$
    – Andrew
    Commented Jan 20, 2011 at 5:38

9 Answers 9

36
\$\begingroup\$

I agree with this.

If we do have line numbering please ensure you can set at what line the numbers start at.

This is beneficial when talking about a snippet from a large amount of code. You can see directly where it relates to.


Edit: 22-01-2011
After reading through the issues people have pointed with line numbers I propose a system where line numbers are referenced in the original code block.

This would be achieved by each code block having a unique name in the page.

A line would be reference by

In line [block1|32]

With the possible output looking like

In line

32 | if($allowed = in_array($var, $array))

when a answer or comment is made which reference's a line in the code block a reference table is updated.

When the question is updated the backend checks for modifications and updates the rows in the reference table.

\$\endgroup\$
3
  • \$\begingroup\$ the suggestion sounds pretty good. I would upvote this again if I could :( \$\endgroup\$
    – greatwolf
    Commented Jan 22, 2011 at 22:09
  • 4
    \$\begingroup\$ how about [highlight:22] to highlight line 22 or even [highlight:22,28] for ranges \$\endgroup\$
    – RobertPitt
    Commented Jan 25, 2011 at 18:44
  • 1
    \$\begingroup\$ How would you handle edits? \$\endgroup\$
    – Dan
    Commented Aug 1, 2011 at 12:49
15
\$\begingroup\$

Anything greater than 5 lines should have line numbers automatically. It will be impossible to review code without them.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ +1 -- Well, it'd be possible. You could say something like "the 5th line of foo::bar", but I agree that they would be massively helpful :) \$\endgroup\$ Commented Jan 20, 2011 at 4:45
11
\$\begingroup\$

The problem with numbers is that stack exchange is meant to be a WIKI style site. If the code gets updated and there are answers referencing those (arbitrary) line numbers then we may lose the reference.

Using line numbers needs to be discouraged because of this problem.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ True, but... a solution might be to keep line numbers in sync, like the index which is automatically generated for links inserted in comments. If the code is modified, adding some lines between lines 3 and 7, the references to the lines of code following the change should be shifted accordingly. \$\endgroup\$
    – user185
    Commented Jan 22, 2011 at 9:05
  • 1
    \$\begingroup\$ I suppose thats possible but I think we may be making it more complicated than it needs to be. \$\endgroup\$ Commented Jan 22, 2011 at 15:42
11
\$\begingroup\$

I've created an userscript to do this:

Line numbering for code block

Line number for Code Review

Install - Source

It does the following:

  • Reduces font size slightly to accommodate large code blocks
  • Syntax highlighting for all code blocks (including inline blocks)
  • Adds line numbering (for code blocks longer than 4 lines) using Google Code Prettify already used in the pages here
  • Autoexpands wide code blocks on hover
  • Shows original source in a popup on double click (for copying purposes)
\$\endgroup\$
8
\$\begingroup\$

However, line numbers might create confusion if we want to comment the code inline. I'd love some nifty UI for inline comments...

\$\endgroup\$
1
  • 1
    \$\begingroup\$ agreed: the line numbers should be preserved in an anwser when you cut the block of code to insert comments in between. \$\endgroup\$
    – user185
    Commented Jan 22, 2011 at 9:02
4
\$\begingroup\$

The trouble with line numbers is that copy and pasting the code back to an editor then becomes a pain.

So if line numbers were introduced there should be some way to turn them off or not incorporate them into the clipboard when you copy the code.

\$\endgroup\$
2
  • \$\begingroup\$ that's odd, somehow I thought you or someone else posted this already way earlier than 2 hours ago. Anyway, completely agree with being able to turn them off. I mentioned making them toggle-able in my post. Also looking at refactormycode.com they incorporated line numbers in the code margin but you're able to select and copy the code without the line numbers. \$\endgroup\$
    – greatwolf
    Commented Jan 29, 2011 at 21:21
  • \$\begingroup\$ -1 One can make it so that the line numbers are not selected, and thus not copied: stackoverflow.com/questions/826782/… \$\endgroup\$
    – ANeves
    Commented Oct 26, 2011 at 11:31
3
\$\begingroup\$

For long code snippets (greater than 15 lines or so), line numbering may be helpful. However, sooner or later there will be a question that undergoes major editing after its initial posting, and that will break the line numbering. Referring to line numbers only also forces readers to scroll back up to the original question.

If we do adopt line numbering, answerers should still quote the entire line of code they are referring to (except when the line is very long, in which case a portion is acceptable). That way, it's at least possible to figure out which line it is and edit the answers accordingly (without resorting to history). Quoting the entire line also would make it possible, at a glance, to see what the answerer is referring to.

Here's an example:

Line 42 is a good example of what not to do. Incrementing a variable and calculating another quantity at the same time only leads to confusion.

/*42*/  total = 3.50 * ++qty + 2.25;

You shouldn't do output within this function (and others implementing business logic) either. Make the function build an object and return that to its caller. It will make maintenance and unit testing so much easier.

/*48*/  printf("Please pay this amount: $%.2f\n", total);
\$\endgroup\$
2
\$\begingroup\$

I've added a comment to the original proposal on meta.SO that line numbers would be a useful feature on this site.

\$\endgroup\$
1
  • \$\begingroup\$ awesome, let's hope it gets some attention :) \$\endgroup\$
    – greatwolf
    Commented Jan 19, 2011 at 23:24
2
\$\begingroup\$

Pie in the sky dreams , but with some sort of clickable "reference this line in this revision" feature, we might be able to do away with the visually printed line numbers as such -- sidestepping both the copy-paste issue, and the line numbers getting out of date issue. The generated links should probably show as a line number, but the reader is effectively expected to click it... bringing up the right rev, and highlighting the line(s) in question. Some mechanism for making a link to a range of lines, and/or a small section of text within a line, might be an added stretch for this feature.

\$\endgroup\$

You must log in to answer this question.

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