Libraries vs. Frameworks
Not everything that looks like a library is one! But what’s the difference?
When we write code - we reuse stuff! And the things we reuse, we usually call a library. But some other things we reuse we call frameworks. But what is the difference? We call Angular a framework but while we call the things we import into our NodeJs projects libraries… So what is the distinction, what makes a framework a framework, what makes a library a library?
Is it a question of size? Not really, we call React a library, but Angular a framework - when they are comparable. Also there are samples of frameworks that are tiny code bases and examples for huge libraries out in the wild.
It’s not a question of who wrote the code we reuse either, since we would have to call everything we did not build ourself the same thus removing the need to a distinction completely!
Does it depend on the language environment we are in? In Dotnet we refer to our Nuget dependencies as libraries, but then again; Asp.Net is also shipped as a Nuget package but we call this a framework!
The distinction is not in any of these ‘simple’ characteristics and to be fair - almost everybody uses these terms interchangeably:
After a lot of discussion around a table at SOCRATES Switzerland back in 2018 we came up with a definition:
Frameworks impose structure on your code, libraries do not!
This means, that for example: Angular is a framework because it forces you to use services, components etc. for your application to work correctly. By this definition React is a framework too, while Reactive Extension are not.
For the .Net world this means that
AspNetCore is a library, since it just defines how HTTP request are handled by a webserver like Kestrel and does not impose any structure onto any other part of your system. Now this is not to be confused with
AspNetCore.Mvc (which is an extension of
AspNetCore) which is by this definition clearly a framework b/c it enforces the use of things like dependency injection, the MVC pattern or certain configuration features.
So, knowing the difference between libraries and frameworks: What is better? Should we only be using libraries or frameworks to build our applications? It depends:
Use frameworks when:
- your use case falls into the purview of the framework (i.e use AspNetCore.Mvc to write web applications and not GRPC-based microservices)
- you know you can stay withing the constraints the framework enforces on you. (This is a tricky one and takes some experience with a given framework)
Use libraries when:
- frameworks are too constraining for your use case
- you know that you have the time and the ability to write the clue-code that comes with combining libraries
- you know the libraries you want to combine and are confident that using them has a significant benefit over using a framework!
Distinguishing when to apply libraries or a given framework is really a skill that comes with experience doing both - and sometimes getting it wrong!