This site runs best with JavaScript enabled.

Improving the usability of your modules

November 06, 2017

Video Blogger

Photo by NASA on Unsplash

How to make your modules more generically useful and easier to test.

This last week I worked on my team's internationalization (aka i18n) solution. We call it react-i18n (if we ever open source it, we'll need to rename it, because that's already taken). It's pretty neat and really small. I'm not going to talk about why we don't use any of the myriad of other tools that do this (maybe I'll save that for another blog post). What I want to talk about is something I did to make that module more usable.

One feature of the module is that it will automatically load your server-rendered content for you. At PayPal we have another module called react-content-loader. This is an express middleware that relies on conventions used in Kraken and inserts the content for the user based on their language preferences. For example, let's say that you have a file:

1// locales/US/en/pages/
2header.title=PayPal Rocks
3header.subtitle=No really, it does

Then this middleware would insert this in the bottom of your page (for US users with en as their preferred language):

1<script type="application/json" id="react-messages">
2 {
3 "pages/home": {
4 "header": {
5 "title": "PayPal Rocks",
6 subtitle: "No really, it does"
7 }
8 }
9 }

Then react-i18n will automatically load that on the client side. All you have to do is use it:

1import getContentForFile from 'react-i18n'
2const i18n = getContentForFile('pages/home')
4function App() {
5 return (
6 <div>
7 <h1>{i18n('header.title')}</h1>
8 <div>{i18n('header.subtitle')}</div>
9 </div>
10 )

So that's how it works (again, I'm sure some of you are thinking of other libs that could do this better, but please spare me the "well actually." I'm aware of them, I promise). Now that you understand basically how this works, I want to talk about a few things that I changed about it to make it more usable.

I'll show you

No side-effects on import

So you'll notice that when we use react-i18n on the client in the example above, we don't have to do anything to initialize or bootstrap it with content. It automatically gets those from the DOM. It does this inside the main export from react-i18n. This way when you import react-i18n, loading the content happens for you. This is a handy feature. But it comes with the trade-off that the main module in react-i18nhas side-effects in the root-level of the module. For example:

1// react-i18n/index.js
2// ... stuff
3// side-effect!
4const content = JSON.parse(document.getElementById('react-messages'))
5// ... more stuff
6export {getContentForFile as default, init}

This presents a few challenges for users of the module. It means that they have to be aware of what happens when they import your module. They have to make sure that they don't import your module before the global environment is ready for it. And that problem manifests itself not only in the application environment, but also in the test environment! And unless you take care to give good warnings when the environment isn't ready (if you even know), people will get cryptic error messages when doing seemingly unrelated tasks (like importing some module that happens to import your module somewhere in the dependency graph).

Another issue is that there could be a reason to configure the initialization process. What if my node doesn't have the id react-messages, but instead uses i18n-content? Or what if I don't server-render the messages at all and they're coming from an ajax request? Turns out that react-i18n actually exposed another module react-i18n/bootstrap to customize this behavior which is great, but that doesn't resolve the problem of stuff happening if someone were to import react-i18n first.

So what I did was a wrapped all side-effects in a function I exported called init(which was similar to the bootstrap thing it already exported):

1// react-i18n/index.js
2// ... stuff
3function init(options) {
4 // ... other stuff
5 // side-effect! But it's ok now because that's clear
6 const messages = JSON.parse(document.getElementById('react-messages'))
7 // ... other other stuff
9// ... more stuff
10export {getContentForFile as default, init}

So this means that anyone using the module now must call the init function, but they're doing that on their own terms and whenever they want it to happen which I think is the key difference. It doesn't matter whether someone imports this module before initialization takes place. It also gives us an opportunity to give a more informative error message if they fail to initialize before they start using the module.

The key is that your module shouldn't do side-effects when it's imported. Instead, export functions which perform the side-effects. This gives the users control over when and what happens. Even better is to not have any side-effects at all if you can help it (which is actually also possible to accomplish with my reworking of react-i18n), but that's a subject for another newsletter.

Make it generic

Before, this library was actually just a part of our app. So we could easily rely on the fact that the JSON object was a nested object where the first key was the name of the localization file and the rest was just a nested version of the contents of that file (as you can tell in the example above). And the implementation and examples in the docs were all geared toward this use case. However, we're in the process of "inner sourcing" this module (and perhaps open sourcing it eventually), so folks are going to use it who use different tools and have different use cases.

So, if it's not too much work and doesn't add too much complexity, then try to make the solution more generic. So now, the implementation doesn't care about the fact that the root level of the localization object is a file name and the rest is the contents of that file. All it cares about is the fact that it's a nested JavaScript object. This means that whereas before, you had to do this:

1import getContentForFile from 'react-i18n'
2const i18n = getContentForFile('pages/home')
4// etc...
6// etc...

You can now do this:

1import getContent from 'react-i18n'
3// etc...
7// etc...

So each invocation of getContent will return the content or if the content is another nested object it'll return another content getter function. I call this "sota-curried" because it's not really currying, but it kinda looks like it a little bit.

Now PayPal's react-i18n is more generically useful because the implementation and documentation don't assume you're using react-content-loader. And as it turned out, doing things this way actually made the implementation simpler! Wahoo!

I should mention also that you can't predict the future, and that's what you sort of have to try to do when building a generic library. While you're doing this, you need to balance usability with the YAGNI principle. I only put this effort in because we were extracting this from our project so others could use it and we needed to support these use cases. Beware of pre-mature optimizations (that's not limited to performance situations, but also features/complexity as well).

There were several other things I could talk about, but I'm going to wrap this email up with this. I hope that you find ways to remove side-effects from the root-level of most of your modules and find ways to make your solution more generic without sacraficing usability or implementation complexity.

Good luck! And stay awesome 😎

Who's awesome? You are!

Things to not miss:

  • "Addressable Errors" by Guillermo Rauch. I was thinking about writing my blogpost as an expansion on this (I think it's a great idea, in addition I think you should version the error messages with your project). Maybe one day.
  • "The Future of RethinkDB" on The Changelog. It's a great example of a community rallying together to keep open source going. And RethinkDB seems really awesome so I'm glad it's pushing forward!
  • Flexbox Zombies. The best way to learn flexbox by the amazing Dave Geddes. Also check out Grid Critters (to learn CSS Grid, also by Dave) which will be released soon!
  • Code Sponsor. I've already shared this, but they have a bunch of money they can give developers who want their OSS work to be funded and are looking for more folks to sign up. So if you're a dev who does OSS, give them a look :)

Discuss on TwitterEdit post on GitHub

Share article
loading relevant upcoming workshops...
Kent C. Dodds

Kent C. Dodds is a JavaScript software engineer and teacher. He's taught hundreds of thousands of people how to make the world a better place with quality software development tools and practices. He lives with his wife and four kids in Utah.