EditorConfig: Consistent coding styles across various editors and IDEs
editorconfig.org
editorconfig.org
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.
When writing in a language that can be auto formatted, it is a huge cognitive overload removed from my shoulders, like having a copilot.
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_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.
A CL restart-style editor integration for this could be interesting.
Do you have any examples of things which were so bad? The team has been pretty response for the handful of things which I reported.
Black will mindlessly split long lines into shorter, and my coworkers will just leave them as is. The results are eye bleedingly awful.
I hope most black proponents expect people to look at the generated code and change things until it looks decent?
Of course this problem is inherent in a tool that is autoformatting rather than warning, and seems to me to indicate a mentality that code is meant to be read by computers, not humans.
I would also flip your last sentence completely: computers don’t have problems reading code of any style. Auto-formatting is so humans don’t burn time adjusting to different styles or, worse, failing to parse them the same way as a compiler.
The split of the long lines aren't bad, but they are also bad because the long lines are inherently too complex.
Of course some times our broken up lines are too complex, and should ideally be restructured, but...
1. The mechanical way Black does it is rarely the best way to decomplexify things.
2. Often the long line wasn't complex at all. It just contained a simple but long string. Line length is a weak measure of code complexity, but as a dumb script, it's all Black can use.
That said, my main beef is really with the 88 char line length. If it was 120 or 140, I wouldn't like Black either, but I'd only be kinda annoyed. Coupled with 4 char indentations, 88 s an absurdly small space to fit code in, when we have the biggest screens in history. It's an incomprehensible madness to me.
Not sure what your final section says. I hope it's not that humans should learn to read code just like computers do?
To me, one of the most important quotes about our job is this:
“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”
We might change things up and I might be OK with my 'black life after all.
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.
Flake8 + Black for Python do a good job
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
C++, .NET languages, TypeScript, JavaScript: Default Visual Studio/VSC configuration
Java: Eclipse/Netbeans/InteliJ configured for Sun's conventions
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.
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.
i say tabs are superior. the real solution is probably variable width tabstops: http://nickgravgaard.com/elastic-tabstops/
The only editor that really does properly support "just set this option and then it's like you are using tabs" is Atom. But I don't really want to use Atom.
If you use an autoformatter there is no need to manually check for misalignement!
Just type in some code, press a key and everything is aligned, problem solved!
Indentation all fucked up? No problem, press the auto format key, and the computer will unfuck it for you.
Need to indent or unindent a block of code? No problem, press the auto format key. You don't even have to think about whether it needs more indentation, or less. The computer will do that for you.
Do you type all your code on one line without putting any spaces in? Well, don't tell anybody, just press the auto format key, and they will never find out. (I know I just told you not to tell anybody, but: I do this all the time. And it's great.)
In fact, you don't need to really think about any of this whatsoever even at this level of detail, because you can just develop the habit of pressing the reformat key every now and again. Or get the editor to do it every time you save, and then you don't even have to remember to do that.
But, however you do it, this sort of tool reduces to pretty much zero the amount of energy you have to expend on caring about how the code looks.
There are configurations for your editor that will allow this, independent of the EditorConfig project.
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.
If you feel like it’s useful, you should probably be using an editor with real elastic tabstops instead.
You picked the wrong vocation my friend. It's "brittle and fiddly" all the way down.
re "you should probably be using an editor with real elastic tabstops" ... i don't get what you're trying to say. What does real or fake elastic tabstops have to do with the choice to align code beyond just indentation?
I think there’s a chicken-and-egg situation with monospaced type and programming. Programming was a thing before digital proportional typefaces were a thing, so some people came to rely on the fixed spacing to treat their source code as a 2d grid rather than a 1d buffer. This in turn made it more difficult to switch over to proportional typefaces despite their advantages, and at some point code == monospaced was just taken for granted. Another issue that comes from mistaking your code for a 2d mosaic is the dogma of dictating a maximum number of characters per line, instead of the more reasonable approach of using whatever expresses the structure of the program most clearly, and relying on line wrapping to make sure it’s all visible.
FWIW I use proportional fonts, and 95% of the time it’s completely unproblematic. Fortunately I rarely work with code that uses manual alignment. It’s usually the occasional ASCII diagram that forces me to temporarily switch over to fixed pitch.
what's the argument against this? There are many arguments for how it can improve readability and speed up understanding of what you're looking at when you're not familiar with it.
If you are in a system small enough that you only ever have to look at code in a code editor, then this anti-feature won't be a real problem.
If you work in a sufficiently complex environment that displays code across a variety of platforms, tabs will only make working with the code more difficult.
One odd exception is GitHub which inexplicably uses size-8 tabs by default which is way too big. I can only assume that somebody there is a space zealot and wants to screw up the UX for enlightened tab users.
When I have to work on N platforms that are constantly shifting, I don't want to spend the overhead and mental effort to reconfigure the ones that can be reconfigured, and eat nameless dread on the ones that can't.
When you view code across dozens of platforms, across dozens of machines, your perspective on "high maintenance config" everywhere changes a bit.
If you can't be effective with default configs, you lose efficiency to config maintenance.
Really, the last thing I want to deal with while I'm triaging some critical issue is dependency management for N code viewers.
http://blog.gngcreative.com/wp-content/uploads/2015/02/mi0fg...
Those metal things you can see there, those are tabstops. They are called tabstops because when you press the tab key, the carriage mechanically stops at the tabstop.
Tabstops were placed every 8 characters. A tabstop is simply a way to "type" 8 spaces with one keypress.
Github is not the only page that uses 8 spaces for a tab, and it's definitely not inexplicable. The Unix/Linux terminal uses 8 spaces by default if you insert a tab. Vim uses 8 spaces by default if you insert a tab. Python considers a tab to be equivalent to 8 spaces. I'm sure there are a lot of other systems that default to this value.
If you want to control how an indentation looks, use spaces. If you want tabs because the size is configurable and can be adjusted to one's preferences, you can't at the same time complain about "unreasonable configurations" that others use :)
tabstops were _not_ restricted to 8 characters. In many systems different tabstops were different widths from their predecessor. E.g. your first tabstop might be 20 characters in but all the ones after it would be 8 past their predecessor. Alternately you could vary all of them but that'd look pretty weird.
This can be very neat for responsive design: use tabs for your indentation, and then on large screens use `tab-size: 4` because you’ve got space to spare and four is generally superior to two, and on narrow screens use `tab-size: 2` because you don’t have as much width to play with.
root = true
[*]
indent_style = tab
indent_size = 2
That being said, GitHub's default setting can only be seen as a spaces mongering ploy to subvert the true usage of tabs.The only arguments I hear for spaces are alignment (I don't have time to give a shit about such minor aesthetic preferences) and forcing other people to see code a certain way (I really don't understand why I would want to force my editing preferences on others). I don't think either one of them justifies the annoyance of spaces.
This is all pretty pretty straightforward to configure on a per-repo basis too, so you can hop around multiple projects with different standards and not have to think too much about this.
In a perfect world each language has exactly one (non-configurable) formatter and this solves all the formatting discussions.
For JS I use prettier and altough I am not 100% happy with some of the formatting choices I _love_ it because I can focus on code and not have flamewars about tabs vs spaces with my co-workers. prettier ends all style discussions. And code reviews are much nicer because people concentrate on checking the code, not the formatting.
As a concrete example (for JS projects), you could configure Prettier to use tabs for indentation:
https://prettier.io/docs/en/options.html#tabs
Then you could use eslint-plugin-prettier, which fails lint if the code isn't Prettier-formatted and makes ESLint autofix also run Prettier:
https://github.com/prettier/eslint-plugin-prettier
Then set up lint to run in CI alongside tests.
Yes. It's an autoformatter and it will make you never care again about tabs and spaces and other unimportant issues us devs love to bicker about. Focus on the higher level stuff instead :)
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..
You can't preview a file without modifying the file.
It seems like CLI tools built for a specific language are the best solution as of now.
Links:
- Autocmd that runs on relevant filetypes: https://github.com/wincent/wincent/blob/db33e22c1/roles/dotf...
- Function that detects a work repo: https://github.com/wincent/wincent/blob/db33e22c1/roles/dotf...
- Function that finds nearest Git directory: https://github.com/wincent/wincent/blob/db33e22c1/roles/dotf...
- Function that applies overrides: https://github.com/wincent/wincent/blob/db33e22c1/roles/dotf...
(Disclaimer: like most things in my dotfiles repo, overengineering level 99.)
From your comment, it sounds like you’ve never used emacs/elisp. Why don’t you give it a try? With evil-mode, you don’t even have to give up your vim keybindings! Emacs is the most configurable and powerful editor in existence, and that’s not an opinion, it’s a fact.
PS: I actually use Intellij for my day to day scala work, but emacs is still the best (given enough time to configure/program the way you want).
Even after using Emacs + Elisp for all that time, I never experienced that transcendental moment that so many fans of Lisp dialects seem to share.
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.
It disables the commenting interface on PRs however :(
The problems we've had are all because we can't get consensus to auto-format, but then someone gets frustrated because they can't understand the code they're reading after someone committed mortal indentation sins, so then they auto-format and bam, history is worthless again.
Mix format
Go fmt
Prettier
Etc
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.
All mainstream languages have good formatters these days that go much deeper and understand the language.
The languages they're using are C#, TypeScript, JavaScript, Go, Python, and Ruby.
Ref: https://docs.microsoft.com/en-us/visualstudio/ide/editorconf...
Visual Studio has built-in support for editorconfig and also supplies C#-specific formatting rules, hence why the web-page you linked-to exists.
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.
I think it's enabled by default now.
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.
Trailing white space, fill-column width, and tabs vs. spaces are really all I’d expect from cross editor config.
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.
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.
I'm sure there's more that could be done.
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.
e.g. I've been on HN for a few years and have never heard of this.
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.
Linux is not a OS, it's a kernel. You would also have to force all developers onto the same distribution.
Edit: Obviously this doesn't apply if you do platform-specific work (like iOS development) or rely on tools that are only available on a single platform; in that case, you're probably right.
If people are using different environments, people are still able to wank and bicker over differences in environment.
By the way, it’s not just about code style and editor. By sharing the same environment, people can share build tools and other tools much more easily.
How far would you take it? Would you (like Steve Jobs) try to get everyone to dress the same, too?
As Wikipedia says: "Modern uniforms are most often worn by armed forces and paramilitary organizations such as police, emergency services, security guards, in some workplaces and schools and by inmates in prisons."
One of these things is not like the others. Is running your professional software team like a paramilitary group or prison the only way you can get them to stop "bickering"? For people who are immature enough to bicker about any difference between individuals, when you enforce the standardization of one attribute, they just switch to something else.
I don't think that running a software team like a paramilitary group is the only way to stop bickering, but some elements of how paramilitary groups are managed are very attractive in this context, and seem to have worked where I saw them applied.
The employees of a lot of banks happen to wear suits, shirts and trousers, programmers included. I wouldn't discount the benefit of this purely because paramilitary organisations also happen to enforce a uniform.
http://www.lel.ed.ac.uk/~gpullum/passive_loathing.pdf esp 3 (25)
You also overvalue uniformity. Or under value the antifragility of not caring how the code was created. The most fragile systems I have ever had the displeasure of being near, required a very specific build machine because the environment couldn't be replicated. In large, because the original development team did what you propose, and mandated all development happen using the same controlled environment.
Yes, a team obviously has to agree on obvious things such as language and build system. However, cornering yourself into such a tight environment leaves a lot of opportunities to have the rug get pulled out from under you.
I currently work on a team that writes DAQ software in C++ for Windows. The developer has their choice in everything (editor, VCS front-end, debugging and profiling, etc.) as long as the build system runs, their code meets the team's code standards, and they're productive with it. We've found that we can basically close our eyes, point at a random target system, and odds are that our code base can be trivially ported to that target with minimal effort. That reality is possible because somebody has incidentally tested just about everything. At one point in the past, we had a pretty narrow/rigid build process, and it bit us in the ass and hamstrung more developers than it was worth.
C/C++ are unique in that build processes and integrating other people's code in them are inordinately hard problems in and of themselves. Different compilers will behave a little differently, different operating systems will behave a little differently, and some of the niceties that a certain IDE or tool provide you will behave a little differently, depending on the exact circumstances you're dealing with. The safe thing to do is make no environment assumptions, and try to make everything as portable between environments as possible. You need to have your focuses, but it's reassuring to know that you can jump ship if and when you need to.
You still need solid testing for making sure you don't miss regressions. So, we aren't claiming this is a replacement for any solid engineering strategies.
And imo autoformatters are too inflexible. Most default rules are ok when you don't make any effort about formatting at all but break down when you manually lay out things for improved readibility. E.g. when splitting arguments to one arg per line and parens on separate lines and the autoformatter will just go and squash them all together into single lines up to the line limit, except the lambda in the middle which will be broken out into a separate line. Or other fun things like that.
Formatters which rearrange code by alphabetical sorting or sorting by visibility are even worse. They can introduce so much commit noise just because you change an identifier.
the sorting causes more problems than its very questionable worth.
I don't, because I've worked in both kinds of environments and have not seen any benefits to the system you propose. Uniform development machines doesn't automatically mean that you've solved all consistency problems. Also, I'm not sure what you mean by "build tools" specifically, but any software needed for a project to run should ideally live within a Vagrant VM (or Docker or whatever), so it should be self-contained and not require any installation of additional software.
> How did you pick up a programming language and editor in the first place? How did you pick up a new one? It was fine.
IME, it takes several months to really become fluent and productive in a new editor, and potentially longer for a new language (depending on previous experience, etc). So "it's fine" as long as you consider a multi-month learning curve "fine".
I'm interested to know what editors you have switched to/from and why. Example, switched from emacs to vi because manager forced us to, etc.
What remains is editor/IDE. I’m still of the opinion that everyone should use the same development environment, preferably a full-fledged IDE which enforces code style at write time. This also prevents the bad diff issue, amongst a lot of the more important problems I’ve explained elsewhere.
Of course it’s all a matter of opinion. I base this opinion on how I experienced this environment. Nowhere else I’ve worked managed to get so much done and collaborate so easily.
I wonder whether this is actually a sort of class issue. The dissonance between being part of god's chosen tribe and the realisation of being decidedly middle class all the while. It manifests itself by turning people into Emacs emos. It's not a phase, I am unique, and I'll run away from home if you tell me otherwise.
It felt absurd that if we aren't all doing the same job then surely we're not on the same team
I would choose the benefit of uniformity over my personal preferences any day. You don’t know until you’ve seen it.
Those haven't been problems for the teams I've been on, and we've always been "every man for himself" when it comes to dev environment. As long as code follows the style guide, passes tests and gets through a code review, nobody cares about the dev's environment. Did you try that and it didn't work? What led to management mandating conformity?
I have never before or since worked at a company that allowed "every man for himself" while managing to enforce the discipline required to maintain a uniform style guide, tests always passing and code review. It was nice to go over to a colleague's computer and see exactly the same code as I see on my own, and in the same environment. Everyone used the same debugger interface and same unit test interface, because everyone used the same IDE. They become more natural to use. People were able to refactor easily and maintain consistent naming standards, which is more difficult in Emacs, and especially in vim.
Most of my friends and coworkers use DeWalt, while some use Milwaukee, and there's even a few oddballs who use Makita, but I've never heard any 'bickering' about it.
Drill bits are standard, but lots of other tool interfaces are not. Batteries are not at all compatible, for example, and not even within the same brand (different voltage, different battery chemistry, ...). When someone's Milwaukee 18V lithium battery dies, and there's only a DeWalt 20V charger, nobody makes snide remarks about brand, any more than if you had the wrong size wrench for the bolt you needed to tighten.
It's true that "drills are not political", so why would you allow developers to claim that text editors are? That is a social problem, so it requires a social solution. You seem to keep implying that software developers uniquely posses an inability to behave reasonably in the presence of alternatives.
The OS used for development is, in many cases, also the OS used for routine debugging. An OS monoculture on a cross-platform product team means that some product gets a lot more incidental testing than the others.
But frankly, your original point doesn't make sense to me, because there's simply no downside, so why argue about upsides? The team can agree on a single code style (or have that mandated from above), but how does that affect editors, and especially OSes? The only meaningful constraint is that one's choices must be able to handle the mandated style.
Same thing for other stuff. For example, it makes sense to mandate a testing framework. But if multiple IDEs support that framework, what's the point of forcing the choice on that?
So yes, the objections will be shortlived because anyone good enough to have options that don't like your choices will choose not to work in your team.
I’ve spent a lot of time setting up my dev environment so it works well for me, and I’m more productive and happy in it.
Style/formatting consistency is one thing, but editor uniformity is an unrelated thing. Yes, it makes collaborating easier, but is that worth the cost of people being less happy or less productive?
I don’t dislike other editors, but I like my setup a lot, and there are, imo, pretty huge switching costs involved in switching to/from vim/emacs and a GUI editor.
That attitude is the quickest way to lose my respect.