The Hardest Program I've Ever Written – How a code formatter works (2015)
Report a link problem
The originally submitted URL now redirects to the address above. Updating the destination does not change the item’s HN history, Hall membership, or rank.
Submission timeline
2007–2026One slot for every year since HN launched. Height is that year's peak points; orange marks a 100+ point or 50+ comment breakout. Select a bar to open its strongest thread.
First comments on top threads
HN comment orderPhilip Wadler has a seminal paper [0] on implementing pretty printers by modeling them using an algebra. There's implementations of the algorithm in most programming languages nowadays, and some state of the art advances that help with performance [1] It's a very elegant algorithm, and is very pleasant to work. Elixir's formatter uses a variant of it [2], and it appears in the standard library [3]. I've personally made use of it to write a code formatter for GraphQL queries…
I read this before working on prettier and it was a bit scary to be honest :) I wasn’t convinced that the exhaustive approach with weigths was a good idea. It makes it computationally super intensive and hard to predict what the printer would do. Instead prettier runs on a simple idea: if something doesn’t fit in one line, break the outermost parent. This simple rule (implemented via the Wadler IR) is efficient to implement and makes the output generally…
The Dart formatter sounds really advanced, and reflects potential complexity of the language. I think I'd still prefer to see a formatter attempt to preserve any formatting which is already 'good enough' to pass as an output threshold. Code isn't just a recipe for a computer to do something, it's a language for explaining to other programmers what that thing is and what's important to the structure of accomplishing it. The choice of where to place a break can matter…
Great blog post and super interesting. My feeling though is the problem is they have a line limit. Maybe they should rethink their style. I'm serious. Before I worked at Google, in 30 years of programming I never worked at a company that had a line limit. Adding a line limit at Google did not make me more productive. At first I thought "hey, I guess 80 chars makes side by side comparison easier" but then I thought back, hmm…
The first top-level comment from each of the four biggest threads, in HN’s own order. Excerpts are shortened; open a comment for full context.
- Breakout years
- 5
- Total points
- 1416
- Total comments
- 472
100+ points or 50+ comments
reference only — not used in Hall rules or ranking
reference only — not used in Hall rules or ranking
Every submission
| Date | Title as submitted | By | Points | Comments |
|---|---|---|---|---|
| 2015-09-09 | The Hardest Program I've Ever WrittenFirst breakout | skybrian | 242 | 76 |
| 2018-06-05 | The Hardest Program I've Ever Written (2015) | tosh | 1 | 0 |
| 2018-06-09 | The Hardest Program I've Ever Written (2015) | tosh | 303 | 76 |
| 2019-11-10 | Automated Code Formatter (dartfmt): The hardest program I’ve ever written | pcr910303 | 2 | 0 |
| 2019-11-17 | Writing an Automated Code Formatter: The hardest program I’ve ever writtenHall induction | pcr910303 | 5 | 0 |
| 2020-02-18 | The Hardest Program I've Ever Written | pcr910303 | 2 | 0 |
| 2020-03-27 | The Hardest Program I've Ever Written – How a code formatter works (2015)Best thread | pcr910303 | 500 | 125 |
| 2022-03-05 | The hardest program I've ever written (2015) | graderjs | 261 | 133 |
| 2024-02-20 | The Hardest Program I've Ever Written: A code formatter | goranmoomin | 2 | 1 |
| 2024-04-28 | The hardest program I've ever written | andsoitis | 1 | 0 |
| 2025-10-29 | The hardest program I've ever written (2015)Latest 20+ point return | jacobedawson | 97 | 61 |