javascript icon indicating copy to clipboard operation
javascript copied to clipboard

What is the benefit of prefer-default-export?

Open CWSpear opened this issue 9 years ago • 167 comments

The docs don't have a Why section for prefer-default-export, and I'm not seeing the benefit of it on my own. I would think that not using default is preferred. With default exports, you lose refactoring power (if you rename the source const/function/class, it won't rename default imports).

As more of an edge case: it makes code less future-proof. i.e. if you create a file that will be a collection of errors, but it only starts with one error, to follow the linting rules, you'll have to have it export default, but then when you add the 2nd error at a later time, you'll have to do a bunch of refactoring that could have been prevented if the recommendation was to avoid default export.

CWSpear avatar Apr 10 '17 17:04 CWSpear

Discoverability is very poor for default exports. You cannot explore a module with intellisense to see if it has a default export or not.

Here import { /* */ } from 'something' you get autocomplete. Here import /**/ from 'something' you get no autocomplete. Perhaps it has a default export / perhaps it doesn't. Autocomplete doesn't make choices on what you want to call something only on what is available :rose:

If it is still unclear:

const /* no autocomplete */ = {foo:123};
const { /* you get autocomplete */ } = {foo:123};

Not an IDE weakness. Syntax weakness :rose:

basarat avatar Aug 23 '18 01:08 basarat

This is just nonsense; a CJS module only has a default export conceptually, despite babel's interop, and the discussion is about authoring a module, not consuming on

You might not have felt that pain. I and many people have :rose:

basarat avatar Aug 23 '18 01:08 basarat

That's not a typo, that's just people naming variables differently, which is a) fine, and b) can be enforced by a linter if that's something you want to do.

I don't like forcing people to rethinking an already named variable. A variable is a concept that should be noun-infied and called the same thing consistently :rose:

basarat avatar Aug 23 '18 01:08 basarat

Tools like importjs work identically with named and default imports, so this is just false on its face.

Not TypeScript. My notes are on TypeScript. Although a quick search on import-js does reveal : https://github.com/Galooshi/import-js/issues/453. That said first time I am hearing of importjs I am assuming it is import-js (something I just found from a google search :man_shrugging: ) :rose:

basarat avatar Aug 23 '18 01:08 basarat

I wouldn't recommend re-exporting in the first place, but export { default } from 'foo'

When you create a module library you need to :rose:

but export { default } from 'foo' as an extra step really doesn't seem that hard to me

You cannot have two of those in the same file. The most common use of re-exporting is to roll multiple files in one :rose:

basarat avatar Aug 23 '18 01:08 basarat

@ljharb Alright, that answers all the points you have raised. I've replied separately so you can discuss separately :heart:

basarat avatar Aug 23 '18 01:08 basarat

@basarat The const /* no autocomplete */ = { foo: 123 } example doesn't really correspond with default exports at all as export default is explicitly defined within modules whereas objects may be anonymous structures.

Note that export default const x = 12 isn't valid, export default takes an expression (although there's some special-casing for functions/classes to also declare them in the file). The only real name you can give to a default export is the filename itself.

If an editor doesn't support renaming exports from import myFunction from './path/to/myFunction.js' to import differentFunction from './path/to/differentFunction.js' then that editor should support that. It's as easy to write as any of the named export ones so why the editors haven't done so is beyond me.

Jamesernator avatar Aug 23 '18 01:08 Jamesernator

I love how the battle still ensues. In all seriousness though, what kind of person do you have to be to intentionally assign different names when importing your modules? Stick to the same name, and if needed, don't export anonymous functions or values - declare them with a meaningful identifier, export default at the end of file, and import using that same identifier. If you need to change the name globally, it's as easy as Ctrl+Shift+H. I find it foolish to not use certain features of JS because of fashion or danger. Why not leverage both with sensible discretion?

alex996 avatar Aug 23 '18 01:08 alex996

@alex996 One example might be if you have operators that differ per thing they operate on e.g.:

import mapObservable from 'rxjs/operators/map.js'
import mapIterable from 'some-itertools/map.js'

Another might be common convention e.g.:

import $ from './jquery.js'

Sure it's not super common, but renaming does happen intentionally and there's no reason to assume it's a mistake. Better than an editor tool would just be code reviews that ensure people are doing things sensibly regardless of whether they're renaming default exports or not.

Jamesernator avatar Aug 23 '18 01:08 Jamesernator

Regarding https://github.com/airbnb/javascript/issues/1365#issuecomment-415236807: conceptually, default exports are what a module is, named exports are what a module has. This is a core language concept.

Re https://github.com/airbnb/javascript/issues/1365#issuecomment-415237317: have you used importjs? It in fact works with both, just as you'd expect.

Re https://github.com/airbnb/javascript/issues/1365#issuecomment-415237619: then presumably you never allow anyone to name their own variables so you can think for them? Local bindings are chosen for the convenience of the author.

Re https://github.com/airbnb/javascript/issues/1365#issuecomment-415238114, importjs works fine with typescript as far as i know; but the existence of bugs doesn't change that this is a tooling concern, not a code authoring concern. If your tools' lack of capability is forcing you to write code differently, that's a reason not to use that tool.

Re https://github.com/airbnb/javascript/issues/1365#issuecomment-415238455, no, you do not. Peo-ple can (and should) deep import from specific paths, not lazily import everything from a manifest, forcing reliance on treeshaking to partially clean up after yourself. Separately, you can export { default as foo } from 'path', so I'm not sure what you're referring to.

@basarat Also, please don't comment multiple times - each time generating email spam for hundreds of people, each time with a somewhat passive aggressive emoji at the end. Let's have a productive discussion, or none at all.

