8 ms·
These days I vastly prefer autoformatters, only run at precommit stage myself. Instead of having to adapt to different styles everytime and getting distracted b
by petetnt 8y ago
These days I vastly prefer autoformatters, only run at precommit stage myself. Instead of having to adapt to different styles everytime and getting distracted by stylistic changes mid-coding (eg. on save), I can just rely that the styles are eventually consistent and code ahead. It takes some time getting used to, but it’s super refreshing.
- williamdclt 8y agoPersonally, I rely a lot on autoformatters ran on save. It frees my mind completely from thinking about formatting, I can write ugly one-liners that get immediately beautified and I have an almost-immediate feedback for syntax errors (it doesn't autoformat => I made a syntax error somewhere)
- vjeux 8y agoThis was something I wasn't really expecting when I started working on prettier. It was a strong selling point when people adopted it within Facebook. I still can't believe that it only took a year between the first commit of prettier and 100% of Facebook massive codebase to be completely formatted using prettier. Prettier is also growing extremely quickly in open source. It's now at 3.7 million downloads a week, compared to 6 million for React.
- badfrog 8y agoI didn't get to say this before I quit working at FB, but thank you for building prettier! It really made writing and reviewing JS much less painful.
- zwily 8y agoEven if there were no other advantages to Prettier, I would use it for this. It seems barbaric to write in languages now that don’t have something similar happening on save.
- dorfsmay 8y agoSame here. This is so powerful. I absolutely love python, but this is one thing it doesn't allow. On the other when working in say Rust, I can express my thought in a line or 2 straight from brain to keyboard without having to care about any formatting, hit autoformatter and also typically get an immediate feedback about basic syntax. When writing in a language that can be auto formatted, it is a huge cognitive overload removed from my shoulders, like having a copilot.
- arusahni 8y agoHave you tried any Python autoformatters? `autopep8`, `yapf`, and `Black` come to mind.
- dorfsmay 8y agoYes. They can format correct python to pep8, but can't guess which if an else correspond to if I write it all on one or two lines.
- ambivalence 8y agoThis experience must have been with a formatter different than Black. That one has no problems with any valid syntax.
- tom_ 8y agoI think the point is that the valid python syntax is potentially a limitation on any autoformatting tool. Like, suppose you were writing in C. You might well write this: if(x);else if(y){if(z);else;} and clang-format would turn it into this: if(x) { } else if(y) { if(z) { } else { } } But the equivalent in Python has to be correctly indented to be interpreted properly, because you need to distinguish these two cases: if x: pass if x: pass elif y: elif y: if x: pass if x: pass else: pass else: pass (This is the classic shift/reduce problem: https://www.gnu.org/software/bison/manual/html_node/Shift_002fReduce.html https://www.gnu.org/software/bison/manual/html_node/Shift_00...) This is sort-of not unreasonable, because of course code has to be syntactically correct for the autoformatter to do its thing, and if you're working in Python, these are the syntax rules. So, what can you do? But suppose you'd come to find it valuable to be able to stuff all your code on one line and have the autoformatter sort it out: you might then find the Python syntax a backwards step by comparison, rather than an improvement. I think this is the claim that's being made, and no more.
- kilovoltaire 8y agoAgreed, I now feel like I'm editing the AST instead of editing text, it's quite magical.
- yellowapple 8y agoThat latter point is how I do a lot of my syntax-related bug finding with Emacs: press C-x h Tab, look for where the indentation starts to look unexpectedly wonky, fix the issue that caused it, repeat.
- smt88 8y agoAlso a good policy for naive diff algorithms that don't know whether minor changes are semantic or not (e.g. whitespace in Python vs. PHP)
- joshklein 8y agoI first encountered this when I began working in golang because it was batteries included with go fmt. I know this makes me late to the party, so can anyone comment on similarly dominant/official tools in their language of professional experience? A web search can find tools, but it’s hard to know when one is likely to be “blessed” by potential future project teams.
- etxm 8y agoI use go fmt and editor config together. Gotta tame the white space locally. All them tabs give me whiplash.
- williamdclt 8y agoFor Javascript, ESLint and Prettier are the most used (can be used together) Flake8 + Black for Python do a good job
- pletnes 8y agoBlack is written by python core devs AFAIK (pip install black).
- konschubert 8y agoI love it so much because you cannot configure it much, so it defines a clear standard that everybody can depend on
- ameliaquining 8y agoC, C++, Objective-C: clang-format Java: Probably google-java-format HTML, CSS, JavaScript, TypeScript, Markdown, YAML: Prettier Python: autopep8, yapf, and black are all widely used and I don't think any of them is dominant. I personally favor black, as it's the only one of the three that strives to leave as few degrees of freedom as possible (i.e., the same source code is always formatted the same way). Rust: rustfmt
- sametmax 8y agoBlack seems to be the most popular these days. It's certainly the one you hear the most about and it's getting support by default in vscode and pycharm.
- tonto 8y agoYou might still want this for your editor to properly have indenting information as you are coding though no?
- acdha 8y agoI use both: an increasing percentage of formatters and linters support .editorconfig so you can set the common basics there and not need to duplicate configuration for every tool you use on a project. I’m in very strong agreement on using formatters, too, but I have mine run on-save. It’s slightly disconcerting at first but extremely relieving when you realize how long it’s been since you’ve talked about code style.
- dvlsg 8y agoI agree, I love on-save formatting. Now I can just type code I know is syntactically correct, hit save, and let the formatter worry about the formatting.
- skoskie 8y agoOk, help me understand this. I’m on #TeamTab which puts me at odds with most coding standards. I usually work solo, so I don’t care, but every time I do let someone else edit a file, the commit shows every line has changed because their editor converts my indents. Are you saying there’s a tool that will let me work with (superior) tabs but have standards compliant commits? I’ve tried doing this in my .gitconfig with limited success. It didn’t work consistently.
- sneak 8y agoTabs are an editor feature, and their use in your editor is somewhat unrelated to the way your editor serializes them onto disk. There are configurations for your editor that will allow this, independent of the EditorConfig project.
- RussianCow 8y agoThis isn't true, because lining things up visually works differently with tabs than with spaces. (Although, personally, this is why I never visually align code like that, but that's the standard style in many organizations/languages.)
- tobr 8y agoI will never understand why people get so religious about tabs vs spaces, but this is a hill I’m willing to die on: please don’t line things up visually in your source code, beyond standard indentation.
- int_19h 8y agoThere are some popular coding standards that essentially require it, unfortunately. My opinion on this is that it's a bad idea in the age where we (finally) have near universal acceptance of source control, because they bloat commits with irrelevant changes in surrounding lines to preserve alignment when identifiers change. Black got it exactly right - optimize for readability and change locality.
- jrockway 8y agoI agree that autoformatters are the only way to go. It is nice if your editor kind of gets it close, though, so it's less jarring when things move around after save.
- renox 8y agoBah I read a lot about the niceties of using autoformaters then I had to use clang-format for C++ at work and when I discovered that the format is totally borked when you use macros in lambdas, well.. I prefer manual formatting, yes each coder does it differently but who cares? The result is still readable which is NOT the case when an automatic formatting tool goes nuts..
- h1d 8y agoSo, do you format back again to your style before editing? You can't preview a file without modifying the file.
- shanemhansen 8y agoThis is why I love gofmt. Because I don't care about formats and finally there's a tool to remove the tabs vs spaces debate and allow people to focus on issues of substance.