Code reuse is one of the most efficient ways to speed development and reduce overhead around your codebase maintenance.
However, it’s important to remember that “reuse” really means “use”. Code shouldn’t be over-generalized and needs to evolve and change over time, so “reuse” means you have to balance abstraction and flexibility.
For this purpose we created an open source platform called Bit, which makes it a whole lot easier to create reusable code and collaborate at any scale.
Here’s why we built it, how it works, and how you can put it to use.
- Bit is open source and currently supports JS.
When talking of code reuse, we usually talk of components and modules (sets of files) in our projects, and how to use them in multiple projects.
It also means making these components/modules discoverable to other developers ans team members to find and use in their projects.
Before Bit, this process involved 3 steps which generate a lot of overhead.
- Publish a package: You’d have to split your project, create a new repository, configure its environments, and publish it to NPM. Even for a small component, this can take hours of work.
- Discoverability: Discoverability for small packages is a major problem. In the open source community, it’s a major pain point. Within organizations, this problem get even worse.
- Making changes: Making changes to any package requires going through its original repository and owners, republishing it, etc. This is a very limiting and cumbersome process which makes it hard to evolve the code.
Put together, these issue are usually enough to block code reuse at scale, leading to more duplications, painful maintenance and slower development.
The truth is, code reuse really shouldn’t be this hard.
Imagine you could choose any set of files, make them reusbale without refactoring anything, let other people easily find and use them in their projects, and easily make changes from any project while keeping sync.
This is the exact workflow Bit provides, which can be leveraged to easily scale code reuse and collaboration. Let’s see how.
In short, Bit eliminates the overhead of making code reusbale, and enables code changes and collaboration between developers and projects. BIt also makes code more discoverable. Here’s a detailed breakdown.
Instead of having to split repos, Bit automatically isolates sets of files from any given project, without refactoring a single code line.
bit add command, you can point Bit to directories or paths in your project. Bit will isolate these reusbale components, with all their dependancies, and create an isolated environment for each of them.
Instead of configuring build and test environments, you can import pre-made environments (example) to apply for all the components in the project. Bit will use these environments to build and test the code in isolation.
Then, you can
bit tag a version for the code and
bit export it to a remote store. The fastest way is to use Bit’s open hub, which is free for open source code and for a limited amount of private code.
This process can take a few seconds or minutes, no code changes or repository splitting are required, and the code becomes available to discover and use in any other project, with tools like NPM and Yarn or with Bit itself (see below).
Code made reusbale with Bit is also made quickly discoverable to find.
This becomes possible thanks to 3 main features:
- A Search engine that helps you find small pieces of code in any scale.
- Code visualization including auto-parsed docs and examples (from the code itself using JSDocs or readme files) and even a live rendering playground when working with UI components.
- Test and build are run in isolation by Bit for every reusbale code component, and results are presented to provide you with better confidence that the code can run in your project.
Evert component and module made reusbale can be instantly found and put to use in any new project, to build new projects faster.
Code shard with Bit can be installed using package managers like NPM, and can also be imported into any project using
When imported, you can make changes to the code directly from any project using it (!). This means you don’t have to go through the code’s source repository or ask for permission to change it.
Changes can than be suggested as a new version, updated if needed and even synced between different projects, leveraging an extension to Git itself, using the
bit merge command.
Thanks to Bit’s code isolation, any developer can use and develop the code from other projects. For example, components written in typescript can be used and developed in a project written in flow-typed.
With Bit, it doesn’t matter if you share 10 React components or 1000 reusbale utility functions.
Bit was built to support code reuse at scale, providing discoverability and control.
For example, you can create collections for different teams, collaborate to make changes, and scale your workflow.
Bit also supports most kinds of architecture, from multiple repositories and microservices to Lerna multi-package “monorepos”, where Bit can help discover and collaborate on components within the repo while automatically handling dependancies to save hours of work.