News for hackers. Stuff that matters.
Re:The hardest program I've ever written (2015) by mjevans on Saturday March 05, 2022 @06:36AM (#30566588)
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 for cognition and can be almost as important as the printed characters for organizing thoughts.

Re:The hardest program I've ever written (2015) by ramraj07 on Saturday March 05, 2022 @07:34AM (#30566974)
It might be true but as OP says in the article, the moment you have potential multiple ways to show the same piece of code, you’re going to surface engineers with each opinion on the code review. Getting an opinionated formatter is the best way to bring engineers back to do what they really need to be doing in code review, which is review the code not its formatting. I’ll never go back to python without black and Isort!
Re:The hardest program I've ever written (2015) by fxtentacle on Saturday March 05, 2022 @08:14AM (#30567261)
I feel like enforcing a specific text style is the wrong approach.

In my opinion, different people have different formatting preferences and forcing someone to use the "wrong" one will lead to slower reading speed and the risk of overlooking errors.

That's why I believe we should treat it like font or color choices. The IDE should display source code in the viewer's preferred style, so that each person sees what they expect. And then the actual source code formatting becomes irrelevant.

Go already goes a good step into this direction by making a language AST tree part of their core libraries. And opening Go source code in JetBrain's GoLand will show you additional annotations, spacing, etc. based on the parsed source code tree (and not based on the source code's text).

Re:The hardest program I've ever written (2015) by davemp on Saturday March 05, 2022 @10:06AM (#30568163)
Having a strictly (formatter) enforced style is actually how you allow people the freedom to use their preferred style.

Everyone can just set up a pre-commit hook to automatically format the code back into the official style, then everyone is free to put the codebase back into their preferred style.

Otherwise, if developers reformatted code to their preferred style regularly, it would create massive diffs.

Re:The hardest program I've ever written (2015) by fxtentacle on Saturday March 05, 2022 @10:39AM (#30568416)
I feel like I might have explained that badly. In my suggestion, the source code would be diffed as a machine-readable AST tree. That means all source code reformatting actions which do not change the meaning of the source code also do not appear in the diff.

In this explanation about Go:

https://golangdocs.com/golang-ast-package

the text following "and we get a nice structure" is what the source code management tools would be working on. It's an abstract representation of the source code's meaning, but not tied to how things are formatted or indented.

Re:The hardest program I've ever written (2015) by ameliaquining on Saturday March 05, 2022 @10:57AM (#30568564)
I agree it would (maybe) be better if things had been designed this way from the beginning, but as it is it would be totally incompatible with all existing source code management tools, which is a nonstarter. Even if you can switch to a new editor or IDE, you still have to worry about the GitHub web UI and your error reporting tooling and who even knows what else.
Re:The hardest program I've ever written (2015) by davemp on Sunday March 06, 2022 @12:33AM (#30575287)
> That means all source code reformatting actions which do not change the meaning of the source code also do not appear in the diff.

The method I described does exactly that. Most modern formatting tools parse code into an AST then rewrite it based formatting rules. So as long as you format both files to the same rules before diffing, only AST changes will show up.

Re:The hardest program I've ever written (2015) by rowanG077 on Saturday March 05, 2022 @04:10PM (#30571901)
On paper I like this idea. But I can't shake the feeling that every non-text based format I have ever encountered sucks.
Re:The hardest program I've ever written (2015) by hiccuphippo on Saturday March 05, 2022 @11:18AM (#30568749)
And this is how git works with the different newline styles. It can convert them to a single enforced style on commit and converted back when you pull changes.
Re:The hardest program I've ever written (2015) by delusional on Saturday March 05, 2022 @11:37AM (#30568943)
I've never had that work correctly. Mostly it just gets in the way when the line endings actually matter (because my javascript has to load in IE7).
Re:The hardest program I've ever written (2015) by awild on Saturday March 05, 2022 @11:51AM (#30569125)
From experience I can tell that one can get used to other people's styles and just work with that oneself. Unifying among an enforced style and sticking to it really reduces the amount of unnecessary discussions.

I genuinely feel that focusing a lot on the detriment of someone else's or an established code style is a sign of a lack of team work.

Re:The hardest program I've ever written (2015) by duped on Saturday March 05, 2022 @11:41AM (#30568996)
The AST usually isn't enough, you want a CST (or what a few sources call "full" syntax tree, preserving white space, comments, etc). I think .NET has the best implementation of this out there, unsurprisingly they have incredible tooling support.
Re:The hardest program I've ever written (2015) by eyelidlessness on Saturday March 05, 2022 @04:19PM (#30572004)
For anyone interested in CST tooling outside of the .NET ecosystem, tree-sitter[1] is general purpose, quite fast, supports a wide range of grammars, and has bindings in quite a lot of environments (including WASM, so likely can be used anywhere with some effort).

