16 ms·
EditorConfig: Consistent coding styles across various editors and IDEs
- petetnt 8y agoThese 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.
- smt88 8y agoMy frustration with editorconfig files is that they don't have nearly enough breadth of config options to create close-to-identical setups for everyone on the team. The options are shallow compared to all the ways a modern formatter (like WebStorm's) can differ between users. It seems like CLI tools built for a specific language are the best solution as of now.
- eibrahim 8y agoThis is really old, so I am curious why is it on HN front page? Am I missing something here?
- tyrust 8y agoPeople occasionally repost content on forums. Usually it's because the OP hasn't seen it before and thought it would be good to share. As long as it's not excessive it can show a new bunch of people something they might not have seen before. e.g. I've been on HN for a few years and have never heard of this.
- eibrahim 8y agoCool. I thought there was a new features or release. It’s a great tool. I use it VIM and VSCode
- fluxsauce 8y agoEditorConfig is fantastic. I've used it for years now across client projects, internal work, and even in educational content that I've authored. It honestly doesn't do a lot, but what it does do is standardize on things that are easily agreed upon like line endings and tab style / width. It leaves language specific options to purpose built linters.
- smt88 8y agoBut if the language-based linter is also an automatic formatter, what's the benefit of editorconfig? All mainstream languages have good formatters these days that go much deeper and understand the language.
- greglindahl 8y agoI use editorconfig for a lot of miscellaneous file types that I don't have an autoformatter for: markdown, jsonl, ...
- guhidalg 8y agoIsn’t the advantage that EditorConfig allows you to choose a style over the style enforced by the language? For new languages and markup files this isn’t a problem because they may have developed a style guide in conjunction with the language but older languages don’t have that benefit.
- smt88 8y agoPossibly, but I don't work with any teams that use any languages that don't have their own formatters, so I don't even know which languages are still missing one. The languages they're using are C#, TypeScript, JavaScript, Go, Python, and Ruby.
- deleted 8y ago[deleted]
- nikeee 8y agoDo languages other than C# support formatter/convention configuration via EditorConfig? Or is C# going some other way here? Ref: https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2017 https://docs.microsoft.com/en-us/visualstudio/ide/editorconf...
- DaiPlusPlus 8y agoIt’s not a language feature but an editor feature. Editors often have built-in syntax-aware linters and refornatters that support additional rules you can put inside editorconfig files. Visual Studio has built-in support for editorconfig and also supplies C#-specific formatting rules, hence why the web-page you linked-to exists.
- mattnewport 8y agoI was looking at this just the other day not realizing it was something other than just a Microsoft thing. There didn't seem to be anything to deal with auto wrapping long lines by breaking them in sensible ways which is one of my favorite things about clang-format for C++. ReSharper does it for C# but doesn't do a very good job in my experience and ReSharper has such abysmal performance in Visual Studio that I've had to turn it off despite it having some useful features. The ability to smart wrap lines is something I haven't found a substitute for yet for C#.
- DaiPlusPlus 8y agoI use a 40-inch 4K monitor running at 96dpi so I have evolved past the need to wrap long lines. Lines that are “too long” simply don’t exist!
- mattnewport 8y agoI use a 43" 4K monitor as my main monitor and a 27" 1440p in portrait as my secondary. I typically have the Unity editor on my main monitor though and C# code on the secondary and in that arrangement with the font at a size I find comfortable I still need to wrap at around 120 columns. The other arrangement I sometimes use is two side by side windows on the 4K monitor and even that still has a width limit. C# is pretty bad in this regard compared to C++ because the standard formatting rules of indenting namespaces and the fact that there are no free functions wastes a lot of the left hand side most of the time.
- Jonanin 8y agoPrevious discussions: https://news.ycombinator.com/item?id=14816427 https://news.ycombinator.com/item?id=14816427 https://news.ycombinator.com/item?id=8462825 https://news.ycombinator.com/item?id=8462825 https://news.ycombinator.com/item?id=10531788 https://news.ycombinator.com/item?id=10531788
- Sir_Cmpwn 8y agoAdded support for this to git.sr.ht a while ago. It's used to set your preferred tab width and to show a ruler at the max line limit (which default to 8 and 80 columns respectively if you don't have an editorconfig file).
- z3t4 8y agoThe editor can detect all of these for each file. If the editor doesn't, it probably don't support editorconfig either.
- daxelrod 8y agoEditorconfig’s main benefit for me is the case when a contributor adds new files.
- z3t4 8y agoThe settings can be derived from other files in the repo
- daxelrod 8y agoDo you know of an editor plugin that does this reliably? I’ve tried a few in vim that end up being more trouble than they’re worth. The hurestics can get tricky, especially when different file types have different kinds of indentation (got Makefiles in your otherwise two-space indented project?) or in projects that include vendored dependencies.
- z3t4 8y agoI dont know of such a plugin. But Im working on a editor that doesn't allow you to edit indentation. The trickiest part is for the editor to know which parts of the file it should touch when you for example insert a code block bracket. And should the diff in SCM now include the whole block, or just the lines where you inserted { and }. And also languages like Python where white space matters.
- runeb 8y agoIMO there should be two configurations for code style. One for files and one for presentation. That way any coder can view and edit files to their own preferences.
- iad 8y agoThis already exists when tabs are used for indentation.
- ken 8y agoIt's almost like plain text (with primitive ASCII art arrows and such) isn't a great user interface, even for developers! We should come up with some way of separating the Model and View... :-)
- the8472 8y agoWe have not even exhausted the possibilities of augmented text. IntelliJ at least annotates callee argument names and inferred types. For rust people are tinkering on borrowchecker region analysis annotations. I'm sure there's more that could be done.
- ysleepy 8y agoJetBrains MPS is such an example: https://www.jetbrains.com/mps/ https://www.jetbrains.com/mps/
- andreineculau 8y agoYou can already do that today if your editor implements a prettify-symbols-mode https://marketplace.visualstudio.com/items?itemName=siegebell.prettify-symbols-mode https://marketplace.visualstudio.com/items?itemName=siegebel... You can let editorconfig/linters/formatters to take care of code style, and prettify-symbols-mode of your preferred presentation, which should be stable as well given a stable code style.
- 0db532a0 8y agoCall me a despot, but I am of the opinion that everyone should use the same development environment in a given team. That means same editor, same style and same OS. People might complain in the beginning if they have to change from another environment, but that will be short-lived, and the cost will be minimal compared to the otherwise continuing wankery and bickering over each developer’s subjective opinions on code style and editor. Why? The manager says so. It’s so simple at that point.
- ginko 8y agoI agree with you if that editor is Emacs and the OS is Linux.
- hobofan 8y ago> Linux Linux is not a OS, it's a kernel. You would also have to force all developers onto the same distribution.
- johnmaguire2013 8y agoYes, Gentoo.
- naniwaduni 8y agois Emacs as OS on top of the Linux kernel a GNU/Linux?
- joseph8th 8y agoBeat me to it! LOL
- 0db532a0 8y agoI agree with you, but I am not the manager.
- dbdjfjrjvebd 8y agoDon't you mean that OS is Emacs and the editor is evil... :)
- joseph8th 8y agoI added an .editorconfig to every project at work. As lead it's my perogative to enforce standards. Personally I'm not super picky. If it's not mangled I have no opinion on formatting or editor choice of any dev on the team, except: tab inserts spaces, no auto-formatting an entire file allowed, and the editor must be editorconfig compatible.
- fastball 8y agoWhat's wrong with using a linter to format a file automatically?
- jpollock 8y agoIt kills the history in the version control system.
- dragonshed 8y agoThis is true only if the formatter is introduced mid-project. Additionally, for git, it only messes up git-blame, you can still traverse the history and see changes prior to an autoformatting change.
- acdha 8y agoI’m mixed on this if you use tools like GitHub or gitlab where it’s fairly easy to navigate the history so my rule is picking a quiet time to autoformat (i.e. coordinating open branches) and then marking the commit clearly as having no other impact so people can avoid reviewing it line by line.
- hashhar 8y agoGolang has solves this problem amazingly well. I only focus on writing code. Everything else gofmt takes care of. The best part is that since all Golang code is formatted the same way its quite easy to see if something is messed up simply by the code structure.
- jchook 8y agoEditorConfig should be called CodeConformity. The project doesn’t actually aim to provide consistent editor config. They only want to control code formatting. The maintainers refuse to implement certain configs on principle, such as soft wrap.
- wincent 8y agoAt my job we use tabs, but elsewhere I've always used spaces. Rather than installing yet another plug-in, I use a Vim autocmd that checks to see whether I'm in a company repo, and if I am, it applies some overrides. Links: - Autocmd that runs on relevant filetypes: https://github.com/wincent/wincent/blob/db33e22c1/roles/dotfiles/files/.vim/plugin/autocmds.vim#L61 https://github.com/wincent/wincent/blob/db33e22c1/roles/dotf... - Function that detects a work repo: https://github.com/wincent/wincent/blob/db33e22c1/roles/dotfiles/files/.vim/autoload/wincent/liferay.vim#L5-L26 https://github.com/wincent/wincent/blob/db33e22c1/roles/dotf... - Function that finds nearest Git directory: https://github.com/wincent/wincent/blob/db33e22c1/roles/dotfiles/files/.vim/autoload/wincent/git.vim#L1-L15 https://github.com/wincent/wincent/blob/db33e22c1/roles/dotf... - Function that applies overrides: https://github.com/wincent/wincent/blob/db33e22c1/roles/dotfiles/files/.vim/autoload/wincent/autocmds.vim#L182-L202 https://github.com/wincent/wincent/blob/db33e22c1/roles/dotf... (Disclaimer: like most things in my dotfiles repo, overengineering level 99.)
- mruts 8y agoAm I the only who thinks that vimscript is truly awful and much inferior to Elisp?
- TeMPOraL 8y agoAs much as I like Elisp - does this matter in this context?
- mruts 8y agoI guess not. Just a thought I suppose.
- wincent 8y agoHaving written quite a bit of vimscript, I can confirm for you its true awfulness.
- grandep 8y agoWhat makes you say this? I don't necessarily _love_ Vimscript but it's a fairly straightforward imperative language. I also never understood why so many people rave about Elisp.
- mruts 8y agoHow can you release something like this with no emacs or vim support?
- kemitchell 8y agoThere is both Emacs and Vim support. Scroll down to "Download a Plugin".
- mruts 8y agoFair enough. I suck.
- egypturnash 8y agoI really love the hand-drawn versions of all the icons for the editors this works with! They add a nice note of playfulness to the whole thing.
- zck 8y agoI added EditorConfig to a project recently, and was super disappointed with the scarcity of things I could actually configure. I thought there would be many, many config settings. But I was wrong -- there are only eight (https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties https://github.com/editorconfig/editorconfig/wiki/EditorConf...), with one of them listed as " Supported By A Limited Number of Editors", and the editor I was using (Intellij) is listed as supporting it, but does not. I wanted to configure a lot of things, like "braces at the end of the line, not a new line by itself", or "always use braces, even in a single-statement branch", or even "one space after 'if' before the paren". None of these are configurable with EditorConfig. It's pretty disappointing.
- confounded 8y agoYou probably want a code-formatter (perhaps on a git hook). Trailing white space, fill-column width, and tabs vs. spaces are really all I’d expect from cross editor config.
- vmasto 8y agoIntelliJ supports editorconfig just fine. I'm not sure why you were disappointed, editorconfig does not advertise the features you mentioned. For more involved autoformatting you need some kind of tool like gofmt or Prettier for JavaScript.
- zck 8y ago> IntelliJ supports editorconfig just fine. When I set the line length config setting, and then have intellij autoformat a file, it doesn't break lines at that length. > I'm not sure why you were disappointed, editorconfig does not advertise the features you mentioned. For more involved autoformatting you need some kind of tool like gofmt or Prettier for JavaScript. The elevator pitch for EditorConfig, as taken from the first sentence of its website, is "EditorConfig helps maintain consistent coding styles for multiple developers working on the same project across various editors and IDEs." The things I want I consider coding styles.
- deleted 8y ago[deleted]