4

This just struck me as an oversight by Microsoft. Since arrays and other data-structures within the .NET framework begin from zero (zero-based) why don't we have a line 0 within the code view in Visual Studio?

Ponder that one :-)

billy.bob
  • 6,549
  • 4
  • 29
  • 45
  • Visual Studio is older than .NET. By a lot. (True, it is written in another zer-based language, but my point is no .NET convention should be assumed to drive Visual Studio decisions.) – Kate Gregory Aug 18 '11 at 14:09
  • http://en.wikipedia.org/wiki/Natural_number – StuperUser Aug 18 '11 at 14:09
  • Because no one answering this question has a sense of humour? ;-) – Arjailer Aug 18 '11 at 14:53
  • @HappyCat so's your face got no sense of humour. Oh snap! – StuperUser Aug 18 '11 at 15:07
  • 2
    Every commercial product has a product manager / marketing director, whose job it is to maximize the friendliness and minimize the shock value to buyers, so they buy. Only programmers think zero is the first number. – Mike Dunlavey Aug 18 '11 at 16:10
  • 1
    @MikeDunlavey, but the point is, Visual Studio is made for programmers! – svick May 08 '12 at 00:47
  • @svick: I know, but programmers can be silly. 1) Know how the "mod" function in math should give you a number from 0 to N-1? Well, "%" says -1%3 is -1. This also affects the way "/" works. Why? Principle of minimum surprise. 2) On performance most (not all) programmers have a blind spot - they think you have to measure to find what to fix. Even if you show them why that's not so, they treat you like you're crazy. – Mike Dunlavey May 08 '12 at 01:23

6 Answers6

40

Because they did a good job at separating the user model from the programming model, that's why. The same reason why most apps don't ask you, for example,

Please enter a varchar(200) representing your name.

Take a lesson from that.

JohnFx
  • 19,052
  • 8
  • 65
  • 112
  • 2
    Great answer. A lot of programmers can benefit from thinking like a user again. – Techgration Aug 18 '11 at 21:40
  • I agree with this answer, but I don't think this answer is necessarily as *obvious* as JohnFx's tone would imply. In the case of Visual Studio (or any IDE), there's no difference between the "user model" and the "programming model", since all users are likely programmers. If I am writing software for some aliens who use a base-37 numerical system, my user model will necessarily display integers in base-37. So, if I'm writing an IDE for *programmers*, who are most likely used to thinking in zero-based indexing, isn't that also my user model?? – Charles Salvia Jul 17 '14 at 21:06
  • I strongly disagree. Just because the user is a programmer does not make zero based counting part of the user model. Programmers only use zero based systems because the compiler expects it. Now if you were writing software where a COMPUTER was the user I would agree. I've been a programmer all my life and I do not count real world objects starting with zero. – JohnFx Jul 18 '14 at 20:34
26
  1. All arrays and data-structures are indexed based on zero, not just in .NET.
  2. Array indexes are numbered for the computer, text lines in a document are numbered for us.
  3. See screenshot :)

enter image description here

Konrad Morawski
  • 9,721
  • 4
  • 37
  • 58
  • 11
    "All array and data-structures are indexed based on zero" -- this is not true, **all** arrays are not zero-indexed, though it's quite common these days. Many historical languages used mathematical-style 1-based indexing (Fortran and COBOL come to mind) but even the more modern Lua uses 1-based indexing. – Dean Harding Aug 18 '11 at 12:21
  • 1
    @Dean Harding I meant they are - internally. Numbering them from 1 is sort of syntax sugar. At least this is my understanding...? Anyhow, .NET standard is much more of a rule than an exception in this aspect. – Konrad Morawski Aug 18 '11 at 12:24
  • Worth noting than VS is not the only editor doing this and that it would be harsh to blame it for its behavior. Even Emacs numbers lines starting from 1 (and rightly so, as you pointed out). – haylem Aug 18 '11 at 13:36
  • Don't forget 1/2-based indexing – alternative Aug 18 '11 at 14:06
  • @Dean - Actually in fortran you can start the lower index at whatever you want a(0:19), a(-19,19) ... it is only by default that they start at one a(19) assumes the lower bound is 1. I do understand the point you were trying to make however. – Rook Aug 18 '11 at 14:28
  • 2
    Inception or recursion? – Kevin Aug 18 '11 at 15:25
21

There is a difference between counting and indexing. The index can start at any number (some languages support that), but for many reasons it is most often practical to have it start at zero. Counting also starts at zero, but as soon as one does count a set that is not empty, the first element is 1, and so on.

Ingo
  • 3,903
  • 18
  • 23
4

Because it is not natural to assume that the first line is the 0th line. Much like it's not natural either that arrays first element is at 0. However that's the first number that can be used in an unsigned integer to index an array with. So instead of wasting one element we use it.

Spoike
  • 14,765
  • 4
  • 43
  • 58
  • 4
    In the case of counting, you say how many elements you have. While in indexing, you merely tell the difference to the base index. – Ingo Aug 18 '11 at 13:12
3

By convention, normal humans count starting from 1, while many programmers and some mathematicians count starting from 0.

The question you have raised about line numbers is not specific to Microsoft Visual Studio. It applies to every other text editor tool, because counting lines from 1 has been adopted as a universal convention in software.

The countless other tools that count text lines from 1 include Windows Notepad, IDE text editors, text editors on Linux, Firefox's JavaScript console, compiler error messages, and so on and so forth.

While it wouldn't be wrong to count lines starting from 0, it would break the established convention and cause a massive amount of confusion to everyone involved, from the developers to the users.

See also:

Nayuki
  • 184
  • 2
  • 11
  • "massive amount of confusion"? Probably not... – Kenneth Aug 18 '11 at 14:11
  • I don't know any mathematicians (and I do know quite a few) that start counting from one. They deal with 0 when needed in where needed, but in real life they all count from one. – Rook Aug 18 '11 at 14:26
  • Except for numbering floors in European countries. For some reason that is zero based. – JohnFx Jan 09 '18 at 21:56
-2

A link which counters some of Dijkstra's points (in an article which is sooner or later gonna come out in this discussion).

Why numbering should start at one

Rook
  • 19,861
  • 9
  • 53
  • 96