1: https://tree-sitter.github.io/tree-sitter/

Re:The hardest program I've ever written (2015) by duped on Saturday March 05, 2022 @05:17PM (#30572504)
Tree sitter is absurdly complicated to use in a real project, a hand written parser might be slower but it's way easier to implement and build.
Re:The hardest program I've ever written (2015) by eyelidlessness on Saturday March 05, 2022 @07:44PM (#30573705)
I haven’t used it in a real project yet, but I’ve tried it out for a few project ideas, and haven’t found it complicated to use. I’m not necessarily saying it isn’t ever complicated, but I was able to knock out a couple proofs of concept in a few hours. My only complaint so far is that the sexpr representation lacks some details, but I have no problem with working around that.
Re:The hardest program I've ever written (2015) by ramraj07 on Saturday March 05, 2022 @01:54PM (#30570647)
This seems like the arguments devs stuck on vim and eMacs keep making. “Ohhh I’m tired of moving my fingers from one key to another!!” [1] It’s just code, if you’re in a good team each PR is just a small diff, it’s hard to believe that’s somehow too complicated for an ostensibly good engineer.

[1] Incidentally the only people I know with carpel tunnel are folks who are entrenched in command line text editors. Maybe there’s some benefit to moving your arms around? Similarly maybe there’s benefit in reading code in a different format, you might actually read it slower and hence comprehend it better.

Re:The hardest program I've ever written (2015) by spc476 on Saturday March 05, 2022 @07:00PM (#30573338)
I am one of those folks entrenched in command line editors, and I avoid carpel tunnel by using a real keyboard (in my case, a real IBM model M) and not the absolute crap that passes for keyboards on laptops (or those keybords in the $1 discount bin).
Re:The hardest program I've ever written (2015) by ramraj07 on Saturday March 05, 2022 @09:47PM (#30574550)
Good for you but I stick to a standard apple chicklet keyboard and thin mouse and feel the most productive. The mix of moving your arm but always having it rested flat helps in my experience.
Re:The hardest program I've ever written (2015) by coldtea on Saturday March 05, 2022 @12:20PM (#30569496)
>In my opinion, different people have different formatting preferences and forcing someone to use the "wrong" one will lead to slower reading speed and the risk of overlooking errors.

Not if everybody using the language is strongly forced (as with Go), since then those with "different formatting preferences" will eventually (and soon) just get used to the enforced style.

Re:The hardest program I've ever written (2015) by chrisweekly on Saturday March 05, 2022 @11:51AM (#30569124)
Agreed! I use Prettier (driven by ESLint) in all my web projects (including enterprise clients', where I've helped introduce / improve / standardize tooling). IME it's a mistake not to use it.
Re:The hardest program I've ever written (2015) by CodesInChaos on Saturday March 05, 2022 @07:05AM (#30566779)
> The Dart formatter sounds really advanced, and reflects potential complexity of the language.

I would not be surprised if a lisp formatter following the same philosophy as the dart formatter would be similarly complex, since the complexity comes from optimizing line length, not parsing.

Re:The hardest program I've ever written (2015) by mbrock on Saturday March 05, 2022 @07:15AM (#30566839)
Incidentally I spent yesterday implementing a Lisp formatter with the algorithm described by Jean-Philippe Bernardy in the simple and elegant paper "A Pretty But Not Greedy Printer."

https://jyp.github.io/pdf/Prettiest.pdf

It's based on introducing choice between vertical and horizontal stacking. It avoids combinatorial explosion by pruning away strictly suboptimal choices. With just a few extra rules for Lisp forms, the results are quite good.

I don't handle comments though, since I only need it for pretty printing values so far.

Re:The hardest program I've ever written (2015) by lakecresva on Saturday March 05, 2022 @06:16PM (#30572975)
I also wrote a version of Bernardy's pretty printer, but I never did come up with a satisfactory way of formatting text together with code. The best looking solution of splitting on whitespace and making each word a group, either tacking it onto the current line or splitting onto a new line ends up being exponential even with pretty aggressive optimization. Everything else I tried looked bad.
Re:The hardest program I've ever written (2015) by virtualwhys on Saturday March 05, 2022 @03:42PM (#30571659)
> The Dart formatter sounds really advanced, and reflects potential complexity of the language.

