1

What's the difference between a library and a framework?

 1 year ago
source link: https://dev.to/ben/whats-the-difference-between-a-library-and-a-framework-1eaj
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Cover image for What's the difference between a library and a framework?
Ben Halpern

Posted on Jun 17

What's the difference between a library and a framework?

This is totally up for debate, so I figured I'd ask.

Discussion (22)

pic

CollapseExpand

Author

Jun 17

I was inspired to ask based on seeing this on Twitter, which I think is a good take, but could spark further discussion

Jason Miller 🦊⚛ profile image
Jason Miller 🦊⚛
twitter logo
"I distinguish between a library and a framework by looking at the Inversion of Control."

I really like this concrete distinction.

→ a library slots into your code (you invoke it)
→ a framework has slots for your code (it invokes you)

surma.dev/things/cost-of… twitter.com/DasSurma/statu…
14:12 PM - 17 Jun 2022
Surma @DasSurma

📝 Friday blog post: I tried to write up my mental model of how to develop for developers.

It’s easy to over-do it with abstractions. Escape hatches, layering abstractions and reusing the idioms of the platform gives developers agency.

👀👇 https://t.co/O555TfwPHz

Comment button Reply

CollapseExpand

For me this was never a fact open for debate but simply accepted software engineering knowledge since the Johnson & Foote paper Designing Reusable Classes (1988).

One important characteristic of a framework is that the methods defined by the user to tailor the framework will often be called from within the framework itself, rather than from the user's application code. The framework often plays the role of the main program in coordinating and sequencing application activity. This inversion of control gives frameworks the power to serve as extensible skeletons. The methods supplied by the user tailor the generic algorithms defined in the framework for a particular application.

React is a Framework

Comment button Reply

CollapseExpand

To me - library is just one of the layers of the framework, like React is a library for building user interfaces, so in order for it to become a framework, it would need a router, data layer management, and "enforce" structure, which frameworks like EmberJS/Angular do. Frameworks are usually opinionated. Libraries are pieced together to make your own framework.

Comment button Reply

CollapseExpand

Frameworks are opinionated. Libraries are pieced together to make your own framework.

This is exactly the thing I came to say here.

React is a library and as such, it "gives you tools to build your own toolbox".

Rails is a framework and as such, it gives you opinions on how you should do things with its own tools/solutions.

Comment button Reply

CollapseExpand

From the same article:

Offramp: For me, this is one of the benefits of libraries vs frameworks. Especially at a larger scale, you start by using a framework, but at some later point in time, you find yourself having outgrown the framework (or the framework has stagnated for too long), and you want to switch. This can be really hard, because frameworks shape your code, and each framework is different, so there is no easy migration path. The contact surface with libraries, on the other hand, is often very small, making a switch from one library to another a much smaller task, with a low blast radius.

Because "frameworks shape your code" they are imposing their own constraints that shape (, constrain, and impact) your solution architecture—that in my mind is expressing an "opinion", a vision of how things should be done.

So frameworks are inherently opinionated.

Concretely one can move from React, to Preact because Preact deliberately mirrors React (to a point). It's going to be more work to move to something like Inferno or even SolidJS. By the time we get to Svelte we're mostly talking about a rewrite.

To me the whole "React is unopinionated" narrative is mostly marketing because it's less opinionated than other frameworks which by design are more prescriptive—because their community values:

"There should be one-- and preferably only one --obvious way to do it."

PEP 20 – The Zen of Python

Comment button Reply

CollapseExpand

I think React has elements of both a library and a framework. But I won't argue about it, I've seen people argue about it and it's a dumb thing to argue about :) In the end, it doesn't really matter which label we slap on it, it is what it is regardless.

Comment button Reply

CollapseExpand

I like to think that if a library is a tool, then a framework is platform. You utilise a tool to build upon something, but you build on a platform. A framework is rather self descriptive IMO. It is a scaffolding on which you build your system. A library is a set of tools you use to build your system.

Comment button Reply

CollapseExpand

A framework dictates your code's architecture flow, whereas a library is something you call and can fit in to whatever structure you'd like. An illustrative example for me was learning egui, a library for defining UI elements, by using eframe, a framework built around egui. I tried just using egui on its own, but it would have required a lot more reinventing the wheel, and making design decisions that eframe dictates for me in a way known to work well with this library.

Comment button Reply

CollapseExpand

I count things like React hooks as framework. Even though a hook is called as though it is a library, it becomes a design language that requires direct dependencies all over the code base. And their functions integrate with the renderer. The overall picture is a framework.

Comment button Reply

CollapseExpand

I have some (quite possibly odd) ideas:

A library does one thing.
But a framework works with many library's or does everything in one task. Basically a toolkit.

So for example:

  • react and rails are frameworks
  • but penrose is library.

Comment button Reply

CollapseExpand

A library gives you aids to help you coding.
A framework makes you adopt a way to code.

So lodash is a library, but Angular is a framework; jQuery started as a library, it ended up being a framework.

React is a framework, according to the definition above.

Comment button Reply

CollapseExpand

Library is like building your home from scratch. The house can be defined in any manner you like it can have just as many rooms you want. Framework, on the other hand is like moving into an already built house you don't have to worry about building problems but you cannot choose how the rooms are arranged/built.

Comment button Reply

CollapseExpand

A library is a piece of wood, and a framework is a frame of window made with wood that can be installed right away into the wall. We can customize the frame but it just works fine without customizing.

Comment button Reply

CollapseExpand

If you shout in a framework you don't get thrown out?

Comment button Reply

CollapseExpand

CollapseExpand

The same question confused me in the second last interview, the interviewer told me library is something that is written from scratch eg. React which is purely top on built of javascript & framework is built on top of something eg. React native which is built on the top of React.

Comment button Reply

CollapseExpand

A framework is

An essential supporting structure of a building, vehicle, or object. [lexico.com]

A library is

A building or room containing collections of books, periodicals, and sometimes films and recorded music for use or borrowing by the public or the members of an institution. [lexico.com]

So I guess a library could be built on a framework.

Try the buffet, etc., etc.

Comment button Reply

CollapseExpand

I agree with your example. A framework is the basis for a project (think create-react-app), a library is bolted on top (e.g. jQuery).

It gets interesting with petite-vue, which provides a bolt-on solution for framework-like features. Not sure how to categorize that.

Comment button Reply

CollapseExpand
<script type="module">
  import { createApp } from 'https://unpkg.com/petite-vue?module'

  function Counter(props) {
    return {
      count: props.initialCount,
      inc() {
        this.count++
      },
      mounted() {
        console.log(`I'm mounted!`)
      }
    }
  }

  createApp({
    Counter
  }).mount()
</script>

This starts it effectively making it the "main program"

  createApp({
    Counter
  }).mount()

This is user code that is called by it

  function Counter(props) {
    return {
      count: props.initialCount,
      inc() {
        this.count++
      },
      mounted() {
        console.log(`I'm mounted!`)
      }
    }
  }
  • If your code calls it, it's a library.
  • If it calls your code, it's a framework.

therefore it (petite-vue) is a framework.

Comment button Reply

CollapseExpand

Framework = set of rules you have to follow in order to use it efficiently (requires more things to setup to fit your needs)
Library = extend your tools seamlessly with new features you can use

Comment button Reply

CollapseExpand

A library is a collection of APIs that you can adapt to fit your code, whereas a framework is a collection of APIs and patterns you adapt your code to fit into.

Comment button Reply

CollapseExpand

As others have said

"Libraries live inside your code, your code lives inside a framework. "

.NET MVC is a framework because all code lives inside of the MVC paradigm.

Comment button Reply


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK