r/javascript • u/philnash • Aug 27 '18
`export default` considered harmful
https://basarat.gitbooks.io/typescript/docs/tips/defaultIsBad.html32
u/dominic_rj23 Aug 27 '18
I am not sure if I like the idea of changing coding style just so that tools works with it.
5
u/villiger2 Aug 27 '18
I think of it the other way around. If I'm doing a project and the tooling better supports one way with no obvious trade-odds, why not do it that way?
Specifically on this issue I don't see the benefits of default exports, tooling aside.
0
25
u/ronniegeriis Aug 27 '18
Honestly, the title should be changed. I could imagine a bunch of inexperienced developers getting confused and blindly becoming opposed to default exports, because they read they're harmful without actually understanding that it's an opinionated take.
32
u/yopla Aug 27 '18
Time for the next article:
> considered harmful considered harmful
7
3
1
u/forthelose Aug 27 '18
It's been around, just a bit older: https://meyerweb.com/eric/comment/chech.html
1
29
u/ryansmith94 Aug 27 '18
Interesting post, good to hear other people's opinions on this. Personally felt the "considered harmful" title was a little over the top, but I do use export default almost exclusively in TypeScript, unless I'm exporting interfaces or constants.
Like you, the CommonJS interop is a concern of mine, but with TS I'm compiling it down to ES5 so it's not an issue. The remainder of your issues seem to boil down to the need for additional syntax in places and poor tooling, not a lot you can do about the syntax at this point, but VS Code provides great tooling that's improving all the time, especially around the usage of default exports.
Whilst I don't agree with the title and don't think this post should put anyone off using default exports, some of the concerns here are certainly good to consider for people using default exports, so nice work đ
29
Aug 27 '18
Personally felt the "considered harmful" title was a little over the top
It's become a meme by this point
1
0
1
u/bheklilr Aug 27 '18
I'm also writing exclusively in typescript, and I don't use default exports at all. It's mostly a personal preference thing though, although I do like the slightly better refactoring capabilities. I also am usually exporting multiple values from a module, having a default export doesn't make much sense.
As an example, if I'm working on a react+redux project and need to add a new connected component Comp, my process is:
mkdir src/components/Comp
touch src/components/Comp/index.tsx
export interface CompProps {}
export class CompComponent extends React.Component<CompProps>
export const Comp = connect(...)(CompComponent)
touch src/components/Comp/actions.tsx
export const FOO_ACTION = '...'
export type FooAction = ActionPayload<{ foo: string }>
export const doFoo = (foo: string) => {...}
export function reducer(state, action) {...}
(sorry for bad formatting, on mobile)
So at a minimum for a single component I might have 7 exports. For a non redux component I'd probably only have 2 or 3, depending on whether I needed a state types interface.
I could put many of these in their own files or something, but it becomes cumbersome very quickly. I don't necessarily use all of those exports, but when I do need them it's really handy to have vscode import them automatically for me.
Again, this is all personal style. To me, a default export is conveying the idea that a file only has a single thing you should use. I use default exports in those cases, but it's just not as common.
2
u/ryansmith94 Aug 28 '18
Yeah totally agree with your usage of default exports, I also tend to use them when I want to imply that it's the only thing that "should" be used from the file.
8
u/flying-sheep Aug 27 '18
all of this is implementation-specific.
you can turn on a linter that complains if you default-import something with a different name as it was defined as or even the filename.
a better refactoring will understand when a class/function is imported under the same name that it was defined with and will change both.
a decent autocompletion will of course suggest the default import. thatâs the whole point of ES6 import syntax: all of it is statically analyzable. The only problem here is when the exported object isnât a class or named function, because then the name has to be âmagicallyâ derived from the filename.
Theyâre right about the CommonJS interop, but i hope few people have to write anything else than ES6+ these days. Typos are a problem of course. You donât want to write a linter for everything like this.
3
u/quick_dudley Aug 27 '18
I'd be reluctant to have my linter complain about what names I use for default-imports because sometimes using a different name is the only way to avoid a collision.
2
u/flying-sheep Aug 27 '18
that only means the linter canât be dumb and has to take that into account.
-1
3
u/allicanseenow Aug 27 '18
Title is misleading and can hinder inexperienced users from following this nice coding convention.
This totally depends on the situation. Just an example in React where most of the time you will write components, "export default class" is and always the most accurate and concise behavior because:
You never rename the class, and you should always have 1 class in a file. That's also how you do should consider doing in OOP by the way.
If there is a sub class, put it in a separate files
For backend, where you can export a lot of APIs from a file, it is better if you put exported components in an object, just like how you do it with module.exports.
3
u/Klathmon Aug 27 '18
Or one of my favorite conventions:
export class Thing extends... // later... export default connect(Thing)(...Your "usable" component is always default exported, and the un-connected component is always exported by name for testing.
3
u/Akkuma Aug 27 '18
The way we've been doing it where I work is you have
/components/SomeComponentunder there is a SomeComponent.js, which is written as a "plain component" without HOC wrapping it. So it'll be something likeexport default class. The index.js then pulls in that component and wraps it as necessary with HOC components and is anexport default connect(SomeComponent).1
u/Klathmon Aug 27 '18
That was the same setup I was using before I read about the convention I put above, which I really enjoyed and it reduces the amount of boilerplate and puts the connect call in the same file.
When you use selectors (located elsewhere), and action creators (also located elsewhere) with redux, it makes a lot of sense as your actual call to
connectis normally only a few lines if that, and it always felt kind of silly to have a full file for that.It makes more sense if you are doing ad-hoc "selectors" in your call to
connect, or are combining several HOCs at once, as then it warrants the larger file, but that's more of a difference in overall architecture.But in the end, as long as you have a plan and stick to it it's fine, the problems show up when you don't have any direction on how to structure things and every file ends up being different.
-3
u/allicanseenow Aug 27 '18
If you use redux, you should not export the class but âexport defaultâ the wrapper instead. There is no reason why sometimes you only want to export the class, but sometimes you export it wrapped in a Redux wrapper
2
u/Klathmon Aug 27 '18
Read the rest of my comment, it's for tests.
That way you can
import { Thing } from './thingin your test files and be able to test the component without having to stub out redux.It's a fantastic convention that allows you to get the full benefits of easy-to-test react components with the globally-connected nature of redux.
-3
u/allicanseenow Aug 27 '18
But do you really want the class without Redux? In the end, redux is also a part of the logic a container has that needs to be tested.
I mean even in a test.js file, you will still want to test functionality that redux props bring? I donât think it is a good idea to separate the logic of redux from the presentational component. But if you really need to test the presentational ones, you can directly import the class as we should isolate the container, which is wrapped by a connector, and the presentational component,
4
u/Klathmon Aug 27 '18 edited Aug 27 '18
Yes, you really want the class without redux, you test redux separately. In fact, you test the action creators, the selectors, and the reducers, all separately.
You are talking about more of an integration test, or even higher level than that.
And why go through all that trouble of stubbing out what you need to just to get the component underneath, when you can just import it by name...
No extra downsides...
And, if you want to import the fully connected component for testing too, you can do that as well!
3
Aug 27 '18
Isnt this more about tooling? Intellij solves most if not all problems you mentioned for me
2
Aug 27 '18 edited Aug 27 '18
Especially in dynamic settings, for instance node threads or the webs import() spec, it's pretty useful being able to import something with little more than a url without having to know internals and names. Personally i think it's a bit weird to do import { foo } from 'foo' if 'foo' is the the only export and i try to avoid it whenever possible. Wouldn't feel right to discard something useful just because some tool can't (yet) deal with it.
2
u/TheFuzzball Aug 27 '18
`export default` considered harmful if you use TypeScript and rely on Intellisense.
3
u/spacejack2114 Aug 27 '18
Actually it's pretty smart if you give a name to the default export. Eg:
export default function foo() {}instead of
export default function() {}Or with a bit of boilerplate:
const foo = "something" export default foo
2
u/kowdermesiter Aug 27 '18
If you refactor Foo in foo.ts it will not rename it in bar.ts.
Yes it will if you use a sane IDE like VSCode
If you end up needing to export more stuff from foo.ts (which is what many of your files will have) then you have to juggle the import syntax.
Oh the horrors... it was a single variable, now it might be 2 or god... three.
If a module has default exports then I don't need IntelliSense, it's a single damn module
You don't get typos like one dev doing import Foo from "./foo"; and another doing import foo from "./foo";
So yeah, that takes a few second to discover. I doubt that this is a problem since you start with the import and using the imported variable will come from the autocomplete.
Keep using defaults folks when it makes sense, like a module only contains a single class / object / variable.
1
Aug 27 '18
Your comment doesn't make much of a case for defaults, other than characterising the objections as whining. I am curious to know why some people prefer them. I am aware of the airbnb decision but I am not completely convinced. In fact, I don't understand why they added this quirky and seemingly unnecessary extra to the specification.
1
u/kowdermesiter Aug 27 '18 edited Aug 27 '18
It IS whining or bikeshedding to say the least.
Thanks for the AirBnB link, I missed it.
I am curious to know why some people prefer them.
Like I said, if you have a single thing in a file, then defaults are perfectly fine.
I'm using it right now: https://imgur.com/a/20Wutgv
It's just nice that I don't have to use
{ Stuff }to import a class when it's all that a file contains.MDN says the same thing:
1
1
u/ghostfacedcoder Aug 27 '18
News at 11: Variables that start with the letter "A" are considered harmful. Why? Because I say so that's why!
1
1
u/loopsdeer Aug 27 '18
This is exactly why I use the ternary operator and goto for all control flow. Just because someone is a savant of algorithms does not make them a brilliant software engineer.
1
u/tchaffee Aug 27 '18
Is Typescript necessary for all your examples? If not, I think it just muddies the waters and makes your article easier to dismiss for those who don't use Typescript. Other than that, I usually need more time to try out something opinionated like this before deciding, but this looks promising.
3
u/cspotcode Aug 27 '18
The link points to a book about TS, so logically all the examples are written in TS.
1
u/tchaffee Aug 28 '18
Thanks for pointing that out. Makes more sense now. If the author wants to make the case for a wider audience, it really deserves its own article outside of the TS context.
35
u/dassurma Aug 27 '18 edited Aug 27 '18
As others have already pointed out, a lot of this is a (valid) complaint about tooling. A couple of remarks I do have, though:
Adding named exports: If you started with a default export, and want to export more, the import syntax can accommodate you quite easily: Go from
import theDefault from "./module.js";toimport theDefault, {namedExport1, namedExport2} from "./module.js";Re-exporting: Note that you can do
export {default} from "./module.js";or even rename exportsexport {default as nowWithName} from "./module.js";