Because it usually is. Most people have learned it that way. What would be the point in having to learn both ways?
Having the characters on a grid makes it less likely that two characters will be interpreted as a single character. Absolute legibility is better with monospaced fonts. Programming involves a lot of characters adjacent to one another that are normally not in prose.
You could turn the question around: why anyone would ever use proportional fonts for anything other than prose? The only real advantage that a proportional font has is that you can squeeze more text into a particular space and as a result you can read faster. The increase in reading speed comes from the sort of context that you normally don't have for programming. The increase in the amount of text is not reliable (it depends on the number of thin characters) and as a result is not helpful for programming.
I appreciate you sharing your thoughts on this. I would gently note that you addressed a question other than the one I posed:
> Why should code have to be in a monospace font? [emphasis added]
The short answer is that it doesn't. Many programmers prefer proportional fonts, just as the vast majority prefer monospaced.
Similarly, many programmers prefer light themes. Being somewhat older than most programmers, I find dark themes very hard to work with. There is a scientific and practical reason for this: A light theme causes the irises in my eyes to "stop down" so there is less blurring of the text. This is similar to the way a camera lens may give a sharper result when stopped down a bit instead of wide open.
> The only real advantage that a proportional font has is that you can squeeze more text into a particular space and as a result you can read faster.
Interesting that you mention this, as it relates to one of the reasons I like proportional fonts. It's not to squeeze in more text, it's so I can use a larger font. At my age, this is helpful! And I find proportional fonts more beautiful and readable.
Anyway, let's respect each other's preferences instead of trying to establish what is best for everyone. You like monospaced fonts, I like proportional, and it's great that we have the choice!
> The only real advantage that a proportional font has is that you can squeeze more text into a particular space and as a result you can read faster
I would disagree with that - I find it easier, more comfortable rather than just faster to read any text in a proportional font because the spacing of characters is, well, proportional to what the form of each letter requires. I think it makes it easier to recognise words. It's true that code has a lot of unusual combinations of characters but still it is mostly ordinary words. A proportional font that makes those unusual combinations of punctutation characters less clear is certainly bad, but there are proportional fonts that don't.
Because it usually is. Most people have learned it that way. What would be the point in having to learn both ways?
Having the characters on a grid makes it less likely that two characters will be interpreted as a single character. Absolute legibility is better with monospaced fonts. Programming involves a lot of characters adjacent to one another that are normally not in prose.
You could turn the question around: why anyone would ever use proportional fonts for anything other than prose? The only real advantage that a proportional font has is that you can squeeze more text into a particular space and as a result you can read faster. The increase in reading speed comes from the sort of context that you normally don't have for programming. The increase in the amount of text is not reliable (it depends on the number of thin characters) and as a result is not helpful for programming.