You got that right -- check out this epic GitHub thread on optional semi-colons [1], and the author's own comment on the subject in an HN thread [2]

[1] https://github.com/dart-lang/language/issues/69

[2] https://news.ycombinator.com/item?id=22706645

Re:The hardest program I've ever written (2015) by atoav on Sunday March 06, 2022 @05:01AM (#30576374)
I thought so as well. Until one day I just switched on black for python to see what it is like.

I realized there was a tradeoff I never considered — the amount of time it would cost me to manually format only 10% better than the black formatter is not something I am willing to invest.

And this is me talking about ideal situations where I calmy try to write beautiful code, when you consider that often you are not in the mood to spend a ton of time on formatting, because it distracts you from focusing onto the actual data flow, logic and so on a formatter as good as this is just beautiful.

This is not something I would've said before I tried the thing btw.

Re:The hardest program I've ever written (2015) by mushyhammer on Saturday March 05, 2022 @07:14AM (#30566838)
> 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.

That feels like Prettier sometimes. I think it leaves some objects on multiple lines or one line depending on how you leave them. But I’m not too sure.

Re:The hardest program I've ever written (2015) by rowanG077 on Saturday March 05, 2022 @04:06PM (#30571867)
I'd really hate a formatter to attempt to preserve formatting if it's "close enough"(tm). Because what is "close enough"(tm) is hugely subjective, like formatting style itself. Formatter should just format to a common style. No funny business.
Re:The hardest program I've ever written (2015) by rendall on Saturday March 05, 2022 @07:22AM (#30566888)
There is tension between having a single source of truth for source code, including an opinionated formatter; and allowing individual programmers expressive facility to format and structure code in a way that makes most sense to them.

I've been thinking lately of a formatter that would resolve this tension. It would be a local, individual formatter, complementary to the global, opinionated, "Prettier" formatter.

When the source code was committed to remote for review, the opinionated formatter would do its thing, making sure the code was formatted properly according to whatever the team agreed. But locally, the code would remain however the developer liked it.

Re:The hardest program I've ever written (2015) by hprotagonist on Saturday March 05, 2022 @07:30AM (#30566944)
a recipe for merge conflicts.

i think maybe your only hope is to somehow “version the AST”, and let formatting be a style sheet or something. But i’m 1. talking out my butt and 2. sure i’m missing something.

Re:The hardest program I've ever written (2015) by fragmede on Saturday March 05, 2022 @08:18AM (#30567289)
It's a decent idea!

One problem though is all of the non-code elements, aka comments and how they intentionally align up with the code (mixed with spaces and tabs), so the ideal format is the one the original programmer wrote it in, and the second programmer using a different stylesheet can't edit comments and have them end up pretty for the first programmer. (The solution, obviously, is to not comment any code >_< )

Re:The hardest program I've ever written (2015) by javbit on Saturday March 05, 2022 @10:09AM (#30568188)
If the display of the code is removed from its representation, I think the same could be done for comments. Comments could be kept as part of the AST and rendered how you like.

E.g.

    (+ 1 2)  ; Add two numbers.
Would become

    (comment (+ 1 2) "Add two numbers")
With semantics like const.

Another would be

    (+  ; Adding
     1  ; one
     2  ; and two.
     )
To

    ((comment + "add")
     (comment 1 "one")
     (comment 2 "two"))
