Line number

From Wikipedia, the free encyclopedia

In computing, a line number is a way of specifying a point in a file by enumerating each line in the file by a number.

Line numbers were a required element of syntax in older programming languages such as BASIC. The primary reason for this is that computers at the time lacked interactive text editors; since the programmer's interface was usually limited to a line editor, line numbers provided a mechanism by which earlier lines in the program could be referenced for editing, and by which the programmer could insert a new line at a specific point in the program.

Largely due to the prevalence of interactive text editing in modern computers, line numbers are no longer a feature of programming languages.

[edit] Line numbers and style

It was a matter of programming style, if not outright necessity, in these languages to leave gaps between successive line numbers—i.e., a programmer would use the sequence (10, 20, 30, ...) rather than (1, 2, 3, ...). This permitted the programmer to insert a line of code at a later time. For example, if a line of code between lines 20 and 30 was left out, the programmer might insert the forgotten line at line number 25. If no gaps were left in the numbering, the programmer would be required to renumber line 3 and all subsequent lines in order to insert the new line after line 2. Of course, if the programmer needed to insert more than nine additional lines, renumbering would be required even with the sparser numbering.

In a large program containing subroutines, each subroutine would usually start at a line number sufficiently large to leave room for expansion of the main program (and previous subroutines). For example, subroutines might begin at lines 10000, 20000, 30000, etc.

[edit] Line numbers and GOTOs

In "unstructured" programming languages such as BASIC and Fortran, line numbers were used to specify the targets of branching statements. For example:

10 IF X = 42 GOTO 40
20 X = X + 1
30 GOTO 10
40 PRINT "X is finally 42!"

GOTO-style branching is now widely considered by programmers to be poor programming style, as it tends to lead to the development of spaghetti code. (See Considered harmful, Structured programming.) Even in some later versions of BASIC that still mandated line numbers, the use of line number-controlled GOTOs was phased out whenever possible in favor of cleaner constructs such as the for loop and while loop.

Many modern languages (including C and C++) include a version of the GOTO statement; however, in these languages the target of a GOTO is specified by a textual label instead of a line number, and the use of GOTO is strongly discouraged by most programming style guides. The typically accepted use being when it is used to escape out of deep looping. For example:

while(1) {
  while (1) {
    if (done) {
      goto freedom;
    }
  }
}
:freedom

This is much cleaner than the alternative that follows, especially when you consider that you need to have a much more widely scoped `done` variable.

while(1) {
  if (done) {
    break;
  }
  while (1) {
    if (done) {
      break;
    }
  }
}

[edit] Line numbers and syntax errors

If a programmer introduces a syntax error into a program, the compiler (or interpreter) will inform the programmer that the attempt to compile (or execute) failed at the given line number. This simplifies the job of finding the error immensely for the programmer.

The use of line numbers to describe the location of errors remains standard in modern programming tools, even though line numbers are never required to be manually specified. It is a simple matter for a program to count the newlines in a source file and display an automatically generated line number as the location of the error. In IDEs such as Microsoft Visual Studio, in which the compiler is usually integrated with the text editor, the programmer can even double-click on an error and be taken directly to the line containing that error.

In other languages