Hasty Briefsbeta

Bilingual

The hardest program I've ever written (2015)

6 months ago
  • #programming
  • #code-formatting
  • #Dart
  • The author wrote a challenging program, dartfmt, an automated code formatter for Dart, which took nearly a year to complete.
  • The formatter modifies whitespace in code to ensure readability and consistency, inspired by Go's gofmt.
  • Balancing quality and performance was difficult, especially with complex formatting rules and line length constraints.
  • The formatter uses chunks, rules, and spans to manage splits and indentation, optimizing for the best output.
  • Line splitting is treated as a graph search problem, with optimizations to handle the exponential solution space efficiently.
  • Special cases, like machine-generated code, can still slow down the formatter, prompting an escape hatch after 5,000 attempts.
  • The formatter's output is a well-formatted Dart code string, achieved through sophisticated ranking and cost calculations.