Hacker News new | ask | show | jobs
by aninhumer 3283 days ago
Why does code need to be monospaced? I feel like the only reason we all do it is that it's always been done like that so it feels wrong not to.

The only actual reason I can think of for it is alignment, and that seems like a minimal benefit really. Variable width text is generally considered easier to read, so why couldn't this also apply to code?

3 comments

Alignment isn't a minimal benefit; it is super important.

It's not just for neatness; either. Alignment shows where differences are between similar lines. And not necessarily consecutive lines. For instance, if we rapidly flip the editing window back and forth between two buffers showing similar code, we can see the differences as the moving parts in a two-frame animation.

Even in proportional fonts, the digits 0 to 9 get the same width. Why? So that tables of figures will look reasonable.

We don't want 1111.11 looking narrower than the 100.00 in the preceding row.

I want everything to be crisply aligned between lines like:

   xr = x * cos(theta) - y * sin(theta);
   yr = x * sin(theta) + y * cos(theta);
This makes it easy to see where the differences are. Any gratuitous deviation from the alignment is visually distracting. I want to be able to scan the thing vertically y and see that I have two x's, two y's, cos/sin sin/cos, and -/+ at a glance.
This must be why math literature has been using fixed width fonts for hundreds of years! No way you could ever align something with (hugely more readable) proportional fonts. BTW, numerals are not automatically same width in proportional fonts; a good font will often have both (and for tables you'd explicitly switch to tabular figures, which will align properly).
>Why does code need to be monospaced? I feel like the only reason we all do it is that it's always been done like that so it feels wrong not to.

Because alignment.

>The only actual reason I can think of for it is alignment, and that seems like a minimal benefit really.

Most programmers I know would argue otherwise, including me. Wanna try a poll?

>Variable width text is generally considered easier to read, so why couldn't this also apply to code?

Because it's only considered "easier to read" for general text (books, articles, etc) where you don't need to quickly see anything standing out of the ordinary, and alignment doesn't matter.

Even for traditional uses like accounting, "variable width" is not considered easier to read.

Fuck knows why it couldn't - but in my personal experience code is infinitely more difficult to read without a monospaced font.