@alex996 "what kind of a person do you have to be" is needlessly insulting; please refrain from such language. I do this all the time - I name things based on how i intend to use them in the module, not based on what the author of that code arbitrarily chose. Often, these are the same, but that doesn't mean that my intuition must match the author's.

Why not leverage both with sensible discretion?

Absolutely I agree with this. I'll repeat again, in bold for emphasis: A default export is what a module is, named exports are what a module has. It is a mistake to discard a core language concept and a powerful means of expressiveness, especially because of unwarranted fears and inaccurate information.

if the tone on this thread gets toxic, i will lock it. I'd prefer not to do that.

ljharb avatar Aug 23 '18 02:08 ljharb

@ljharb

This means intellisense is broken, not default exports. A competent IDE understands what is at this point a 3+ year old standard.

Then please tell me which IDE's do understand this...

dietergeerts avatar Aug 23 '18 06:08 dietergeerts

@dietergeerts I'm afraid I can't, as I prefer an editor to an IDE. However, there's a bunch of conversation farther upthread - in which you were directly involved - that addresses why it shouldn't be any different for named vs default exports.

ljharb avatar Aug 23 '18 06:08 ljharb

@ljharb I get the why and when of default exports, and the concepts. This is the only rule I currently overwrite because both Visual Studio Code and WebStorm/IntelliJ IDEA can't auto-import things with it, and the auto-import gives us a massive productivity boost.

For packages with default export, I create live templates, so I can import them fast and easily, like for Lodash and RxJS, which have exports named the same ;), and that's where the default exports of Lodash are really great, so I can get:

import _map from 'lodash/map';
import { map } from 'rxjs/operators/map';

So not against default exports, it's really useful in libraries like Lodash, but for most projects, it's better not to use them due to the productivity boost and developer experience.

dietergeerts avatar Aug 23 '18 06:08 dietergeerts

I've subscribed to this thread so that I can discover and understand scenarios where named exports were found to be better than default, and vice-versa. Hope we can maintain that spirit in this thread.

Any suggestion that one approach should never be used has my automatic downvote. Maybe you didn't recognize its use case or value yet, or using the other approach has so far been sufficient for your team. But before banning it altogether, maybe you should consider that language designers have probably thought a lot about it too, perhaps more than you, and included it for good reason.

If you go by the original design goals, default exports were intended to be the "favored" approach. The whole thing was discussed in depth before finalizing the ES6 modules spec. Note the point (from linked discussion) relevant to this topic:

The syntax should still favor default import. ES6 favors the single/default export style, and gives the sweetest syntax to importing the default. Importing named exports can and even should be slightly less concise.

My thumb rule based on a few years of using ES6 modules is that I'd mostly prefer default for exporting classes, and named for exporting a bunch of related functions/constants (instead of a class full of static methods). But I always decide afresh, even if it usually takes only a few seconds, what makes sense for the module that I'm about to create.

As far as the prefer-default-export rule is concerned, I have kept it for now, to serve as a reminder. And I don't mind disabling it inline where I am sure a module should have named exports starting with only one today.

gaurav7h4p4 avatar Aug 26 '18 11:08 gaurav7h4p4

It's sadly a style I've seen too much to export an object with all of the "named" exports as members. This then breaks tree-shaking (for obvious reasons), where as you can tree-shake if you export them all separately.

Isn't this the problem? If developers are not aware of how treeshaking works, the temptation to resolve the lint warning when exporting multiple functions from within one file is to export a single object with all the named exports as members. This makes the linting error go away, but it prevents treeshaking, so it's actually encouraging a practice that could lead to poorer performance. Surely the treeshaking argument alone is enough to encourage all exports to be named exports?

citypaul avatar Oct 29 '18 12:10 citypaul

if the tone on this thread gets toxic, i will lock it. I'd prefer not to do that.

I read it - if the arguments are against mine, I will lock the thread. Everyone is saying "hey it's a bad idea" and you keep repeating "lalalala you're all stupid and my idea is better because it's mine." And what I infer is - "we can't change the rule because the entire codebase is already messed up and fixing it would require no less than 365 man days."

Hermholtz avatar Oct 31 '18 22:10 Hermholtz

@chojrak11 not at all; there's tons of arguments against our position on the thread already.

We could change the rule and codemod it in about 5 minutes - that's not the issue at all. It's a worse mental/conceptual model and would make maintaining and reasoning about our code harder - it's not that it's hard to achieve.

ljharb avatar Oct 31 '18 22:10 ljharb

a worse mental/conceptual model

This is why I wrote what I wrote. Saying "worse" is just your opinion, which is supported by theories that most of the people chose not to acknowledge. I'm glad I don't have to follow these rules, as I think the default export is worse mental/conceptual model rooted in one-class-per-file philosophy from Java, which I also consider a failure.

Hermholtz avatar Oct 31 '18 23:10 Hermholtz

@ljharb

@ljharb I get the why and when of default exports, and the concepts. This is the only rule I currently overwrite because both Visual Studio Code and WebStorm/IntelliJ IDEA can't auto-import things with it, and the auto-import gives us a massive productivity boost.

Created feature request for such autocomplete. You can support it below with thumb up Autocomplete of export default

VRuzhentsov avatar Nov 13 '18 16:11 VRuzhentsov

Today I read an article that sums up nice why I still overwrite this rule, while in our team, the decision is to ONLY overwrite Airbnb rules when there is a clear benefit for dev and avoiding bugs. https://humanwhocodes.com/blog/2019/01/stop-using-default-exports-javascript-module/

dietergeerts avatar Jan 19 '19 14:01 dietergeerts