3 ms·
> If computers are good at anything, they are good at parsing code and analyzing it. So I set out to make this work, and prettier was born. I didn't want to sta
by sharpercoder 10y ago
> If computers are good at anything, they are good at parsing code and analyzing it. So I set out to make this work, and prettier was born. I didn't want to start from scratch, so it's a fork of recast's printer with the internals rewritten to use Wadler's algorithm from "A prettier printer".
Bob Nystrom (munificent) disagrees[0] after writing one himself:
> The search space we have to cover is exponentially large, and even ranking different solutions is a subtle problem.
[0]: http://journal.stuffwithstuff.com/2015/09/08/the-hardest-program-ive-ever-written/ http://journal.stuffwithstuff.com/2015/09/08/the-hardest-pro...
- NegativeLatency 10y agoThat's a great article, it really digs into some of the complex issues
- harpocrates 10y agoThis is a great read! From having dipped my toes into this, I'd say a large part of the problem (from the library designer's point of view) is deciding what interface to expose to the library user and making that as language agnostic as possible. The question becomes: what are the fundamental things that usually characterize pretty-printing? Obviously (or maybe not), you'll want indentation, hard-breaks, soft-breaks, ... but where does it end? Less obviously, you may want to have alignment features. Figuring out the right set of building blocks is what I think Wadler did well. Also, describing how these building blocks interact using laws is really incredibly useful! For that, credit goes (I think) to Hughes [1]. [1] http://www.cse.chalmers.se/~rjmh/Papers/pretty.ps http://www.cse.chalmers.se/~rjmh/Papers/pretty.ps
- munificent 10y agoI will note that one could design a pretty printer that reliably had better performance if the formatting style they chose was simpler and more amenable to it. My (mostly self-imposed) task was more difficult because I was trying to follow the existing style that humans were hand-applying to their Dart code, and that had a lot of tricky non-local cases that look nice but are hard to automate.
- korm 10y agoI have to say Dartfmt is one of my favorite things about using Dart. It works amazingly well and the fact that it's not configurable is a great decision [0]. I'm never afraid to peek into unfamiliar code because of that. So thank you. [0] https://github.com/dart-lang/dart_style/wiki/FAQ#why-cant-i-configure-it https://github.com/dart-lang/dart_style/wiki/FAQ#why-cant-i-...
- munificent 10y agoYou're welcome! That means a lot to me. I tried really really hard to make it produce output I and others would like. > I'm never afraid to peek into unfamiliar code because of that. This is exactly why dartfmt exists. It's not about making your code more readable to you, it's about making strangers' code more readable, because that lowers the bar to contribution between people in the ecosystem.