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

Ben Pereira - Sep 11 '23 - - Dev Community

First before we can dive in the differences, it makes sense to get to know each of those in depth:

Libraries

A library is a set of blocks of codes written by an author that can be re used by anyone to perform specific behaviors and actions.

Let’s take Lombok as example of a library, in it’s website states:

Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.
Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.

Lombok’s idea is that allows you to reduce boilerplate through mitigation of methods creation for encapsulation, metrics, builders, etc..

Why would you say Lombok is a library? it’s a block of code that can be used anywhere basically, is not constrained by an environment or lifecycle, either workflow.

You can use Lombok with other libraries and should be fine (most of the time).

Besides that this java library have a very well defined purpose, even with multiple usages and lots of different configurations the idea of it is well defined through it’s page.

Frameworks

Using the same idea of Lombok let’s get one framework as example and showcase it in depth, Struts!

Apache Struts is a free, open-source, MVC framework for creating elegant, modern Java web applications. It favors convention over configuration, is extensible using a plugin architecture, and ships with plugins to support REST, AJAX and JSON.

Just from the initial description you get an idea that Struts offer much more functionalities than Lombok and not just that already conveying a way of how you should make your application, as a web app.

If you go through the bird eye and key technologies you will see a lot of functionalities that Struts provide and a lot of support on several other libraries.

The Difference

Both can provide a lot of different functionalities, some more, some less, but the main difference between a framework and a library is the purpose I would say.

Frameworks are a sum of libraries that are tied together to help you achieve your necessity as easy as it can, allowing you to have a lot of definitions standardized before you even start to do something about it.

With that in mind that’s why you see a lot of articles talking about Inversion of Control — IoC when there is differences between framework and libraries.

Let’s take Struts as an example. People use Struts to make web applications, if you try to build a different kind of application with this framework you would be just using the libraries from it and not actually using the most that Struts has to offer.

When you use Struts you already have what’s needs to be done to make a web app. You follow through their guidelines, might have some options for little bit of flexibility, but almost everything is defined already.

The framework already has guidelines and usually it doesn’t allow you do try things differently not by default, you would have to create your custom one, and sometimes that might not even be possible, in those cases you would make more of it using the libraries instead of the framework itself.

Conclusion

Framework and Libraries are bunch of codes built to be reused but the main difference being that Frameworks have specific purposes that are created to make your life easier by taking control of most of the processes which is constrained by the definition of it’s standards, which is not bad because it makes your life easier (but when it comes to customization there might be heavy costs involved).

And libraries are set of codes that help you to deliver what’s needed but leaves you off a constrained standard to be followed on by them.

Examples of Libraries

  • Google Guava
  • Scala Library
  • Apache Commons Lang
  • Jackson Databind
  • Logback
  • Log4j2

Examples of Frameworks

  • Struts
  • Spring
  • Mockito
  • JUnit
  • Play
  • Grails
  • Dropwizard
  • Hibernate

That’s it! If there is anything thing else to discuss feel free to drop a comment, if I missed anything let me know so I can update accordingly.

Until next post! :)

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .