News for hackers. Stuff that matters.
Re:Almost monospaced: the perfect fonts for writing by bsimpson on Sunday November 06, 2022 @09:00PM (#33500127)
Isn't the whole point of monospaced fonts columnar alignment (e.g. the table in the article)? Seems like "almost" defeats the point.

Then again, I don't see values lined up much these days:

    {
      "someKey":       "someValue",
      "someOtherKey:"  "someOtherValue,
and if you only care about leading whitespace (that is, the whitespace before a character appears on the line), maybe it doesn't matter how your font is spaced.

I suspect numbers in these almost-mono fonts are monospaced, so numerical values should still have nice rhythm too.

Re:Almost monospaced: the perfect fonts for writing by opk on Sunday November 06, 2022 @09:36PM (#33500425)
I long ago concluded that trying to line stuff up in columns like this in code is a mistake. It often results in realignment of blocks for small changes so what should be a small diff in the git history ends up being big.

Only rarely is the vertical alignment useful because mostly code isn't read vertically. Used in moderation, the reverse Christmas tree style used in the Linux kernel can be as good for readability (sorted longest to shortest). For some constant structures a reordering of fields to put the variable length element at the end of the line removes the need for padding space (like in the output of `ls -l` where filenames come last).

A common indentation style for continuation lines is to align with the start of arguments to the function. I've never seen the sense of this variable amount where indentation after a while is much more than after an if. Double the normal indentation for continuation works well in languages where line continuation isn't otherwise distinct from normal blocks.

Re:Almost monospaced: the perfect fonts for writing by andai on Sunday November 06, 2022 @11:16PM (#33501064)
This just seems like a limitation of the technology. When you change the width of one column, the whole table should realign, without any changes to those parts of the file (it's just about how it's displayed).

I recall reading something about "tab stops" that solved this problem, but I don't think there are any mainstream implementations of it.

Re:Almost monospaced: the perfect fonts for writing by archseer on Monday November 07, 2022 @12:12AM (#33501458)
I think you mean elastic tab stops https://nickgravgaard.com/elastic-tabstops/
Re:Almost monospaced: the perfect fonts for writing by operator-name on Monday November 07, 2022 @08:20AM (#33505427)
Elastic tabstops actually mentions the input font in its "see also" section!
Re:Almost monospaced: the perfect fonts for writing by mmis1000 on Monday November 07, 2022 @12:12AM (#33501464)
tab stops is still available in terminals. Tab in terminal move the cursor to next x*8(depends on setting) position independent of what character you input before.

But a major flaw is that it only works when your cell has less than 8 characters. Or it will go to the wrong stop. Which isn't that common today.

Because why insist in short names and 80 width character limit? Almost everyone have a screen that can fit hundreds of characters per line today.

Re:Almost monospaced: the perfect fonts for writing by eru on Monday November 07, 2022 @05:34AM (#33504077)
> Because why insist in short names and 80 width character limit? Almost everyone have a screen that can fit hundreds of characters per line today.

Yet, books don't work that way, even though we have the technology. Long lines aren't readable. There's a reason newspapers have columns.

See eg https://baymard.com/blog/line-length-readability and https://en.wikipedia.org/wiki/Line_length

Re:Almost monospaced: the perfect fonts for writing by mmis1000 on Monday November 07, 2022 @12:24PM (#33508946)
Even without the line length problem, 8 character is unlikely to fit in a meaningful word. And because you need a space between each column, you actually only have 7. And nobody using random abbreviations now.
Re:Almost monospaced: the perfect fonts for writing by throwaway290 on Monday November 07, 2022 @12:56AM (#33501719)
Writing programs by manipulating text is the actual limitation of the technology here, alignment is just a minor detail by comparison.
Re:Almost monospaced: the perfect fonts for writing by throw827474737 on Monday November 07, 2022 @02:47AM (#33502503)
That's often heard, but what's the alternative? So far every other alternative has been more limiting, not?
Re:Almost monospaced: the perfect fonts for writing by eru on Monday November 07, 2022 @05:35AM (#33504087)
There are some sane alternatives.

You can keep to mostly text, but give the editor more leeway in how to lay it out and make the code's structure more visible.

Some really simple options, that you might not even recognise as going beyond text: folding of block and go-to-definition.

Re:Almost monospaced: the perfect fonts for writing by throwaway290 on Monday November 07, 2022 @09:23AM (#33506103)
AST, GUI... The technology is not there yet.
Re:Almost monospaced: the perfect fonts for writing by Rafert on Monday November 07, 2022 @11:35AM (#33508096)
Tab stops are called out in the footnotes:

> Never attempt to line up text by using spaces. The only exception is if you are using a monospaced font. But in word processing applications, there are appropriate tools available for lining up text, like tables[1] and tab stops[2].

[1]: https://practicaltypography.com/tables.html [2]: https://practicaltypography.com/tabs-and-tab-stops.html

Re:Almost monospaced: the perfect fonts for writing by rocqua on Monday November 07, 2022 @01:28AM (#33501917)
The value of alignment (at least for me) is not in making the text of the code more readable. Instead, the value is communicating the structure of the code.

Seeing the same shape repeatedly tells me a lot about what is happening. It also helps highlight changes between two lines that are very similar.

Re:Almost monospaced: the perfect fonts for writing by galaxyLogic on Monday November 07, 2022 @03:47AM (#33503046)
> the value is communicating the structure of the code.

I agree and I think that is the same thing as code-readability. Code-readability means you can easily understand the code.

I personally try to abide by what I call "hedge" formatting convention, the punctuation forms a vertical hedge which then shows the structure of your code clearly:

[ a

, b

, c

]

Moving my eyes down to look for the closing bracket is easier (to me) than moving my eyes from left to right to spot it somewhere on the right side of the page.

Note how above ALL the punctuation is in the same (first) column, Having read that it then becomes easier to read what is on the rest of the columns.

In general it helps when things that are the same, or have the same purpose, on each line are in the same horizontal position. It is is then easier to see what is different about each line, since it is easy to see what is the same.

Re:Almost monospaced: the perfect fonts for writing by hoseja on Monday November 07, 2022 @04:20AM (#33503357)
Yes, this is what I've also gravitated towards.
Re:Almost monospaced: the perfect fonts for writing by the_sleaze9 on Monday November 07, 2022 @08:36AM (#33505598)
Thanks, I hate it.

But as long as everything is written under the same guidelines, I will accept it.

Re:Almost monospaced: the perfect fonts for writing by q7xvh97o2pDhNrh on Monday November 07, 2022 @09:38AM (#33506290)
Huh, I've always wondered about this style of code formatting and never fully understood why people did it.

Your explanation makes good sense. I guess I just wasn't seeing the forest for the trees. ;)

Re:Almost monospaced: the perfect fonts for writing by layer8 on Monday November 07, 2022 @06:26PM (#33514004)
Languages should allow for leading commas to enable the more uniform:

  [
  , a
  , b
  , c
  ]
Depending on the rest of the language syntax, it could also be nice to have a markdown-style list syntax:

  [
  - a
  - b
  - c
  ]
Re:Almost monospaced: the perfect fonts for writing by galaxyLogic on Tuesday November 08, 2022 @12:18AM (#33516101)
In JavaScript a leading comma in an array would create a first element whose value is undefined, I believe.
Re:Almost monospaced: the perfect fonts for writing by bmn__ on Tuesday November 08, 2022 @02:18AM (#33516702)
What a stupid design. If the consortium had just one member having the competence of knowing some competing design and using his brain for five minutes, this disaster could have been avoided.

https://news.ycombinator.com/item?id=9624429

Re:Almost monospaced: the perfect fonts for writing by layer8 on Tuesday November 08, 2022 @03:33AM (#33517136)
Well, JavaScript is pretty much a lost cause anyway.
Re:Almost monospaced: the perfect fonts for writing by yowzadave on Wednesday November 09, 2022 @04:02PM (#33537780)
That's correct, but you can use a trailing comma if you prefer uniform lists:

  [
    a,
    b,
    c,
  ]
Re:Almost monospaced: the perfect fonts for writing by galaxyLogic on Friday November 11, 2022 @09:19PM (#33569351)
I simply prefer :

[ a

, b

, c

]

because, I think this is as 'uniform" as it gets. All syntactic separators are in the first column. Thus they are all uniformly in the same column. Line 1 has the syntactic separator '[' which means start-of-array. Lines 2 and 3 have ',' as the syntactic separator which means element-separator. And line 4 has the final syntactic marker which means "end of Array".

All elements of the array start at the same column, uniformly. And all syntactic separators are in the same column also uniformly.

Re:Almost monospaced: the perfect fonts for writing by ako on Monday November 07, 2022 @01:42AM (#33502010)
I stopped manually formatting code long ago, just accept what the code formatter tool decides is good enough. Saves a lot of time.
Re:Almost monospaced: the perfect fonts for writing by sverhagen on Monday November 07, 2022 @02:27AM (#33502338)
In circumstances, and depending on configuration, the code formatter tool may still aim for alignment, such as assignment operators on subsequent lines. Whether you like that or not, for me the question still stands: how does that jive with "almost monospaced".
Re:Almost monospaced: the perfect fonts for writing by pmontra on Monday November 07, 2022 @02:50AM (#33502533)
I've been using proportional fonts in programming for about ten years. I stopped doing things like

  abc = 1
  d   = 2
which add very little readability value IMHO.

Things like

  func(arg1,
       arg2,
       arg3)
usually don't align well because func( has a different width than the five spaces in the lines below. However

1) my text editor (emacs) aligns those lines for me (probably any programmer's editor does) so at least I don't have to do precision work

2) I ended up writing code like

  func(
    arg1,
    arg2,
    arg3
  )
especially with named arguments (arg=value). That aligns even with proportional fonts and all arguments immediately stand out.

If the arguments are few (two or three) and there are no named arguments I don't break them on multiple lines.

The result is that no coworker ever complained about my code.

Languages with a formatter (Elixir) solve the problem once and for all and align well with both kinds of fonts.

Re:Almost monospaced: the perfect fonts for writing by amadvance on Monday November 07, 2022 @03:31AM (#33502900)
I do something similar also with if() and others:

  if (condition...
      && condition...
      && condition...
  ) {
Re:Almost monospaced: the perfect fonts for writing by tuyiown on Monday November 07, 2022 @07:23AM (#33504906)
Same here but pushed it to force same column condition statement:

    if ((
      condition1
    ) && (
      condition2
    ) && (
      ...
    )) {
Re:Almost monospaced: the perfect fonts for writing by Izkata on Monday November 07, 2022 @02:13PM (#33510783)
But that loses the table-like structure and makes it harder to read.
Re:Almost monospaced: the perfect fonts for writing by anilakar on Monday November 07, 2022 @03:22AM (#33502811)
Formatting your code in an unintuitive way because of the font is a code smell to me.
Re:Almost monospaced: the perfect fonts for writing by pmontra on Monday November 07, 2022 @03:25AM (#33502845)
Honest question: which one is unintuitive?
Re:Almost monospaced: the perfect fonts for writing by alpaca128 on Monday November 07, 2022 @05:17AM (#33503926)
Why? The purpose of code is to be readable to humans.

At the same time I get your point and also the argument about Git diffs (which also should be readable). So maybe the ideal situation would be a separation of code and its formatting, so that we have more options than tabs and also no need for crutches like tabs. Like a better, more flexible code formatter that lets you display and edit code in the editor using one style but then saves the file in a standard format that's consistent across the whole project.

Re:Almost monospaced: the perfect fonts for writing by nkrisc on Monday November 07, 2022 @05:41AM (#33504154)
Viewing the examples generally, the second one better communicates hierarchy at the cost of an extra line. The arguments are all grouped together visually and without distraction, while in the first example the first argument gets muddled with the function name. Why? To save a line break?
Re:Almost monospaced: the perfect fonts for writing by kazinator on Monday November 07, 2022 @12:27PM (#33508992)
The canonical notation is like this:

  func(arg1, arg2, arg3)
the first example splits it across multiple lines in a way which maximally preserves the other apects of the notation.
Re:Almost monospaced: the perfect fonts for writing by nkrisc on Monday November 07, 2022 @09:46PM (#33515361)
> the first example splits it across multiple lines in a way which maximally preserves the other aspects of the notation.

Once you’re splitting it across lines I don’t think that is a desirable attribute. Better to use a format that suits multiple lines than attempt to match as closely as possible to a format intended for one line. Like those “flying cars” that try to look like the “canonical” car.

From my perspective this is a UI problem (though not a serious one).

Re:Almost monospaced: the perfect fonts for writing by seanmcdirmid on Monday November 07, 2022 @06:58AM (#33504721)
That’s what many auto formatter do anyways, even ones setup for fixed width fonts.
Re:Almost monospaced: the perfect fonts for writing by ako on Monday November 07, 2022 @04:02AM (#33503190)
It should ignore "almost monospace". Most projects have multiple developers, you should assume different developers have different IDE/font preferences, so your formatting shouldn't try to cater for it.
Re:Almost monospaced: the perfect fonts for writing by GoblinSlayer on Monday November 07, 2022 @05:54AM (#33504243)
I just put stuff on one line and let the editor wrap them. Doesn't waste vertical space, compatible with any font, any size, any window width, any form factor, the best from all worlds.
Re:Almost monospaced: the perfect fonts for writing by thaumasiotes on Monday November 07, 2022 @01:58AM (#33502107)
> I long ago concluded that trying to line stuff up in columns like this in code is a mistake. It often results in realignment of blocks for small changes so what should be a small diff in the git history ends up being big.

Seems wrongheaded to me; that's the same reason Elm wanted you to write arrays like this:

    [item1
     ,item2
     ,item3
     ,item4
    ]
instead of a sane way. It means adding or removing an element only changes one line in the diff!

Who cares? It's not hard to understand what's happening in a diff that makes this kind of change. You want the code to be easy to read, not easy to diff.

You could also easily base your diff algorithm on a lexer for the language rather than a lexer for ascii, in which case changing the amount of whitespace would register as "not a change".

Re:Almost monospaced: the perfect fonts for writing by madsohm on Monday November 07, 2022 @02:32AM (#33502383)
Many languages also support a comma after the last item, meaning you can still have one line diffs and have a sane reading structure:

    [
      item1,
      item2,
      item3,
      item4,
    ]
Re:Almost monospaced: the perfect fonts for writing by mikojan on Monday November 07, 2022 @03:42AM (#33502991)
This looks bad. Haskell is much better:

  [ item1
  , item2
  , item3
  , item4
  ]
Re:Almost monospaced: the perfect fonts for writing by thaumasiotes on Monday November 07, 2022 @04:16AM (#33503327)
Compare lisp:

    (item1
     item2
     item3
     item4)
;p
Re:Almost monospaced: the perfect fonts for writing by vore on Monday November 07, 2022 @04:03AM (#33503200)
Leading comma is better at resolving textual 3-way merges, e.g.:

   [ a         [ a       [ a
   , b         , b    /  , b
   , c   -->   , c   /   , c
   , d         , d  /    , dd
   ]           , e       ]
               ]
Your usual 3 way merge algorithm will correctly deduce:

   [ a
   , b
   , c
   , dd
   , e
   ]
Contrast this with trailing comma:

   [ a,         [ a,       [ a,
     b,           b,    /    b,
     c,   -->     c,   /     c,
     d            d,  /      dd
   ]              e        ]
                ]
You now have a merge conflict between "d," and "dd".

However, if you were to require a trailing comma after the final element you wouldn't have this problem.

Re:Almost monospaced: the perfect fonts for writing by Izkata on Monday November 07, 2022 @02:17PM (#33510859)
> Contrast this with trailing comma:

Example doesn't have a trailing comma. If it did, it would act the same as the leading comma.

But also the leading comma just moves the problem to the beginning of the list instead of the end. Trailing comma with the opening [ on its own line wouldn't have that problem anywhere.

Re:Almost monospaced: the perfect fonts for writing by hnbad on Monday November 07, 2022 @03:18AM (#33502783)
Arguably another problem with lining up code like in the example is that it emphasizes the relationship between the keys and between the values rather than between each key-value pair. This may be useful when writing out matrices but in the example of an object/dict with key-value pairs it seems a distraction more than an affordance.
Re:Almost monospaced: the perfect fonts for writing by crispyambulance on Monday November 07, 2022 @08:23AM (#33505469)
> ... trying to line stuff up in columns like this in code is a mistake.

I've encountered this sentiment more than a few times and I find it hard to accept. It totally makes sense to align repetitive stuff in columns. That's why people use tables in other contexts, and code should be no different. This alignment makes clear any deviations from the norm and lets you "see" the structure of the data easily.

It doesn't matter for minor stuff and, usually, the code linter/formatter is "good enough"-- but sometimes, you REALLY want to have columnar layout!

Re:Almost monospaced: the perfect fonts for writing by blakewatson on Sunday November 06, 2022 @09:12PM (#33500223)
Yeah, you’re right. You lose columnar alignment but beginning indentation still aligns and, in Input Sans, I believe the numbers are tabular. David Jonathan Ross makes this argument in the explainer for Input:

> Sometimes programmers rely on the monospaced grid to create a second column of values or comments on the right side of the page. It’s true, these secondary columns won’t align in a proportionally spaced font. But why are we making these columns in the first place? Even in a monospaced font they can be finnicky and hard to maintain.

> In virtually every other form of typography, the responsibility of alignment is given to the typesetting application, not the font. If source code editors can highlight syntax, they could also interpret tabs and syntax to create true, adjustable columns of text.

https://input.djr.com/info/

Re:Almost monospaced: the perfect fonts for writing by bsimpson on Sunday November 06, 2022 @09:54PM (#33500549)
I still can't believe personalization isn't more widespread in coding. For instance, it ought to be reasonably trivial to set up your formatting preferences in your editor, but have the file saved to disk be in a common format.

You want 8-space tabs and braces on their own lines? Cool. We'll save the file with all the formatting defaults, but run a formatter tuned to your preferences before displaying.

Re:Almost monospaced: the perfect fonts for writing by kaashif on Sunday November 06, 2022 @10:11PM (#33500661)
Here's my reason: if what I see on screen doesn't match the code actually being committed, that freaks me out.

Same reason I don't like using those things where <= turns into ≤ in your editor but it's written to disk as <=.

Re:Almost monospaced: the perfect fonts for writing by somethingsaid on Sunday November 06, 2022 @11:22PM (#33501106)
Similarly, for some languages the meaning of what is written can change based on the indentation and line breaks. Or a statement written under one formatting standard can become significantly harder to read under a different formatting standard.
Re:Almost monospaced: the perfect fonts for writing by bsimpson on Sunday November 06, 2022 @11:44PM (#33501260)
There are plenty of automated formatters - clang-format, prettier, gofmt, black. You would delegate to one of them, configurable in the project/editor, and adjustable by file extension.

If the code doesn't parse, save it as-is. If it does, run the formatter with the defaults before saving. When opening, run the formatter with the user's preferences before displaying.

Gets you the benefits of a formatter, with the freedom to control your environment to your tastes. You can already pick your editor, font, and color theme. You ought to be able to pick your formatter settings too.

Re:Almost monospaced: the perfect fonts for writing by kevin_thibedeau on Monday November 07, 2022 @12:21AM (#33501515)
This invariably leads to fucked up diffs and useless blaming. In theory tabs are superior as a way to provide configurable layout but as soon as you have to collaborate between users with different tooling you end up in a mixed whitespace hellscape.
Re:Almost monospaced: the perfect fonts for writing by Beltalowda on Monday November 07, 2022 @12:26AM (#33501544)
Accurate reporting of the error positions is one reason this is a non-trivial thing to implement. It's a fixable problem, just takes quite a bit of tooling and complexity for a good experience, and all things considered: is it really worth it?
Re:Almost monospaced: the perfect fonts for writing by throwaway27727 on Monday November 07, 2022 @12:42AM (#33501640)
Re:Almost monospaced: the perfect fonts for writing by coldpie on Monday November 07, 2022 @10:28AM (#33507025)
IMO, the fact that personalization isn't more widespread is good evidence that code style doesn't actually matter. Humans adapt. So long as it doesn't actually interfere with the function of writing code (eg mixed tabs & spaces; wildly inconsistent identifier names), your reader will get over it quickly after some snide comments to their friends on IRC, and then work on the actual work that needs doing. I think people spend too much thought on this topic.
Re:Almost monospaced: the perfect fonts for writing by kqr on Sunday November 06, 2022 @11:33PM (#33501184)
Numbers in many non-monospaced fonts are also monospaced. It's a font feature known as tabular figures.
Re:Almost monospaced: the perfect fonts for writing by lelanthran on Monday November 07, 2022 @12:27AM (#33501553)
With non-monospaced fonts, it doesn't matter how many or how few leading spaces you insert, the columns may never line up, and your IDE can't determine them automatically.

With monospaced fonts, the columns will be guaranteed to line up with the correct number of spaces, which your IDE will determine anyway.

Re:Almost monospaced: the perfect fonts for writing by jen20 on Monday November 07, 2022 @01:20AM (#33501866)
This “feature” of lined up values annoys the hell of me when using Go (in particular) and also seems prevalent in Ruby-derived things. IMO unless you pinstripe line backgrounds, it makes it harder to trace which value maps to which key in the presence of a single longer key name. It also leads to noisier diffs.
Re:Almost monospaced: the perfect fonts for writing by karmakaze on Monday November 07, 2022 @02:12AM (#33502202)
If spaces had half-width, then the Duo could still use spaces for alignment, though monospaced text would need to be run through a filter.
Re:Almost monospaced: the perfect fonts for writing by Cthulhu_ on Monday November 07, 2022 @04:00AM (#33503163)
You know, now you mention it, I thought about this and realized I mainly want items to START on the same column, after that I don't care; only exception is tables in markdown, but those are few and far between.

I might give these fonts a try, although I've been used to Menlo for a long time.

Re:Almost monospaced: the perfect fonts for writing by WithinReason on Monday November 07, 2022 @04:04AM (#33503211)
I wonder if it would be possible to create a font that is not monospaced for letters but it's monospaced for words, so if you have a word + white space the end of the white space is the same as it would be with a monospace font. Alternatively, one could write "alignment characters" like & if one wanted to align lines like in LaTeX formulas.
Re:Almost monospaced: the perfect fonts for writing by rob74 on Monday November 07, 2022 @04:22AM (#33503370)
Yeah, that's why I'm skeptical about these "almost monospaced" fonts too. In my opinion, the actual problem is sans serif monospaced fonts: there most of the glyphs are sans serif, but then the designers are forced to add serifs only to the narrow glyphs to fill up the available space, so these end up sticking out like sore thumbs. So monospaced fonts that have serifs on all glyphs look much better IMHO.
Re:Almost monospaced: the perfect fonts for writing by rrwo on Monday November 07, 2022 @05:05AM (#33503803)
Typewriters and dot matrix printers were monospace. It had nothing to do with lining code up.

When I attended university in the mid-1990s, and again in mid-2000s, we were required to hand in papers using monospace font, black ink, with a specific point size and margins.

It kept everything looking consistent with typewriters (which some people still used through the 1990s), but also kept people from using distracting or illegible fonts, or trying to work around page or word limits by using different fonts.

Re:Almost monospaced: the perfect fonts for writing by lucideer on Monday November 07, 2022 @06:29AM (#33504499)
Another benefit of monospaced fonts is ease of calculation of text-container width: any line x character count will always be the same length as any other.

The main use of this in practice is using line-char-count limits as a proxy to lint for code readability, but it can also be beneficial in other contexts.

Re:Almost monospaced: the perfect fonts for writing by scythe on Monday November 07, 2022 @09:18AM (#33506032)
If you drop the awkward "3/4" width and just have 0.5, 1, 1.5 char widths, then you could recover alignment by using U+2009 as the half-width space, maybe entered as Shift+space.
Re:Almost monospaced: the perfect fonts for writing by IshKebab on Monday November 07, 2022 @09:33AM (#33506238)
From the article:

> You might be thinking, “Okay, but now characters won’t line up neatly.”

Re:Almost monospaced: the perfect fonts for writing by jamesfisher on Monday November 07, 2022 @02:19PM (#33510894)
I agree "almost" defeats the point, and that columnar alignment is not very valuable, and so there's actually a stronger argument to be made: we should just use normal fonts for coding. What's wrong with, say, Helvetica?