Libraries vs. Frameworks
In the world of programming, the difference between a library and a framework is often a source of confusion and debate. In general, the differentiating feature is a matter of control — frameworks dictate how your project will be structured, whereas libraries are building blocks that can be used anywhere. If we use the metaphor of architecture, libraries are bricks, and frameworks are, fittingly, the frame.
Example: jQuery vs. AngularJS
To illustrate the differences between libraries and frameworks, let’s look at two important open source solutions for building interactive webpages — jQuery and AngularJS.
Fast forward another decade to 2017: web development is going through a similar transformation with the emergence of MV* frameworks such as React and AngularJS. Like jQuery, they offer huge, time-saving abstractions for DOM manipulation. But unlike jQuery, they can’t be seamlessly incorporated into an existing project — as frameworks, they mandate how your code is structured and run.
For instance, one of the most useful abstractions in AngularJS is data binding — tying the value of some variable to what the user types:
The catch is that we can’t just add a <script> tag that pulls in AngularJS and start writing templates like this. The HTML needs to be part of an AngularJS component, which will parse it and generate the code we otherwise would have had to write ourselves. In truth, this minimal example requires dozens of lines of additional boilerplate before it will work.
Libraries and Frameworks at DataFire
With DataFire, our mission is to make your data easily accessible, no matter where it lives or what you want to do with it. We provide open-source integrations for over 500 different services, such as Slack, GitHub, and MongoDB, as well as a command-line tool for connecting them. But is it a library or a framework?
The answer is both. For each service we connect to, we provide a client library, available on npm. For instance, you can install the Reddit RSS library like this:
npm install @datafire/reddit_rss
Just like jQuery, you can use this anywhere you want Reddit data. Each of the 500+ DataFire clients can be incorporated into an existing codebase without modification.
But DataFire also provides a framework — a set of high-level abstractions for building, connecting, and triggering actions. If you’re looking to build an API or sync data between two services, the DataFire framework can help.
To use it, you need to install the DataFire command-line tool:
npm install -g datafire
Then, inside your project, create DataFire.yml:
Finally, start running your project:
datafire serve --port 3000
Conclusion: Tradeoffs and Control
There is a clear tradeoff when deciding whether you should be providing a library or a framework, and it all comes down to control.
Libraries offer the user a large amount of control, and have a low barrier to entry. They can be seamlessly incorporated into existing projects to add new functionality, and generally require little learning and boilerplate code to get started.
Frameworks are more powerful, but mandate how users organize their code. They are more likely to be used when starting a new project rather than incorporated into an existing codebase, and the developer will need to learn about the framework’s core concepts before they can start using it productively. But this inversion of control also helps developers avoid common errors, inefficiencies, and bikeshedding.
Overall, frameworks are more opinionated and libraries are more flexible. Both patterns of abstraction have their place in the world of programming, and while neither is inherently better, it’s important to determine which is appropriate for the problem you’re solving.