You could display comments as popups, marginalia, or even in a traditional fashion (since some intent is captured by the comment scoping. You could also have different types of comments like annotation to have different kinds of display types.
Re:The hardest program I've ever written (2015) by Cullinet on Saturday March 05, 2022 @08:46AM (#30567532)
I agree with the decency of the ide

But isn't this the nearest argument why we should start publishing negative results papers?

The most important thing that I have ever learned was a announcement made by Google about how their industry consortium seeking to trade print magazine advertising inventory, failed with reference to the nature of this failure. No matter how lacking in detail this notice was, no matter that I was sent down a seven years solitary and very lonely path of mercifully ultimate discovery, on the heels of my startups exit collapse due to my cofounder tragically dying but we'd have been sunk by the problems indirectly revealed in that Advertising Age news item. However painful, and I'm talking about therapy for years after I emerged from reclusion, and real health issues giving up running and my diet turning to junk energy hits...I have learned more from being told "no can do Z because of y" and I seriously think that we /seriously have to start already getting over the reasons why we don't talk about our failures/. I mean holy smoke wouldn't we ever run out of good conversation ever again if we could have a good old banter and brew over our cockups? I'm thinking that this is how women are so much more successful in reproduction if they actually want to. What could we do if we tried?

Re:The hardest program I've ever written (2015) by VBprogrammer on Saturday March 05, 2022 @09:19AM (#30567797)
Was this GPT-2 trying to write a HN comment?
Re:The hardest program I've ever written (2015) by olvy0 on Saturday March 05, 2022 @09:52AM (#30568035)
I'm not sure. Their posting history has multiple posts of this type with disconnected sentences. With GPT 2 at least there is usually some continuity and a semblance of a shared context between the sentences.
Re:The hardest program I've ever written (2015) by freedomben on Saturday March 05, 2022 @02:31PM (#30571037)
After reading several of the comments, I think OP is not a native English speaker, and that makes for some awkward grammar/sentence structure.
Re:The hardest program I've ever written (2015) by andai on Saturday March 05, 2022 @02:41PM (#30571152)
I am reminded of Terry Davis.
Re:The hardest program I've ever written (2015) by rendall on Saturday March 05, 2022 @07:52AM (#30567103)
> let formatting be a style sheet

That's a good expression of the idea. If merge conflicts happen, we're doing it all wrong. Version control shouldn't even really be aware of this.

Re:The hardest program I've ever written (2015) by hprotagonist on Saturday March 05, 2022 @07:56AM (#30567139)
tree-sitter and git: now kith!
Re:The hardest program I've ever written (2015) by Cullinet on Saturday March 05, 2022 @08:48AM (#30567548)
the most extreme validation of typographical precision is the banknote. Discuss?...
Re:The hardest program I've ever written (2015) by anchpop on Saturday March 05, 2022 @01:29PM (#30570363)
This is exactly what Unison is trying to do
Re:The hardest program I've ever written (2015) by munificent on Saturday March 05, 2022 @03:59PM (#30571810)
> reflects potential complexity of the language.

The language is fairly complex syntactically and that definitely adds some cost to formatting.

But I think much of the complexity comes from two things:

1. A lot of idiomatic Dart uses function literals in a block-like way, as in:

    test(() {
      expect(1 + 2, 3);
    });
2. But Dart doesn't actually having trailing block argument syntax like Smalltalk, Ruby, and Kotlin. So the formatter has to look at the closures passed to an argument list and decide which ones look better using block formatting, versus regular argument list formatting like:

    someFunction(
        () {
          expect(1 + 2, 3);
        });
Also, at the time I first wrote dartfmt, there was a lot of very nicely hand-formatted code in the wild that used different subtle layout choices to make different argument lists look nice. In order to persuade people to adopt the formatter at all, it had to be sophisticated enough to figure out many of those patterns and apply them automatically.

It's not as good as a human (mainly because it doesn't have semantic context) but it had to be pretty close or people wouldn't have tried it.

Now that it's well established, I think it would probably be possible to simplify how it formats while still making users happy. Possibly happier because the results would be a little easier to predict.

> 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.

An automated formatter will never be as good as carefully crafted artisanal formatting. In particular, automated formatters don't know what stuff means. A good human might choose to line break a function call like so:

    setColor(red: 123, green: 54, blue: 26,
        alpha: 45);
Because they know that "RGB" is a single coherent concept and alpha is less closely related. An automated formatter doesn't (and probably shouldn't) have that domain knowledge.

But the value proposition of automated formatting is not just "how nice is the resulting code to read". You have to look at the total value proposition of completely yielding formatting to a tool versus allowing human control over it. When it's completely automated:

1. You can run it on generated code that contains absolutely no whitespace and still get nice output.

2. Humans can do large-scale refactorings, format, and get output that is consistent with the existing state of the codebase without having to understand any local style preferences.

3. Humans never have to spend time deciding how to format. Further, they don't even have to spend time deciding if they should format.

4. When reading a random codebase, it is likely to be formatted in a style you are used to even if you have zero communication with that team. This is particularly important in open source.

5. The code looks familiar to you wherever you encounter it: IDEs, plain text editors, code review tools, blog posts, StackOverflow answers. As opposed to letting everyone pick their own style and relying on users to apply their preferred style locally, it's just always in a familiar style.

6. Like any automation, the tool doesn't make mistakes. Even very careful humans hand-formatting make more mistakes than they realize. (I know because I've looked at their code). Those mistakes can be distracting for readers.

7. It gets people out of the mindset of being nitpicky about style. It encourages them to stay focused on the structure and naming of their code, which is what really matters.

8. It eliminates style arguments in code reviews. Those take time and, worse, cause disharmony, for next to no benefit.

I think it's very worth excepting some small loss of overall formatting quality to get those in return.